Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 221
Marcio Avatar asked Oct 16 '25 07:10

Marcio


2 Answers

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

like image 105
Marcio Avatar answered Oct 18 '25 01:10

Marcio


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

like image 38
The Barnacle Avatar answered Oct 18 '25 02:10

The Barnacle