I have a datatable in windows form and I want to add a new row to the table. I always receive the error
Table doesn’t have a primary key.
The question is that I don’t want a primary key at all. It may have duplicated “ID” in the table.
My code:
using (DataTable dt = new DataTable())
{
dt.Columns.Add("EntryDE", typeof(string));
dt.Columns.Add("Descr", typeof(string));
dt.Columns.Add("Id", typeof(int));
foreach (DataGridViewRow row in dgv.Rows)
{
DataGridViewCheckBoxCell check = row.Cells[0] as DataGridViewCheckBoxCell;
if (check.Value != null)
{
if ((bool)check.Value)
{
//this row has a checkBox set to true (tick is added)
//add this row to dataTable ...
DataRow myRow = (row.DataBoundItem as DataRowView).Row;
DataRow dr = dt.NewRow();
dr["EntryDE"] = myRow["ID"].ToString();
dr["Descr"] = myRow["EntryName"];
dr["Id"] = Id;
dt.Rows.Add(dr);
}
}
}
Thanks for advice.
The error isn’t coming from the DataTable because this works:
You’ve got some other problem.