15

Using C# Windows Forms;

I have a DataGridView with a number of cells. I would like to show digits (from 1-9) in the cell. The digits should be placed under each other in a 3x3 format.

I looked around, and only ended up with a rather complex custom implementation of a richtextbox cell.

Is there a way I can draw a custom rectangle and then implement this as backgroundimage of the cell or something? The cell need to be redrawn several times. So I can't just call the paint event I guess.

Note: The cell must not be edited by the user.

Kamil
  • 13,363
  • 24
  • 88
  • 183
dylanmensaert
  • 1,689
  • 5
  • 24
  • 39
  • Does this answer your question? [How to set DataGridView textbox column to multi-line?](https://stackoverflow.com/questions/1559867/how-to-set-datagridview-textbox-column-to-multi-line) – rold2007 Dec 08 '21 at 08:13

3 Answers3

27

I don't know if this will satisfy you, but you can use Environment.NewLine to create simple line break inside 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 comments - you will need to:

  1. Set the WrapMode for the DataGridViewColumn to DataGridViewTriState.True

  2. Make sure you set a height for the row, or set the DataGridView's AutoSizeRowsMode to DataGridViewAutoSizeRowsMode.AllCells

If you don't want to edit that column - you can set DataGridView.Column.ReadOnly property to true.

Update: It took me a while to find this property with the above information. In VS C# 2017 the WrapMode property is located in the datagridview DefaultCellStyle dialog.

awdorrin
  • 85
  • 1
  • 6
Kamil
  • 13,363
  • 24
  • 88
  • 183
  • already did this. Problem is that cell is a textbox not rich textbox. So everything is situated on 1 line, regardless of the given string :/ – dylanmensaert May 13 '13 at 03:24
  • 5
    In addition to this you need to set the `WrapMode` for the `DataGridViewColumn` to `DataGridViewTriState.True` and make sure you set a height for the row, or set the `DataGridView`'s `AutoSizeRowsMode` to `DataGridViewAutoSizeRowsMode.AllCells` – Adrian May 13 '13 at 03:30
  • @user1933169 i added information how to disable edit on column. If you need more details - just ask in comments. – Kamil May 13 '13 at 03:42
4

as a supplement:

dataGridView.Columns[x].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

x = Column-Index

Wolfgang Schorge
  • 157
  • 1
  • 10
2

Right Click on GridView >> properties>>DefaultCellStyle>>Wrapmode=True