[英]How to set DataGridView textbox column to multi-line?
如何让“ DataGridViewTextBoxColumn
”中DataGridView
支持多行属性?
您应该能够通过将DataGridViewTextBoxColumn
的DefaultCellStyle
的WrapMode
设置为true
来实现这一点。
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 Haren mentioned, you need to set
WrapMode
of the DefaultCellStyle
of your DataGridViewTextBoxColumn
to true
.正如Tim S. Van Haren提到的,您需要将
DataGridViewTextBoxColumn
的DefaultCellStyle
的WrapMode
设置为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.除了
WrapMode
, DataGridView
的AutoSizeRowsMode
必须设置为适当的DataGridViewAutoSizeRowsMode
枚举值。 A value such as DataGridViewAutoSizeRowsMode.AllCells
allows the cell to expand vertically and show the entire wrapped text. DataGridViewAutoSizeRowsMode.AllCells
的值允许单元格垂直扩展并显示整个换行文本。
Apart from setting WrapMode
of the DefaultCellStyle
, you can do the following:除了设置
WrapMode
中的DefaultCellStyle
,你可以做到以下几点:
EditingControlShowing
EventEditingControlShowing
事件Control
property on the EventArgs to the type you want (ie textbox, checkbox, or button)Control
属性转换为您想要的类型(即文本框、复选框或按钮)Multiline
property like below:Multiline
属性,如下所示:private void MyGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox TB = (TextBox)e.Control;
TB.Multiline = true;
}
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;
}
如果您只想为DataGridView
一列设置 Multiline 属性,您可以这样做
dataGridView.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.