在 Datagridview 中删除整行

2024-03-06

我正在尝试删除 Datagridview 中的整行。这就是我目前正在做的事情:

 DataGridViewCellStyle style = new DataGridViewCellStyle();
 style.Font = new Font(dgview.Font.OriginalFontName, 7, FontStyle.Strikeout);              
 dgview.Rows[dgview.RowCount - 1].DefaultCellStyle.ApplyStyle(style);

这种方法只会删除单元格中包含任何文本的部分。我想要的是连续的三振,即一条穿过该行的线。

我将不胜感激任何帮助。提前致谢。

编辑:在另一个问题中将此视为可能的答案-“如果所有行都具有相同的高度,那么可能最简单的方法就是应用一个背景图像,该背景图像只有一条穿过中心的大线,颜色相同作为测试。”

如果其他一切都失败了,那么我会选择这个。但难道就没有更简单的事情了吗?

EDIT2:通过一些调整实现了马克的建议。 cellbound 属性对我来说无法正常工作,因此我决定使用 rowindex 和 rowheight 来获取位置。

  private void dgv_CellPainting(object sender,DataGridViewCellPaintingEventArgs e)
    {
        if (e.RowIndex != -1)
        {
            if (dgv.Rows[e.RowIndex].Cells["Strikeout"].Value.ToString() == "Y")
            {
                e.Paint(e.CellBounds, e.PaintParts);
                e.Graphics.DrawLine(new Pen(Color.Red, 2), new Point(e.CellBounds.Left, gridHeaderHeight+ e.RowIndex * rowHeight+ rowHeight/2), 
                    new Point(e.CellBounds.Right, gridHeaderHeight+ e.RowIndex * rowHeight+ rowHeight/2));
                e.Handled = true;
            }
        }
    }

如果您创建一个事件处理程序datagridview_CellPainting, then DataGridViewCellPaintingEventArgs e有您需要的一切。

例如,您可以找出当前正在绘制的单元格的行/列(e.RowIndex, e.ColumnIndex).

因此,您可以使用它来确定当前单元格是否是您要修改的单元格。如果是的话,您可以尝试以下操作:

e.Paint(e.CellBounds, e.PaintParts);  // This will paint the cell for you
e.Graphics.DrawLine(new Pen(Color.Blue, 5), new Point(e.CellBounds.Left, e.CellBounds.Top), new Point(e.CellBounds.Right, e.CellBounds.Bottom));
e.Handled = true;

这将绘制一条粗的蓝色对角线,但您明白了... e.CellBounds 也具有高度/宽度,因此您可以轻松计算中间值来绘制线条。

您还可以更改诸如e.CellStyle.BackColor如果你想要的不仅仅是一条线。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Datagridview 中删除整行 的相关文章

随机推荐