代码之家  ›  专栏  ›  技术社区  ›  Matt Frear

├图像列红色x

  •  5
  • Matt Frear  · 技术社区  · 17 年前

    我创建了一个DataGridView并添加了一个├ImageColumn(使用设计器)。

    DataGridView具有“AllowUserToAdd-ows=true”,因此会显示一个空白行供用户在其中输入新行。在该空白行中,DataGridView ImageColumn显示一个红色x。我希望无论该行是否有任何数据,该列中都始终显示相同的图像(我正在绑定单元格的单击事件,因此将DataGridView ImageColumn用作按钮)。

    如何去掉红色的x?

    6 回复  |  直到 17 年前
        1
  •  4
  •   Chris Doggett    17 年前

    我使用ImageList来保存我的所有图像,但这里有一个精简的版本应该会有所帮助。您设置值是正确的,但您应该将其设置为位图的null等效值,这只是一个静态的空白图像。我使用:

    e.Value = (imageIndex != int.MinValue) ? imageList.Images[imageIndex] : nullImage;
    

    private readonly Image nullImage = new Bitmap(1, 1);
    
        2
  •  2
  •   EdChum Arthur G    14 年前

    private void dgUserFileList_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) {
          DataGridViewRow row = dgUserFileList.Rows[e.RowIndex];
          if (row.IsNewRow) {
              row.Cells[0].Value = null;
            }
          }
        }
    
        3
  •  2
  •   Artemix    13 年前

    InitializeComponent()

    dataGridView1.Columns[0].DefaultCellStyle.NullValue = null;
    

    然后钩住 RowPrePaintEvent 删除网格末尾空行的红色X,因为 AllowUserToAddRows = true :

    private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
    {
          if (e.RowIndex >= numberOfRows)
          {
               dataGridView1.Rows[e.RowIndex].Cells[0].Value = null;
          }
    }
    

    numberOfRows 是网格中有效行的数量。刚刚花了几个小时处理这个问题。希望这能省去别人的头痛。..

        4
  •  1
  •   Matt Frear    17 年前

    private void dgvWorksheet_RowsAdded(object sender, 
         DataGridViewRowsAddedEventArgs e)
    {
        dgvWorksheet.Rows[e.RowIndex].Cells[colStartClock.Index].Value = null;
    }
    

    我还在Form_Load中将列的NullValue设置为null

    colStartClock.DefaultCellStyle.NullValue = null;
    

    我不确定是否还有其他需要做的事情。它似乎正在工作,但似乎有点问题——随机点击有时会导致异常,因此需要更多的调查。

        5
  •  1
  •   Ulf Kristiansen    11 年前

    这 MSDN documentation

    默认情况下,空单元格显示默认错误图形。防止 空值 或 DBNull.Value ,设置 DataGridViewCellStyle.NullValue DefaultCellStyle 空值 在向控件添加行之前。这不会影响行 然而,新的记录。防止错误图形出现在 控制时新记录的行 AllowUserToAddRows 财产 真 空值 在控件的处理程序中 RowsAdded 事件或设置列 CellTemplate DataGridViewImageCell DefaultNewRowValue 空值 .

    var column = new DataGridViewImageColumn();
    column.DefaultCellStyle.NullValue = null;
    column.CellTemplate = new DataGridViewEmptyImageCell();
    

    private class DataGridViewEmptyImageCell : DataGridViewImageCell
    {
        public override object DefaultNewRowValue { get { return null; } }
    }
    

    Cell.Value 在 Rows.Added 活动。

        6
  •  0
  •   Luan    11 年前

    dataGridView1.CellFormatting += delegate(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.RowIndex < 0 || e.ColumnIndex < 0)
            return;
    
        var grid = (DataGridView)sender;
        if (grid.Columns[e.ColumnIndex] is DataGridViewImageColumn &&
            grid[e.ColumnIndex, e.RowIndex].DefaultNewRowValue.Equals(e.Value))
            e.Value = null;
    };
    

    这样,组件将始终使用单元格的NullValue图像,而不是默认的“不可用图像”。

    推荐文章