如何在数据网格视图中隐藏特定的单元格值

本文关键字:隐藏 单元格 视图 数据 数据网 网格 | 更新日期: 2023-09-27 18:37:24

我有一个数据库,其中包含一个这样的表:

 id  | Description|  Rate    | ...
-----+------------+----------+------
 1   |  Product1  |  200     | ...
 2   |  Product2  |  200     | ...
 3   |  Product1  |  200     | ...
 ... |  ...       |  ...     | ...

现在我需要隐藏特定的单元格值,即 Product1如列说明

它应该像要在数据网格视图上显示的空值:

 id  | Description|  Rate    | ...
-----+------------+----------+------
 1   |  Product1  |  200     | ...
 2   |  Product2  |  200     | ...
 3   |            |  200     | ...
 ... |  ...       |  ...     | ...

如何在数据网格视图中隐藏特定的单元格值

您可以处理 DataGridView.CellPainting 事件来标识要自定义的单元格。

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
 // We are interested in handling the values in the "Description" column only..
 if (e.ColumnIndex == DescriptionDataGridViewTextBoxColumn.Index)
 {
     string description="something";
     if (e.Value != null)
     {
         if (e.Value.ToString()==description)
         {
             e.CellStyle.ForeColor = e.CellStyle.BackColor;
             e.CellStyle.SelectionForeColor = e.CellStyle.SelectionBackColor;
         }
     }
 }
}