C# DataGridViewCheckBoxColumn 隐藏/灰显

发布于 2024-12-08 11:35:28 字数 145 浏览 1 评论 0原文

我有一个包含几列和几行数据的 DataGridView。其中一列是 DataGridViewCheckBoxColumn ,并且(基于行中的其他数据)我希望选择“隐藏”某些行中的复选框。我知道如何使其只读,但我希望它根本不显示,或者至少与其他复选框显示不同(变灰)。这可能吗?

I have a DataGridView with several columns and several rows of data. One of the columns is a DataGridViewCheckBoxColumn and (based on the other data in the row) I would like the option to "hide" the checkbox in some rows. I know how to make it read only but I would prefer it to not show up at all or at least display differently (grayed out) than the other checkboxes. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美煞众生 2024-12-15 11:35:28

一些解决方法:将其设为只读并将颜色更改回灰色。
对于一个特定的单元格:

dataGridView1.Rows[2].Cells[1].Style.BackColor =  Color.LightGray;
dataGridView1.Rows[2].Cells[1].ReadOnly = true;

或者,更好但更“复杂”的解决方案:
假设您有 2 列:第一列包含数字,第二列包含复选框,当 number > 时,该列不应可见。 2. 您可以处理 CellPainting 事件,仅绘制边框(例如背景)并中断其余绘制。为 DataGridView 添加事件 CellPainting (可以选择测试 DBNull 值以避免在空行中添加新数据时出现异常):

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    //check only for cells of second column, except header
    if ((e.ColumnIndex == 1) && (e.RowIndex > -1))
    {
        //make sure not a null value
        if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != DBNull.Value)
        {
            //put condition when not to paint checkbox
            if (Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value) > 2)
            {
                e.Paint(e.ClipBounds, DataGridViewPaintParts.Border | DataGridViewPaintParts.Background);  //put what to draw
                e.Handled = true;   //skip rest of painting event
            }
        }
    }
}

它应该可以工作,但是如果您在检查条件的第一列中手动更改值,则必须刷新第二个单元格,因此添加另一个事件,例如 CellValueChanged

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        dataGridView1.InvalidateCell(1, e.RowIndex);
    }
}

Some workaround: make it read-only and change back color to gray.
For one specific cell:

dataGridView1.Rows[2].Cells[1].Style.BackColor =  Color.LightGray;
dataGridView1.Rows[2].Cells[1].ReadOnly = true;

Or, better but more "complicated" solution:
suppose you have 2 columns: first with number, second with checkbox, that should not be visible when number > 2. You can handle CellPainting event, paint only borders (and eg. background) and break painting of rest. Add event CellPainting for DataGridView (optionally test for DBNull value to avoid exception when adding new data in empty row):

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    //check only for cells of second column, except header
    if ((e.ColumnIndex == 1) && (e.RowIndex > -1))
    {
        //make sure not a null value
        if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != DBNull.Value)
        {
            //put condition when not to paint checkbox
            if (Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value) > 2)
            {
                e.Paint(e.ClipBounds, DataGridViewPaintParts.Border | DataGridViewPaintParts.Background);  //put what to draw
                e.Handled = true;   //skip rest of painting event
            }
        }
    }
}

It should work, however if you change value manually in first column, where you check condition, you must refresh the second cell, so add another event like CellValueChanged:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        dataGridView1.InvalidateCell(1, e.RowIndex);
    }
}
月光色 2024-12-15 11:35:28

http://msdn.microsoft.com/en-us /library/system.windows.forms.datagridviewcheckboxcell.aspx

DataGridViewCheckBoxCell.Visible = false;

编辑:哦,等等,它是只读的。德普。

在这种情况下,请尝试用空的 DataGridViewTextBoxCell 替换单元格。

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcell.aspx

DataGridViewCheckBoxCell.Visible = false;

Edit: Oh, wait, it's read only. Derp.

In which case, try replacing the cell with an empty DataGridViewTextBoxCell.

三人与歌 2024-12-15 11:35:28

摘自自定义 Windows 窗体 DataGridView 控件中单元格的外观,您如果单元格处于只读模式,则可以捕获 CellPainting 事件并且不绘制单元格。例如:

public Form1()
{
   InitializeComponent();
   dataGridView1.CellPainting += new 
      DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}

private void dataGridView1_CellPainting(object sender,
   System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
   // Change 2 to be your checkbox column #
   if (this.dataGridView1.Columns[2].Index == e.ColumnIndex && e.RowIndex >= 0)
   {
      // If its read only, dont draw it
      if (dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly)
      {
         // You can change e.CellStyle.BackColor to Color.Gray for example
         using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
         {
            // Erase the cell.
            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
            e.Handled = true;
         }
      }
   }
}

唯一需要注意的是,当您更改 DataGridViewCheckBox 之一的 ReadOnly 属性时,您需要调用 dataGridView1.Invalidate();细胞的。

Taken from Customize the Appearance of Cells in the Windows Forms DataGridView Control, you could catch the CellPainting event and not draw the cell if its in read only mode. For example:

public Form1()
{
   InitializeComponent();
   dataGridView1.CellPainting += new 
      DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}

private void dataGridView1_CellPainting(object sender,
   System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
   // Change 2 to be your checkbox column #
   if (this.dataGridView1.Columns[2].Index == e.ColumnIndex && e.RowIndex >= 0)
   {
      // If its read only, dont draw it
      if (dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly)
      {
         // You can change e.CellStyle.BackColor to Color.Gray for example
         using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
         {
            // Erase the cell.
            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
            e.Handled = true;
         }
      }
   }
}

The only caveat is that you need to call dataGridView1.Invalidate(); when you change the ReadOnly property of one of the DataGridViewCheckBox cell's.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文