在Datagridview中删除整行

时间:2011-07-26 07:53:13

标签: c# winforms datagridview

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

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

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

我很感激任何帮助。提前谢谢。

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

如果其他一切都失败了,那我就去做吧。但是,有没有更简单的东西?

EDIT2:通过一些调整实现了Mark的建议。 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;
            }
        }
    }

2 个答案:

答案 0 :(得分:5)

如果您为datagridview_CellPainting创建了一个事件处理程序,那么DataGridViewCellPaintingEventArgs e就拥有了您需要的一切。

例如,您可以找到当前正在绘制的单元格的行/列(e.RowIndexe.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之类的内容。

答案 1 :(得分:5)

试试这个:

foreach(DataGridViewRow row in dgv.Rows)
                if(!string.IsNullOrEmpty(row.Cells["RemovedBy"].Value.ToString()))
                    row.DefaultCellStyle.Font = new Font(this.Font, FontStyle.Strikeout);
相关问题