How to add a new row to datagridview programmatically

前端 未结 18 2151
滥情空心
滥情空心 2020-11-22 09:40

if add row to DataTable

DataRow row = datatable1.NewRow();
row[\"column2\"]=\"column2\";
row[\"column6\"]=\"column6\";
datatable1.Rows.Add(row);         


        
18条回答
  •  臣服心动
    2020-11-22 09:53

    You can do:

    DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
    row.Cells[0].Value = "XYZ";
    row.Cells[1].Value = 50.2;
    yourDataGridView.Rows.Add(row);
    

    or:

    DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
    row.Cells["Column2"].Value = "XYZ";
    row.Cells["Column6"].Value = 50.2;
    yourDataGridView.Rows.Add(row);
    

    Another way:

    this.dataGridView1.Rows.Add("five", "six", "seven","eight");
    this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");
    

    From: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx

提交回复
热议问题