我想在Datagrid中添加新行但是有错误。
错误:当控件受数据限制时,无法以编程方式将行添加到DataGridView的行集合中。
row = new string[] { "0", "1", hesab_nomresi,soyad,ad,ataadi,vesiqe,teskilat_kodu,tevellud,nomre,cins };
kartsifarishiGridView.Rows.Add(elaveEtme.row);
AllowUserToAddRow = false;
数据源(从作者评论中添加)
string sqlSorgu = "SELECT" + " customer.id, + " IIf (customer.cins = 'M','Kişi','Qadın') AS Cins " + " FROM customer ORDER BY customer.id ASC";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlSorgu, Program.esas.bazayaQosul);
DataTable dataTable = new DataTable();
set = new DataSet();
set.Tables.Add(dataTable);
dataAdapter.Fill(dataTable);
kartsifarishiGridView.DataSource = dataTable;
答案 0 :(得分:2)
您可以直接将新行添加到绑定到网格的数据源,并调用kartsifarishiGridView.DataBind();
new string[] { "0", "1", hesab_nomresi,soyad,ad,ataadi,vesiqe,teskilat_kodu,tevellud,nomre,cins };
这个想法是你不能在绑定的datagridview中添加一行。您必须将它添加到datagridview绑定到的结构(数据表)。
示例:强>
string sqlSorgu = "SELECT" + " customer.id," + " IIf (customer.cins = 'M','Kişi','Qadın') AS Cins " + " FROM customer ORDER BY customer.id ASC";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlSorgu, Program.esas.bazayaQosul);
DataTable dataTable = new DataTable();
set = new DataSet();
set.Tables.Add(dataTable);
dataAdapter.Fill(dataTable);
// Here is the code for adding new row
dataTable.Rows.Add(new string[]
{
"0", "1", hesab_nomresi, soyad, ad, ataadi, vesiqe, teskilat_kodu, tevellud, nomre,
cins
});
kartsifarishiGridView.DataSource = dataTable;
答案 1 :(得分:0)
以下是如何解决这个通用方法:
myDataGridView.BindingContext[dg.DataSource, dg.DataMember].AddNew();