如何隐藏datagridview单元格值,但值和图像应保持c#

时间:2016-05-05 05:44:08

标签: c# .net datagridview

我想删除datagridview单元格值,但需要保留值和图像。我在cellPainting事件中绘制了该图像。请检查图像并告诉我有人实现这一目标。谢谢

Please check this image

  private void dgvMobileOperators_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {

            if (e.RowIndex >= 0 && e.ColumnIndex == 8 && Convert.ToInt32(e.Value.ToString()) == 1)
            {
                e.PaintBackground(e.ClipBounds, false);
                dgvMobileOperators[e.ColumnIndex, e.RowIndex].ToolTipText = e.Value.ToString();
                PointF p = e.CellBounds.Location;
                // p.X += imageList1.ImageSize.Width;
                p.X += 24;
                // string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"EasySMPP\App\Images\sms.ico");
                string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\Images\\connect1.png";
                e.Graphics.DrawImage(Image.FromFile(path), e.CellBounds.X, e.CellBounds.Y, 64, 16);
                e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, p);
                e.Handled = true;
            }
            else if (e.RowIndex >= 0 && e.ColumnIndex == 8 && Convert.ToInt32(e.Value.ToString()) == 0)
            {
                e.PaintBackground(e.ClipBounds, false);
                dgvMobileOperators[e.ColumnIndex, e.RowIndex].ToolTipText = e.Value.ToString();
                PointF p = e.CellBounds.Location;
                // p.X += imageList1.ImageSize.Width;
                p.X += 24;
                // string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"EasySMPP\App\Images\sms.ico");
                string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "\\Images\\disconnect.png";
                e.Graphics.DrawImage(Image.FromFile(path), e.CellBounds.X, e.CellBounds.Y, 64, 16);
                e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, p);
                e.Handled = true;
            }

2 个答案:

答案 0 :(得分:0)

选择状态列两次,如status1 status2,并隐藏status1列(具有原始值),status2列硬编码为空值,在c#中根据status1为status2列写入逻辑(显示图像)。 ...

答案 1 :(得分:0)

通常最简单的方法是将ForeColor的{​​{1}}设置为Column

Transparent

这样你就可以保持价值但不可见。

但是,既然你是dgvMobileOperators.Columns[8].DefaultCellStyle.ForeColor = Color.Transparent; dgvMobileOperators.Columns[8].DefaultCellStyle.SelectionForeColor = Color.Transparent; 个单元格,为什么不简单地跳过CellPainting来电?