Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DataTable dt = new DataTable();
            DataRow dr = dt.NewRow();
            DataColumn col = new DataColumn(); ;
            col = new DataColumn("col1");
            dt.Columns.Add(col);
            col = new DataColumn("col2");
            dt.Columns.Add(col);
            col = new DataColumn("col3");
            dt.Columns.Add(col);
            col = new DataColumn("col4");
            dt.Columns.Add(col);
            dr["col1"] = this.drpdwnmedicine_name.SelectedValue;
            dr["col2"] = this.drpdwnmanufacturer_name.SelectedValue;
            dr["col3"] = this.dosage.Value;
            dr["col4"] = this.quantity.Value;
            dt.Rows.Add(dr);
            //dt.AcceptChanges();
 
            displaycartgridview.DataSource = dt;
            displaycartgridview.DataBind();




dear everyone...
the above code works fine..but it s not adding multiple rows..only one row it s creating ..then if we add one more row it is replacing old rows..so pls help me to come out from this
Posted
Updated 17-Aug-11 21:29pm
v2

Here you create a new data table every time this method is called, so all rows are lost.
DataTable dt = new DataTable();


You should separate the table creation and row adding to different methods so that the table is created once and then the rows are added to it.
 
Share this answer
 
make a loop for the no. of rows you want to add in the loop put the codes like


DataRow dr = dt.NewRow();
dr["col1"] = this.drpdwnmedicine_name.SelectedValue;
dr["col2"] = this.drpdwnmanufacturer_name.SelectedValue;
dr["col3"] = this.dosage.Value;
dr["col4"] = this.quantity.Value;
dt.Rows.Add(dr);



and bind the gridview outside the loop after filling the datatable


hope this will help
 
Share this answer
 
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