Link to home
Start Free TrialLog in
Avatar of vensali
vensaliFlag for India

asked on

how to wrap a merged cells of datagridview column in vs2010 (vb.net)

Pls suggest on how to achieve this feature in vb.net (vs2010)
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Can you be more descriptive in stating what you need? What you want to accomplish?
Avatar of vensali

ASKER

I have a column in datagrid with merged cells. I need to wrap the text in merged cells.  I am able to do the text wrapping unmerged cells
Can you post the code how you are merging and un-merging cells in your code
Avatar of vensali

ASKER

Column to be merged in datagridview -  2
       
dgv1.Columns(2).Width = 200
 dgv1.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter   in form load event

  If e.ColumnIndex = 2 AndAlso e.RowIndex <> -1 Then

            Using gridBrush As Brush = New SolidBrush(Me.dgv1.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)

                Using gridLinePen As Pen = New Pen(gridBrush)
                    ' Clear cell  
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds)

                    ' Draw line (bottom border and right border of current cell)  
                    'If next row cell has different content, only draw bottom border line of current cell  
                    If e.RowIndex < dgv1.Rows.Count - 1 AndAlso dgv1.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString() <> e.Value.ToString() Then
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
                    End If

                    ' Draw right border line of current cell  
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)

                    ' draw/fill content in current cell, and fill only one cell of multiple same cells  
                    If Not e.Value Is Nothing Then
                        If e.RowIndex > 0 AndAlso dgv1.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString() = e.Value.ToString() Then
                        Else
                            e.Graphics.DrawString(CType(e.Value.ToString, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault)
                        End If
                    End If

                    e.Handled = True
                End Using
            End Using
        End If
Avatar of vensali

ASKER

dgv1.Columns(2).DefaultCellStyle.WrapMode = DataGridViewTriState.True
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vensali

ASKER

thanks