Reputation:
I want to add new row in Datagrid but have error.
ERROR: Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
row = new string[] { "0", "1", hesab_nomresi,soyad,ad,ataadi,vesiqe,teskilat_kodu,tevellud,nomre,cins };
kartsifarishiGridView.Rows.Add(elaveEtme.row);
AllowUserToAddRow = false;
Datasource: (added from comment of author)
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;
Upvotes: 1
Views: 2517
Reputation: 18031
Here is how to tackle this a generic way:
myDataGridView.BindingContext[dg.DataSource, dg.DataMember].AddNew();
Upvotes: 0
Reputation: 4315
You can add new row directly to the data source which you are binding to the grid and call kartsifarishiGridView.DataBind();
new string[] { "0", "1", hesab_nomresi,soyad,ad,ataadi,vesiqe,teskilat_kodu,tevellud,nomre,cins };
The idea is that you can't add a row to a binded datagridview. You'll have to add it to the structure that the datagridview is binded to (the datatable).
Example:
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;
Upvotes: 2