How to set DataGridView textbox column to multi-li

2019-01-08 22:30发布

How to let "DataGridViewTextBoxColumn" in DataGridView supports Multiline property?

4条回答
不美不萌又怎样
2楼-- · 2019-01-08 22:40
    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;
    }
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-08 22:42

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. 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.

查看更多
看我几分像从前
4楼-- · 2019-01-08 22:49

You should be able to achieve this by setting the WrapMode of the DefaultCellStyle of your DataGridViewTextBoxColumn to true.

查看更多
等我变得足够好
5楼-- · 2019-01-08 22:54

Apart from setting WrapMode of the DefaultCellStyle, you can do the following:

  1. You need to catch GridView's EditingControlShowing Event
  2. Cast Control property on the EventArgs to the type you want (i.e. textbox, checkbox, or button)
  3. Using that casted type, change the Multiline property like below:
private void MyGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    TextBox TB = (TextBox)e.Control;
    TB.Multiline = true;            
}
查看更多
登录 后发表回答