如何在datagridview中隐藏特定的单元格值

时间:2013-10-30 12:10:20

标签: c# windows winforms datagridview c#-3.0

我有数据库,其中包含如下表格:

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

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

应该像在datagridview上显示空值一样:

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

1 个答案:

答案 0 :(得分:3)

您可以处理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;
         }
     }
 }
}