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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:51:55  来源:igfitidea点击:

How to set DataGridView textbox column to multi-line?

c#.netdatagridviewdatagridviewcolumn

提问by Wahid Bitar

How to let "DataGridViewTextBoxColumn" in DataGridViewsupports Multiline property?

如何让“ DataGridViewTextBoxColumnDataGridView支持多行属性

采纳答案by Tim S. Van Haren

You should be able to achieve this by setting the WrapModeof the DefaultCellStyleof your DataGridViewTextBoxColumnto true.

您应该能够通过设置来实现这一目标WrapModeDefaultCellStyle您的DataGridViewTextBoxColumntrue

回答by usman Majeed

Apart from setting WrapModeof the DefaultCellStyle, you can do the following:

除了设置WrapModeDefaultCellStyle,你可以做到以下几点:

  1. You need to catch GridView's EditingControlShowingEvent
  2. Cast Controlproperty on the EventArgs to the type you want (i.e. textbox, checkbox, or button)
  3. Using that casted type, change the Multilineproperty like below:
  1. 您需要捕获 GridView 的EditingControlShowing事件
  2. 铸造Control于EventArgs的到你想要的类型属性(即文本框,复选框或按钮)
  3. 使用该强制类型,更改如下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 WrapModeof the DefaultCellStyleof your DataGridViewTextBoxColumnto 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 AutoSizeRowsModeof the DataGridViewmust be set to the appropriate DataGridViewAutoSizeRowsModeenumeration value. A value such as DataGridViewAutoSizeRowsMode.AllCellsallows the cell to expand vertically and show the entire wrapped text.

我发现您需要在设计器中做两件事才能使文本单元格显示多行。作为添S.范·哈伦提到的,你需要设置WrapModeDefaultCellStyle你的DataGridViewTextBoxColumntrue。尽管这确实使文本换行,但它不会使行扩展以显示第一行以外的任何内容。此外WrapMode,该AutoSizeRowsModeDataGridView必须设置相应的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;
    }