How to set DataGridView textbox column to multi-line?

C#.NetDatagridviewDatagridviewcolumn

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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.

Solution 3 - C#

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;            
}

Solution 4 - C#

    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;
    }

Solution 5 - C#

If you would like to set Multiline property just for one column of your DataGridView you can do

dataGridView.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWahid BitarView Question on Stackoverflow
Solution 1 - C#Tim S. Van HarenView Answer on Stackoverflow
Solution 2 - C#Tom FaustView Answer on Stackoverflow
Solution 3 - C#usman MajeedView Answer on Stackoverflow
Solution 4 - C#Pavan MView Answer on Stackoverflow
Solution 5 - C#rtuszynskiView Answer on Stackoverflow