温馨提示×

c# datagridview怎么添加一行数据

c#
小亿
340
2024-01-09 00:26:14
栏目: 编程语言

要在C#中的DataGridView中添加一行数据,可以使用以下代码:

// 创建一个新的行对象
DataGridViewRow row = new DataGridViewRow();

// 创建单元格对象
DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell cell3 = new DataGridViewTextBoxCell();

// 设置单元格的值
cell1.Value = "Value1";
cell2.Value = "Value2";
cell3.Value = "Value3";

// 将单元格添加到行中
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);

// 将行添加到DataGridView中
dataGridView.Rows.Add(row);

以上代码创建了一个新的行对象,并为每个单元格设置了值。然后将单元格添加到行中,最后将该行添加到DataGridView中。

请确保将dataGridView替换为你实际使用的DataGridView控件的名称,并根据需要设置单元格的值。

0