Marcio
Marcio

Reputation: 1733

C# - DataGridView with Wrap text in Cell but without spaces

I have used the DataGridView with the line break but for some reason something is wrong. This is the code:

this.übersetzerDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
this.übersetzerDataGridView.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True;

Here is the result:

enter image description here

What am I doing wrong?

Upvotes: 4

Views: 6050

Answers (3)

The Barnacle
The Barnacle

Reputation: 49

Since @Michael posted his code in German and it might make some people confused (just like me) this did the work for me -

Add the following code to the RowValidated event in your DataGridView

Private Sub DGV_RowValidated(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_I.RowValidated
    DGV_I.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    DGV_I.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
    DGV_I.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
End Sub

The result -

DGV Wrap

Upvotes: 1

S Grave
S Grave

Reputation: 1

  1. Enable wrap mode for the column in question:

    dgv.Columns["dtComment"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;

  2. Set AutosizeRowsMode to AllCells

Upvotes: -1

Marcio
Marcio

Reputation: 1733

[SOLVED] After much searching, I finally found the solution.

By using Fill value in the column in question.

The column width adjusts so that the widths of all columns exactly fills the display area of the control.

 this.übersetzerDataGridView.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;         
 this.übersetzerDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;         
 this.übersetzerDataGridView.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

Here is the result:

enter image description here

Thanks a lot.

Upvotes: 7

Related Questions