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;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM