Search code examples
vb.netdatagridviewsplitcell

Split cells in datagridview vb.NET. Is that possible?


I need to represent different 4 (max, maybe 2 or 3) colors to the user in datagridview control @ VB.NET. An example cell would be:


---------------
| Blue | Red  |
---------------    <<<- A Cell
| Gray | Blue |
---------------

So,

i need to split a cell to different size or counts. I know merging cells is possible, is opposite-merge possible? Or anybody have any idea for this problem?

Thank you in advance.


Solution

  • I can think of three solutions:


    Have extra columns and rows so you can merge them into any pattern you'd expect.


    Handle the DataGridView.RowPrePaint event. Record the row index for use in the following event.

    Handle the DataGridView.CellPainting event. You can get the column index, cell bounds, and graphics handler from the event args (e). Call e.Graphics.FillRectange()


    Create a custom class inheriting from System.Windows.Forms.DataGridViewCell. Add properties to define the cell splitting and colours. Write your custom painting in the Paint event. Populate the DataGridView with these cell types.