gridview不添加多行 [英] gridview not adding multiple rows
本文介绍了gridview不添加多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DataTable dt = 新 DataTable();
DataRow dr = dt.NewRow();
DataColumn col = 新 DataColumn(); ;
col = 新 DataColumn(" );
dt.Columns.Add(col);
col = 新 DataColumn(" );
dt.Columns.Add(col);
col = 新 DataColumn(" );
dt.Columns.Add(col);
col = 新 DataColumn(" );
dt.Columns.Add(col);
dr [" ] = 此 .drpdwnmedicine_name.SelectedValue;
dr [" ] = 此 .drpdwnmanufacturer_name.SelectedValue;
dr [" ] = 此 .dosage.Value;
dr [" ] = 此 .quantity.Value;
dt.Rows.Add(dr);
// dt.AcceptChanges();
displaycartgridview.DataSource = dt;
displaycartgridview.DataBind();
亲爱的大家...
上面的代码可以正常工作..但是它不会添加多行..它只会创建一行="h2_lin">解决方案
在这里您每次都会创建一个新的数据表,此方法被调用,因此所有行都将丢失.
DataTable dt = new DataTable();
您应该将表的创建和向不同的方法添加行分开,以便一次创建表 ,然后将行添加到表中.
为没有您要在循环中添加的行数将类似
的代码放入
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);
并在填充数据表后将gridview绑定到循环外
希望对您有所帮助
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
解决方案
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.
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
这篇关于gridview不添加多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文