简体   繁体   English

无法将行动态添加到我的数据绑定dataGridview中

[英]Cannot add rows to my data-bound dataGridview dynamically

   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;

I'm getting data-bound error therefore I cannot add rows. 我收到数据绑定错误,因此无法添加行。 Any help would be great. 任何帮助都会很棒。 This is how I bound data to my dataGV: 这是我将数据绑定到dataGV的方式:

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

If the DataGridView is data bound you cannot add new rows/columns to the DataGridView directly. 如果DataGridView是数据绑定的,则不能直接向DataGridView添加新的行/列。 But you could add a row to a DataTable which will update your DataGridView automatically. 但是您可以在DataTable添加一行,这将自动更新DataGridView

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

Or you could add the new row to your source list and then reset the DataSource: 或者,您可以将新行添加到源列表中,然后重置DataSource:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 控件数据绑定时无法在DataGridView中以编程方式添加行 - Rows cannot be added programmatically in the DataGridView when the control data-bound XML加载后,DataGridView无法添加行,因为“控件绑定了数据” - DataGridView cannot add rows as “control is data-bound” after XML load 当控件是数据绑定的时,不能以编程方式将行添加到DataGridView的rows集合中 - Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound 错误“当控件绑定数据时,行无法以编程方式添加到datagridview的行集合中” - Error “Rows cannot be programatically added to datagridview's row collection when the control is data-bound” 当控件为数据绑定时,无法以编程方式将行添加到 datagridview 的行集合中 - Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound 当控件为数据绑定时,无法以编程方式添加到 DataGridView 的行集合中 - Cannot be programmatically added to the DataGridView's rows collection when the control is data-bound 带有Grid.Rows.Add方法的C#DataGridView数据绑定错误 - C# DataGridView data-bound error with Grid.Rows.Add method 当控件是集合数据源上的数据绑定错误时,不能以编程方式将行添加到DataGridView的rows集合中 - Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound error on collection data source 如何在数据绑定时以编程方式向 datagridview 添加一行? - How to programmatically add a row to a datagridview when it is data-bound? 更改数据绑定DataGridView中的显示信息 - Alter Displayed Information in data-bound DataGridView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM