Click here to Skip to main content
12,405,700 members (64,436 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C ASP.NET
Please Some one help me with adding a row to gridview dynamically

I have created a grid using a data table as follows..
/***Code to make a table**/
DataSet ds1 = new DataSet();
      DataTable dtable = new DataTable();
      DataColumn dt = new DataColumn("Packaging Size");
      DataColumn dt1 = new DataColumn("Unit");
      DataColumn dt2 = new DataColumn("No Of Unit");
      DataColumn dt3 = new DataColumn("Total Product Use");
      DataColumn dt4 = new DataColumn("unit");
      DataColumn dt5 = new DataColumn("Date");
 

      dtable.Columns.Add(dt);
      dtable.Columns.Add(dt1);
      dtable.Columns.Add(dt2);
      dtable.Columns.Add(dt3);
      dtable.Columns.Add(dt4);
      dtable.Columns.Add(dt5);
      ds1.Tables.Add(dtable);
then----

DataRow dr = ds2.Tables[0].NewRow();
       ds2.Tables[0].Rows.Add(dr);
       dr[0] = TextBox9.Text;
       dr[1] = DropDownList1.SelectedItem.Text;
       dr[2] = TextBox11.Text;
       dr[3] = TextBox12.Text;
       dr[4] = TextBox13.Text;
       dr[5] = DatePicker3.SelectedDate.ToString();
--but this works only for 1 row...

i want it for mulatiple rows..

Plzzzzzzzzzz help...
Posted 1-Mar-10 20:00pm
Updated 20-Aug-12 19:51pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

The simple way is -

//create datatable and columns,
DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Column 1"));
dtable.Columns.Add(new DataColumn("Column 2"));
 
//simple way create object for rowvalues here i have given only 2 add as per you requirement
object[] RowValues = { "", "" };
 
//assign values into row object
RowValues[0] = "your value 1";
RowValues[1] = "your value 2";
 
//create new data row
DataRow dRow;
dRow = dtable.Rows.Add(RowValues);
dtable.AcceptChanges();
 
//now bind datatable to gridview... 
grv.datasource=dbtable;
grv.databind();
Do necessory changes and dont forget to
dtable.AcceptChanges();
grv.datasource=dbtable;
 
grv.databind();
this will work for 1000's of times....
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

any more answers plzz..??? My main problem is my gridview`s data set get overwriten...
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100