Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

c# - Multiple lines in a DataGridView cell

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Im 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 DefaultCellSytle dialog.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...