首先在頁面中添加DataGridView容器,並設置好列信息。
在方法中使用如下語句進行行的添加
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Rows[0].Cells[0].Value = 2;
dataGridView1.Rows[0].Cells[1].Value = 3;
dataGridView1.Rows[0].Cells[2].Value = 4;
dataGridView1.Rows[0].Cells[3].Value = 5;
}
單元格背景色的設置
//設置單個單元格的背景色
dataGridView1.Rows[0].Cells[3].Style.BackColor = Color.Blue;
//索引0列的單元格的背景色為淡藍色
dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Aqua;
//索引0行的單元格的背景色為淡灰色
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.LightGray;
設置單元格的寬高
dataGridView1.Columns[0].Width = 13;
dataGridView1.Rows[0].Height = 8;