C# DataGridView容器手动添加数据与单元格样式设置

首先在页面中添加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;
posted @ 2020-11-07 15:03  Mirage_c  阅读(1086)  评论(2)    收藏  举报