53

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106

5 Answers5

78

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

Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
  • 22
    you also need to set `dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;` see [this post](http://stackoverflow.com/questions/1706454/c-multiline-text-in-datagridview-control) – BornToCode Jul 08 '12 at 23:05
  • Remember that if you use custom Cell Styles in your table, you'll need to specify the WrapMode for each one you want wrapping, as new Styles default it to false. – Slate Apr 07 '14 at 12:42
32

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.

Derek W
  • 9,708
  • 5
  • 58
  • 67
Tom Faust
  • 1,385
  • 11
  • 11
  • 4
    For reference, `AutoSizeRowsMode` is a property of the `DataGridView`, and needs to be set to something like `DataGridViewAutoSizeRowsMode.AllCells` – Rebecca Scott Aug 31 '11 at 02:52
  • As @BenScott has stated, [`DataGridView.AutoSizeRowsMode`](http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.autosizerowsmode%28v=vs.110%29.aspx) is not a boolean value and must be assigned the appropriate `DataGridViewAutoSizeRowsMode` enumeration value. I will edit the answer to reflect this. – Derek W Jan 19 '15 at 17:23
  • I also set the AlternatingRowsDefaultCellStyle.WrapMode to have True to get it to work. – John Kurtz Mar 23 '18 at 16:37
5

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;            
}
KyleMit
  • 30,350
  • 66
  • 462
  • 664
usman Majeed
  • 51
  • 1
  • 1
0
    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;
    }
Pavan M
  • 9
  • 1
0

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;
rtuszynski
  • 21
  • 6
  • 1
    Question was answered 11 years ago, do you really think your answer is better than accepted one? – Igor Goyda Feb 28 '21 at 19:47
  • @IgorGoyda Yes as it seems there is no answer for creator question "how to make Multiline property for a column". This is what I was searching for and did not find answer here. He did not asked how to make it for every column and all the answers lead to it. Also many answers were made 3 years ago so I wonder why are you so rude towards me. – rtuszynski Mar 01 '21 at 14:47
  • 1
    I guess you missed the accepted answer, please, read it carefully. As for me, I don't see a difference between yours and accepted. – Igor Goyda Mar 01 '21 at 15:19
  • 1
    I guess I missed the accepted answer, too - specifically the "DefaultCellStyle" component. This plainly lists example code that works, which in this case, helped me get past my stumbling block, so +1 for that, even 11+ years after the original poster. – churchid Dec 21 '21 at 21:44