这是我刚刚尝试过的演示。它似乎工作正常。整个想法是你不能隐藏 DataGridView 中的特定单元格。但是,您可以将其隐藏,因为当它想要隐藏任何控件/元素时将使用普通的 GUI 引擎(我认为是这样)。您只需自定义它以使用BackgroundColor
您的DataGridView
. 当然,要让它发挥作用,并不是那么容易。这是给你的代码:
//First, you have to be sure the whole third column is Visible.
//CellPainting event handler for your dataGridView1
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 2)//This is the Column index you want to hide.
{
object o = e.RowIndex == -1 ? null : dataGridView1[e.ColumnIndex - 1,e.RowIndex].Value;
if (o!=null &&!(bool)o || e.RowIndex == -1 || e.RowIndex == dataGridView1.RowCount - 1)
{
e.Graphics.FillRectangle(new SolidBrush(dataGridView1.BackgroundColor), e.CellBounds);
if(e.RowIndex > -1) dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly = true;
e.Handled = true;
}
if (o != null && (bool)o)
{
dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly = false;
}
}
}
//CellContentClick event handler for your dataGridView1
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
UpdateThirdColumCell(e);
}
//CellContentDoubleClick event handler for your dataGridView1
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
UpdateThirdColumCell(e);
}
private void UpdateThirdColumCell(DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)//The column index of the CheckBox column
{
DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
cell.Value = cell.EditingCellFormattedValue;
dataGridView1.Invalidate();
if ((bool)cell.Value)
{
dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex + 1, e.RowIndex];
}
}
}
//CellStateChanged event handler for your dataGridView1
private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.Cell.ColumnIndex == 2 && e.Cell.Selected)
{
dataGridView1.BeginEdit(false);
}
}
就这样 :)