Click here to Skip to main content
14,691,375 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Updated 31-Dec-19 22:49pm
v2

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();<br/>grv.datasource=dbtable;<br/><br />
grv.databind();<br/>

this will work for 1000's of times....
   
v2

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900