How can I manually add data to a dataGridView?

前端 未结 5 630
梦谈多话
梦谈多话 2020-12-16 19:23

I\'m trying to run this code, and I get an exception:

Index was out of range. Must be non-negative and less than the size of the collection. Param

5条回答
  •  一向
    一向 (楼主)
    2020-12-16 20:04

    You are just missing one line :-P

    DataGridViewRow row = new DataGridViewRow();
    row.CreateCells(dataGridView1);  // this line was missing
    row.Cells[0].Value = "Cell1";
    row.Cells[1].Value = "Cell2";
    dataGridView1.Rows.Add(row);
    

提交回复
热议问题