【发布时间】:2013-10-24 08:44:27
【问题描述】:
我正在尝试将从数据库获取的行添加到 DataTable。
首先我尝试使用 bucketdt.Rows.Add(r); 我收到一个错误提示 行属于另一个表
然后我用bucketdt.ImportRow(r); 然后它根本不复制行!
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
//DataTable f = new DataTable();
dosObject = new DataOperations();
bucketdt = new DataTable();
count=0;
foreach (string key in Session.Keys)
{
string val = Session[key].ToString();
DataRow r = bucketdt.NewRow();
r = dosObject.SubCategoryDetails2(Convert.ToInt16(val)).Rows[0];
bucketdt.ImportRow(r);
bucketdt.AcceptChanges();
}
GridView1.Enabled = true;
GridView1.DataSource = bucketdt;
GridView1.DataBind();
}
else
Response.Redirect(FormsAuthentication.LoginUrl);
我错过了什么吗?
现在我尝试按照@wonko79 的说明调整代码
DataTable f = new DataTable();
dosObject = new DataOperations();
bucketdt = new DataTable();
count=0;
foreach (string key in Session.Keys)
{
string val = Session[key].ToString();
DataRow r = bucketdt.NewRow();
f = dosObject.SubCategoryDetails2(Convert.ToInt16(val));
r["Id"]=f.Rows[0].ItemArray[0].ToString();
r["SubCategoryName"] = f.Rows[0].ItemArray[1].ToString();
r["Make"] = f.Rows[0].ItemArray[2].ToString();
r["Cost"] = f.Rows[0].ItemArray[3].ToString();
//bucketdt.ImportRow(r);
bucketdt.Rows.Add(r);
bucketdt.AcceptChanges();
n 现在它说该行不包含列:'Id' 不属于表。
【问题讨论】:
标签: c# asp.net datatable datarow