C# 如何将 DataGridView 文本框列设置为多行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1559867/
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
How to set DataGridView textbox column to multi-line?
提问by Wahid Bitar
How to let "DataGridViewTextBoxColumn
" in DataGridView
supports Multiline property?
如何让“ DataGridViewTextBoxColumn
”DataGridView
支持多行属性?
采纳答案by Tim S. Van Haren
You should be able to achieve this by setting the WrapMode
of the DefaultCellStyle
of your DataGridViewTextBoxColumn
to true
.
您应该能够通过设置来实现这一目标WrapMode
的DefaultCellStyle
您的DataGridViewTextBoxColumn
到true
。
回答by usman Majeed
Apart from setting WrapMode
of the DefaultCellStyle
, you can do the following:
除了设置WrapMode
的DefaultCellStyle
,你可以做到以下几点:
- You need to catch GridView's
EditingControlShowing
Event - Cast
Control
property on the EventArgs to the type you want (i.e. textbox, checkbox, or button) - Using that casted type, change the
Multiline
property like below:
- 您需要捕获 GridView 的
EditingControlShowing
事件 - 铸造
Control
于EventArgs的到你想要的类型属性(即文本框,复选框或按钮) - 使用该强制类型,更改如下
Multiline
属性:
private void MyGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox TB = (TextBox)e.Control;
TB.Multiline = true;
}
回答by Tom Faust
I have found that there are two things that you need to do, both in the designer, to make a text cell show multiple lines. As Tim S. Van Harenmentioned, you need to set WrapMode
of the DefaultCellStyle
of your DataGridViewTextBoxColumn
to true
. And although that does make the text wrap, it doesn't make the row expand to show anything beyond the first line. In addition to WrapMode
, the AutoSizeRowsMode
of the DataGridView
must be set to the appropriate DataGridViewAutoSizeRowsMode
enumeration value. A value such as DataGridViewAutoSizeRowsMode.AllCells
allows the cell to expand vertically and show the entire wrapped text.
我发现您需要在设计器中做两件事才能使文本单元格显示多行。作为添S.范·哈伦提到的,你需要设置WrapMode
的DefaultCellStyle
你的DataGridViewTextBoxColumn
来true
。尽管这确实使文本换行,但它不会使行扩展以显示第一行以外的任何内容。此外WrapMode
,该AutoSizeRowsMode
的DataGridView
必须设置相应的DataGridViewAutoSizeRowsMode
枚举值。诸如DataGridViewAutoSizeRowsMode.AllCells
允许单元格垂直扩展并显示整个换行文本的值。
回答by Pavan M
int multilineht = 0;
private void CustGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
multilineht = CustGridView.Rows[CustGridView.CurrentCell.RowIndex].Height;
CustGridView.AutoResizeRow(CustGridView.CurrentCell.RowIndex, DataGridViewAutoSizeRowMode.AllCells);
}
private void CustGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
CustGridView.Rows[CustGridView.CurrentCell.RowIndex].Height = multilineht;
}