Multiple Rows in a DataGridView Cell

Using C# Windows Forms ;

I have a DataGridView with the number of cells. I would like to show the numbers (1 to 9) in the cell. Numbers should be located under each other in 3x3 format.

I looked around and just finished with a rather complicated custom implementation of the richtextbox cell.

Is there a way to draw a custom rectangle and then implement this as a backgroundimage cell or something else? This cell needs to be redrawn several times. Therefore, I can’t just name the paint event that I assume.

Note. The cell cannot be edited by the user.

+7
source share
1 answer

I don't know if this will satisfy you, but you can use Environment.NewLine to create a simple line break inside the cell.

Example:

 string nl = Environment.NewLine; // new line variable string data = "1 2 3" + nl + "4 5 6" + nl + "7 8 9"; 

Added later:

As Adrian said in the comments - you will need:

  1. set WrapMode for DataGridViewColumn to DataGridViewTriState.True

  2. make sure you set the height for the row or set the DataGridView AutoSizeRowsMode to DataGridViewAutoSizeRowsMode.AllCells

If you do not want to edit this column, you can set the DataGridView.Column.ReadOnly property to true .

Update: It took me a while to find this property with the above information. In VS C # 2017, WrapMode is in the Data View dialog box of DefaultCellSytle .

+18
source

Source: https://habr.com/ru/post/1480404/


All Articles