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