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

后端 未结 6 957
囚心锁ツ
囚心锁ツ 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:41

    The Bound Datagridview has a problem that when you want to add your data programmatically it prevent it to add it directly. so the indirect and best way to add data is like this.. and remember never add data directly to datagridview programmatically because it create problem always, add data to your datasource instead :-)

    code for VB.NET
    Dim r As DataRow ( C# : Datarow r=new Datarow() below codes apply to C# also)
    r = dataset.Tables(0).NewRow
    r.Item("field1") = "2"
    r.Item("field2") = "somevalue"
    dataset.Tables(0).Rows.Add(r)
    
    dataset.Tables(0).acceptchanges()
    
    the update will goes as you do ever
    

提交回复
热议问题