简体   繁体   中英

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:

        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. But you could add a row to a DataTable which will update your DataGridView automatically.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM