DataGridView 单元格中的多行 [英] Multiple lines in a DataGridView cell
问题描述
使用C#
Windows Forms
;
我有一个带有多个单元格的 DataGridView
.我想在单元格中显示数字(从 1-9).数字应以 3x3
格式放在一起.
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.
我环顾四周,最终得到了一个相当复杂的 richtextbox
单元的自定义实现.
I looked around, and only ended up with a rather complex custom implementation of a richtextbox
cell.
有没有办法绘制一个自定义矩形
,然后将其实现为单元格的backgroundimage
或其他什么?单元格需要多次重绘.所以我猜我不能只调用 paint
事件.
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.
注意:用户不得编辑单元格.
推荐答案
我不知道这是否会让您满意,但是您可以使用 Environment.NewLine
在单元格内创建简单的换行符.
Im don't know if this will satisfy you, but you can use Environment.NewLine
to create simple line break inside cell.
示例:
string nl = Environment.NewLine; // new line variable
string data = "1 2 3" + nl + "4 5 6" + nl + "7 8 9";
稍后添加:
正如 Adrian 在评论中所说 - 您需要:
As Adrian said in comments - you will need to:
将
DataGridViewColumn
的 WrapMode 设置为DataGridViewTriState.True
set the WrapMode for the
DataGridViewColumn
toDataGridViewTriState.True
确保为行设置了高度,或者将 DataGridView 的 AutoSizeRowsMode
设置为 DataGridViewAutoSizeRowsMode.AllCells
make sure you set a height for the row, or set the DataGridView's AutoSizeRowsMode
to DataGridViewAutoSizeRowsMode.AllCells
如果您不想编辑该列 - 您可以将 DataGridView.Column.ReadOnly
属性设置为 true
.
If you don't want to edit that column - you can set DataGridView.Column.ReadOnly
property to true
.
更新:我花了一段时间才找到带有上述信息的属性.在 VS C# 2017 中,WrapMode 属性位于 datagridview DefaultCellSytle
对话框中.
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.
这篇关于DataGridView 单元格中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!