1

我想在 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;
4

2 回答 2

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;
于 2012-06-09T14:52:17.193 回答
0

以下是如何以通用方式解决此问题:

myDataGridView.BindingContext[dg.DataSource, dg.DataMember].AddNew();
于 2015-11-12T08:50:35.937 回答