简体   繁体   English

如何将 DataGridView 文本框列设置为多行?

[英]How to set DataGridView textbox column to multi-line?

如何让“ DataGridViewTextBoxColumn ”中DataGridView支持多行属性

您应该能够通过将DataGridViewTextBoxColumnDefaultCellStyleWrapMode设置为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提到的,您需要将DataGridViewTextBoxColumnDefaultCellStyleWrapMode设置为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.除了WrapModeDataGridViewAutoSizeRowsMode必须设置为适当的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 ,你可以做到以下几点:

  1. You need to catch GridView's EditingControlShowing Event你需要捕捉 GridView 的EditingControlShowing事件
  2. Cast Control property on the EventArgs to the type you want (ie textbox, checkbox, or button)将 EventArgs 上的Control属性转换为您想要的类型(即文本框、复选框或按钮)
  3. Using that casted type, change the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM