Cellpainting checkbox or paint part of the cell

别等时光非礼了梦想. 提交于 2019-12-11 10:14:36

问题


I have this situation with DataGridView in Winforms. I want to achive something like this, with some separation between rows

The image and address N39 are actually on 2 columns, with no horizontal margin and no column grid line to make it look like they belong to a whole. To achieve sepration between rows, I override CellPainting and repaint the whole grid, using from Graphics.FillRectangle (with height less than cell height), Graphics.DrawLine to Graphics.DrawString and Graphics.DrawText to draw cell contents.

In the second scenario, I have checkbox as one of the columns.

If I don't repaint, the cell will touch the white grid lines and leave no separation between rows.

However if I want to override CellPainting to create some separation like above, I may need to repaint the whole content but I am not aware of any method to draw and handle checkbox. So I was thinking if possible, I would just draw an enclosing rectangle with background color (light gray) to the cells and leave the content for Winform for handle.

My questions are:

  1. How can I repaint only part of the cell, e.g. draw an enclosing rectangle, and let the remaining cell content for Winforms to handle?

  2. If we have no choice but to repaint the whole cell, how do you repaint and handle checkbox column?

  3. Any other suggestion are welcome.

Thanks a lot in advance.

Best.


回答1:


There are two handy methods in the rich set of parameters in the ubiquous e of the CellPainting event:

e.PaintContent and e.PaintBackground do all the work if you want to change only a part of the looks:

if (e.ColumnIndex == yourColumnIndex)
{
    // do your special stuff..
    e.Graphics.FillRectangle(Brushes.Wheat, e.CellBounds);
    // ..
    // now get the regular content drawn by the system
    e.PaintContent(e.CellBounds);  
    // and quit
    e.Handled = true;
}


来源:https://stackoverflow.com/questions/26253110/cellpainting-checkbox-or-paint-part-of-the-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!