Strikeout entire row in Datagridview
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开发者_JAVA百科 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
, then DataGridViewCellPaintingEventArgs e
has 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:
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;
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.BackColor
if you want more than just a line.
Try this:
foreach(DataGridViewRow row in dgv.Rows)
if(!string.IsNullOrEmpty(row.Cells["RemovedBy"].Value.ToString()))
row.DefaultCellStyle.Font = new Font(this.Font, FontStyle.Strikeout);
精彩评论