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?

Recommended Answers

All 4 Replies

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
            Else
                oCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            End If
        Next
    End Sub

I need to look back at what I did with asp.net and try to figure out the differences in datagrid from forms to asp

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

Will keep the right click option for another time ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.