无法动态地向我的数据绑定dataGridview添加行

时间:2015-07-07 07:30:50

标签: c# winforms datagridview row

   RDP0 yeni = new RDP0();
        yeni.IPADDRESS = "1.1.1.1";
        yeni.PASSWORD = " 10akh0";
        yeni.NO = 8;
        yeni.CUSTID = 1000;
        testList.Add(yeni);
        dataGridView1.DataSource = testList;

我收到了数据绑定错误,因此我无法添加行。任何帮助都会很棒。 这就是我将数据绑定到dataGV的方式:

        tm = new testModel();
        var query = from fe in tm.ERDP0 select fe;
        testList = query.ToList();
        dataGridView1.DataSource = testList;

1 个答案:

答案 0 :(得分:1)

如果DataGridView是数据绑定,则无法直接向DataGridView添加新行/列。但您可以向DataTable添加一行,这会自动更新您的DataGridView

DataTable dt = dataGridView1.DataSource as DataTable;
dt.Rows.Add("new row");

或者您可以将新行添加到源列表,然后重置DataSource:

testList.Add(youritem);
dataGridView1.DataSource = null;
dataGridView1.DataSource = testList;