在Datagridview中删除整行

Arc*_*rus 4 c# datagridview winforms

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

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

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

我很感激你的帮助.提前致谢.

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

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

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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Mar*_*ark 5

如果您为其创建事件处理程序datagridview_CellPainting,则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;
Run Code Online (Sandbox Code Playgroud)

这将绘制一条粗蓝色对角线,但你明白了...... e.CellBounds也有高度/宽度,所以你可以很容易地计算中间线来画线.

e.CellStyle.BackColor如果你想要的不仅仅是一条线,你也可以改变一些事情.

  • @Arcturus Mark给了你一个很好的提示.如果你只搜索了一下或者看看msdn,你会自己找到附加信息. (3认同)

PUG*_*PUG 5

试试这个:

foreach(DataGridViewRow row in dgv.Rows)
                if(!string.IsNullOrEmpty(row.Cells["RemovedBy"].Value.ToString()))
                    row.DefaultCellStyle.Font = new Font(this.Font, FontStyle.Strikeout);
Run Code Online (Sandbox Code Playgroud)