I’m trying to strike out entire row in Datagridview. This is what I’m doing currently:
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Font = new Font(dgview.Font.OriginalFontName, 7, FontStyle.Strikeout);
dgview.Rows[dgview.RowCount - 1].DefaultCellStyle.ApplyStyle(style);
This approach only strikes out the part of cells that have any text in them. What I’d like is to have a continuous strikeout i.e a single line that runs across the row.
I’d appreciate any help on this. Thanks in advance.
EDIT: Saw this as probable answer in another question- “Probably the easiest way to do this, if all the rows are the same height, is to apply a background image to it that just has a big line through the center, the same color as the test.”
If everything else fails then I’d go with this. But isn’t there anything more simple?
EDIT2: Implemented Mark’s suggestion with a bit of tweaking. The cellbound property wasn’t working properly for me so I decided to get the location by using the rowindex and 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;
}
}
}
If you create an event handler for
datagridview_CellPainting, thenDataGridViewCellPaintingEventArgs ehas everything you need.For example, you can find out the row/column of the cell currently being painted (
e.RowIndex,e.ColumnIndex).So you can use this to determine if the current cell is the one you want to modify. If it is, you can try the following:
This will draw a thick blue diagonal line, but you get the idea… e.CellBounds also has Height/Width so you can easily calculate the middle to draw your line.
You can also change things like
e.CellStyle.BackColorif you want more than just a line.