Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound

后端 未结 6 965
囚心锁ツ
囚心锁ツ 2020-11-29 10:02

First of all, I looked up this related question in here but the solution dataGridView1.Rows.Add() doesn\'t work in my case.

In my Datagridview, I have

6条回答
  •  孤城傲影
    2020-11-29 10:38

    When I write this line in Form_Load

    DGVData.DataSource = DataSQLite.GetData("SELECT * from tableName");
    

    And trying to add a new row this error appeared to me

    Instead of using DataSource try this code

     private void Form_Load(object sender, EventArgs e) {
            DataSQLite.OpenDB();      // this line to open connection to Database and DataSQLite class i created it
            DataTable tbl = DataSQLite.GetData("SELECT * from tableName");
            for (int i = 0; i < tbl.Rows.Count; i++) {
                DGVData.Rows.Add();  // this line will create new blank row
                for (int j = 0; j < Numberofcolumns; j++) {
                    DGVData.Rows[i].Cells[j].Value = tbl.Rows[i][j].ToString();
                }
            }
        }
    

    After this code you can easily add rows

提交回复
热议问题