Now that I can see the data in the datagrid, I realize that the size of the fields is too small - the customerid and customer name fields both could use expanding so that I can see all of the text. Is there a way to programmatically extend the length of the field so that it can show the entire string?

9 Years
Discussion Span
Last Post by bajanpoet

Something like this?
It will expand the last column in the datagridview. You can edit it to your needs :=)

Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete

        Dim _DGV As DataGridView = DirectCast(sender, DataGridView)
        _DGV.Columns(_DGV.ColumnCount - 1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    End Sub

To help you a little more:

You also can add a ContextMenuStrip to your solution and respond on the rightclicking of a user into one of the columns in your datagrid.
For example, if the user rightclicks into a column a menuitem will be shown with "show all data". If clicked, that particular column will be autosized.

Add the variabele _ActiveColumn to the general declaration section.
Add a contextmenu to your solution with 1 item "Show all data"

Private _ActiveColumn As Integer = 0

    Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
        _ActiveColumn = e.ColumnIndex

        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.ContextMenuStrip1.Show(Me.DataGridView1, New Point(e.X, e.Y))
        End If

    End Sub

    Private Sub ShowAllDataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowAllDataToolStripMenuItem.Click
        For Each oCol As DataGridViewColumn In Me.DataGridView1.Columns
            If oCol.Index = _ActiveColumn Then
                oCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                oCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            End If
    End Sub

Ok I've finally gotten to look at these snippets.... 4Advanced, works like a charm.... :)

Will keep the right click option for another time ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.