怎么向C#中的datagriview逐条添加数据

2025-01-07 14:37:12
推荐回答(2个)
回答1:

DataGridViewRow row = new DataGridViewRow(); //新建一个DataGridViewRow
foreach (DataGridViewColumn c in dataGridView1.Columns)
{
row.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);//给行添加单元格
}
row.Cells[0].Value = reader.GetString(0); //给DataGridViewRow添加数据
row.Cells[1].Value = reader.GetString(1); //给DataGridViewRow添加数据
row.Cells[2].Value = reader.GetString(2); //给DataGridViewRow添加数据
dataGridView1.Rows.Add(row); //添加进DataGridView

回答2:

将数据放在DataTable中,然后再绑定