問題描述
將現有表中的數據行添加到新表 (Adding a datarow from an existing table to a new table)
Hi i want to add a datarow that im getting back from a data table to a new datatable
this is the code im using:
foreach (DataRow dr1 in dt.Rows)
{
string AptType = dr1["AppointmentType"].ToString();
if (AptType == "FreeTime")
{
dt2.ImportRow(dr1);
}
}
RadGrid2.DataSource = dt2;
reader.Close();
conn.Close();
the problem is that when i then go to run the page with the table on it im getting a datakey error and that one of the columns isnt being recognised
thanks in advance
‑‑‑‑‑
參考解法
方法 1:
Do the two data tables have the same schema? Those errors might be thrown if they do not match columns, datatypes, or keys.
方法 2:
You should use Typed TableDataAdapters, I would make your life so much easier...
This is very easy to do and to understand.
Follow this tutorial Strongly Typed TableDataAdapters and DataTables
Once you grasp the concept, you should do something like this:
MyTypedTableAdapter tableAdapter = new MyTypedTableAdapter();
MyTypedDataTable dt = tableAdapter.GetData();
foreach (MyTypedDataRow row in dt.Rows)
{
string AptType = row.AppointmentType;
if (AptType == "FreeTime")
{
dt2.ImportRow(row);
}
}
RadGrid2.DataSource = dt2;
(by kevinw、Matthew Jones、maxbeaudoin)