DataGridView strange cell wrapping effect

Hi

I have a DataGridView with all columns Wrap Mode set to false. A button which toggles the column wrap mode between false and true on one column.

The button does work, showing the cells in the column flipping between wrapped/not wrapped.

However, if Wrap is currently set to true, then scrolling down in the DataGridView control shows a bad display - it seems the text in the cells is wrapping, but the cell height has not adjusted correctly and remains at single line height. The cells initially shown in the view are expanded properly, only those 'out of sight' show this corruption when scrolling into view.

I have tried all sorts of property settings to try to fix this, but have failed so far.

Any hints for this problem?

[767 byte] By [leshay] at [2008-1-10]
# 1

Hi

Trying to bump this post.

This is the code I use to toggle the WrapMode for a particular column:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Me.DataGridView1.Rows.Count < 1 Then Exit Sub
If Me.DataGridView1.Columns(10).DefaultCellStyle.WrapMode = DataGridViewTriState.False Then
Me.DataGridView1.Columns(10).DefaultCellStyle.WrapMode = DataGridViewTriState.True
Else
Me.DataGridView1.Columns(10).DefaultCellStyle.WrapMode = DataGridViewTriState.False
End If
Me.DataGridView1.Width += 1 ' ********** see note below
End Sub

When I click the button, *without* the noted line, then I can see the text in the column does Wrap, but the row heights don't - hence the noted line. With the noted line inserted, then when clicking the button, the visible portion Wraps and the row heights adjust accordingly - however, when then scrolling down, the initially non visible portions of the DataGridView scroll into view with the text Wrapped, but the row heights are not adjusted - they just remain single size.

I have tried with a 'Me.DataGridView.Refresh' in both the routine above and in the DataGridView scroll event, but that doesn't appear to improve anything.

Why, when the WrapMode has been programatically changed, would the actual control not show it as such? Is there something I am doing wrong here, or perhaps something which I need to do to cause the changes to propogate through the DataGridView?

leshay at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

I've looked at this yesterday and again today and can't duplicate the effect you are seeing.

Could you post the Datagridview section from Form.Designer.vb so that we can see exactly how you have the grid configured.

Dave299 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

Hi Dave

Here is the section:

'
'DataGridView1
'
Me.DataGridView1.AllowUserToAddRows = False
Me.DataGridView1.AllowUserToDeleteRows = False
Me.DataGridView1.AllowUserToOrderColumns = True
Me.DataGridView1.AllowUserToResizeRows = False
Me.DataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader
Me.DataGridView1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells
Me.DataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column8, Me.Column11, Me.Column6, Me.Column1, Me.Column2, Me.Column9, Me.Column10, Me.Column3, Me.Column4, Me.Column5, Me.Column7})
DataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft
DataGridViewCellStyle4.BackColor = System.Drawing.SystemColors.Window
DataGridViewCellStyle4.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
DataGridViewCellStyle4.ForeColor = System.Drawing.SystemColors.ControlText
DataGridViewCellStyle4.SelectionBackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
DataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.ControlText
DataGridViewCellStyle4.WrapMode = System.Windows.Forms.DataGridViewTriState.[False]
Me.DataGridView1.DefaultCellStyle = DataGridViewCellStyle4
Me.DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill
Me.DataGridView1.Location = New System.Drawing.Point(0, 0)
Me.DataGridView1.Margin = New System.Windows.Forms.Padding(3, 2, 3, 2)
Me.DataGridView1.MultiSelect = False
Me.DataGridView1.Name = "DataGridView1"
Me.DataGridView1.ReadOnly = True
Me.DataGridView1.RowHeadersVisible = False
Me.DataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing
DataGridViewCellStyle5.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
DataGridViewCellStyle5.ForeColor = System.Drawing.Color.FromArgb(CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(64, Byte), Integer))
DataGridViewCellStyle5.SelectionBackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
DataGridViewCellStyle5.SelectionForeColor = System.Drawing.Color.FromArgb(CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(64, Byte), Integer))
Me.DataGridView1.RowsDefaultCellStyle = DataGridViewCellStyle5
Me.DataGridView1.RowTemplate.Height = 28
Me.DataGridView1.ShowCellErrors = False
Me.DataGridView1.ShowEditingIcon = False
Me.DataGridView1.ShowRowErrors = False
Me.DataGridView1.Size = New System.Drawing.Size(912, 268)
Me.DataGridView1.TabIndex = 0
Me.DataGridView1.TabStop = False

leshay at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4
Try changing AutoSizeRowsMode to AllCells - it's currently set to DisplayedCells

Dave299 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

OK Dave, that did help.

I can't imagine missing that! I tried all sorts of property settings.

I suspect that the fact that I was also using any combination of the property settings as well as a mixture of

'Me.DataGridView1.AutoResizeColumns()'
'Me.DataGridView1.AutoResizeColumns(10)'

'Me.DataGridView1.Width += 1'

had to have contributed.

Particularly the first of those three which is very slow (he third seems to be the fastest.

Thanks again - I'll have to mention you in the credits Smile

leshay at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...