Strikeout an entire row in a datagridview

I am trying to cross out a whole line in a Datagridview. This is what I am currently doing:

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 removes part of the cells that have any text in them. I would like to have a continuous out, that is, one line that goes through the line.

I would appreciate any help with this. Thanks in advance.

EDIT: That was like the likely answer in another question: "Probably the easiest way to do this if all the lines are the same height is to apply a background image to it that just has a large line through the center, the same color as the test" .

If all else fails, I will go with that. But is there nothing simpler?

EDIT2: Mark proposal has been implemented with a small number of settings. The cellbound property does not work properly for me, so I decided to get the location using 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; } } } 
+4
source share
2 answers

Try to handle the cell event.

Then you can check that this is the line you want and draw a line through it yourself.

EDIT:

If you are creating an event handler for datagridview_CellPainting , then DataGridViewCellPaintingEventArgs e has everything you need.

For example, you can find out the row / column of the current cell ( e.RowIndex , e.ColumnIndex ).

So you can use this to determine if the current cell is the one you want to change. If so, 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 a 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 string.

+5
source

Try the following:

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

All Articles