DataGridView
目标:实现如下类似效果
核心代码:
dataGridView1.Rows[row].Cells[col].Style.Padding = new Padding(dataGridView1.Rows[row].Cells[col].OwningColumn.Width, 0, 0, 0); dataGridView1.Rows[row].Cells[col].Style.BackColor = Color.Gray;
一个完整的实例:
参考https://stackoverflow.com/questions/14124033/hide-some-datagridview-checkbox-cell
代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace TestDGV { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void AddRow_Click(object sender, EventArgs e) { this.dataGridView1.Rows.Add(); } private void AddColumn_Click(object sender, EventArgs e) { string colInfo = this.ColumnName.Text; if(string.IsNullOrEmpty(colInfo)) { MessageBox.Show("Column Info Empty."); return; } if(dataGridView1.Columns.Contains(colInfo)) { MessageBox.Show("Column Name Exist."); return; } var c = new DataGridViewTextBoxColumn(); c.HeaderText = colInfo; c.Name = colInfo; dataGridView1.Columns.Add(c); } private void GreyCell_Click(object sender, EventArgs e) { int row; if(!Int32.TryParse(this.RowIndex.Text,out row)) return; int col; if (!Int32.TryParse(this.ColumnIndex.Text, out col)) return; if (row >= this.dataGridView1.Rows.Count) return; if (col >= this.dataGridView1.Columns.Count) return; dataGridView1.Rows[row].Cells[col].Style.Padding = new Padding(dataGridView1.Rows[row].Cells[col].OwningColumn.Width, 0, 0, 0); dataGridView1.Rows[row].Cells[col].Style.BackColor = Color.Gray; } } }