C# DataGridView 单元格中的多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16514352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:12:00  来源:igfitidea点击:

Multiple lines in a DataGridView cell

c#winformsdatagridview

提问by dylanmensaert

Using C#Windows Forms;

使用C#Windows Forms;

I have a DataGridViewwith 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 3x3format.

我有DataGridView多个单元格。我想在单元格中显示数字(从 1-9)。数字应以某种3x3格式放在彼此的下方。

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

我环顾四周,最终得到了一个相当复杂的richtextbox单元格自定义实现。

Is there a way I can draw a custom rectangleand then implement this as backgroundimageof the cell or something? The cell need to be redrawn several times. So I can't just call the paintevent I guess.

有没有办法可以绘制一个custom rectangle然后backgroundimage在单元格或其他东西上实现它?单元格需要多次重绘。所以我不能只调用paint我猜的事件。

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

注意:用户不得编辑单元格。

采纳答案by Kamil

Im don't know if this will satisfy you, but you can use Environment.NewLineto create simple line break inside cell.

我不知道这是否会让您满意,但您可以使用Environment.NewLine它在单元格内创建简单的换行符。

Example:

例子:

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

Added later:

稍后补充:

As Adriansaid in comments - you will need to:

正如阿德里安在评论中所说 - 您需要:

  1. set the WrapMode for the DataGridViewColumnto DataGridViewTriState.True

  2. make sure you set a height for the row, or set the DataGridView's AutoSizeRowsModeto DataGridViewAutoSizeRowsMode.AllCells

  1. 将 WrapMode 设置DataGridViewColumnDataGridViewTriState.True

  2. 确保为行设置高度,或将 DataGridView 设置AutoSizeRowsModeDataGridViewAutoSizeRowsMode.AllCells

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

如果您不想编辑该列 - 您可以将DataGridView.Column.ReadOnly属性设置为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 DefaultCellSytledialog.

更新:我花了一段时间才找到带有上述信息的属性。在 VS C# 2017 中,WrapMode 属性位于 datagridviewDefaultCellSytle对话框中。