Question : DataGridView - setting the column size ?

I after creating the DataGridView and binding it to a DataSource I try to set the Column Width and I get an error. I check and see that it has grid.columns.count as 0 as well rowcount yet the dataset has 4 rows and about 12 columns, AND this grid does display when the form on which it is placed is displayed. Please see the code. Maybe someone can find what I must be doing wrong here.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
Private Sub ShowOrderDetail(ByVal SalesHeaderID As Integer)
        Dim sql As String = "spOrderDetail"
        Dim ds As New DataSet
        Dim grd As New DataGridView

        Try
            Using conn As New SqlConnection(My.Settings.OrderEntrySalesConnectionString)
                conn.Open()
                Using da As New SqlDataAdapter(sql, conn)
                    With da
                        .SelectCommand.CommandType = CommandType.StoredProcedure
                        .SelectCommand.Parameters.Add("@SalesHeaderID", SqlDbType.Int).Direction = ParameterDirection.Input
                        .SelectCommand.Parameters("@SalesHeaderID").Value = SalesHeaderID
                        .Fill(ds)
                    End With
                End Using
            End Using
        Catch ex As Exception
            DisplayErrorMsgLogToTable(ex, "Customer Browse - audit - 1")
        End Try

        Try
            grd.Name = "grd"
            grd.DataSource = ds.Tables(0)
            grd.Size = New System.Drawing.Size(720, 220)
            '
            ' ******************************
            ' Here comes the error if uncomment the following
            ' commented lines. 
            '
            'grd.Columns(0).Width = 80
            'grd.Columns("Description").Width = 200
            'grd.Columns("By Wt.").Width = 45
            'grd.Columns("Inventory").Width = 55
            grd.Visible = True
            grd.ScrollBars = ScrollBars.Vertical
            grd.RowHeadersVisible = False
            Dim frm As New Windows.Forms.Form
            With frm
                .Size = New System.Drawing.Size(730, 300)
                .Controls.Add(grd)
                .Dock = DockStyle.Fill
                .BringToFront()
                .StartPosition = FormStartPosition.CenterParent
                .SizeGripStyle = Windows.Forms.SizeGripStyle.Hide
                .Text = "Order Detail for " + Me.cboCustomerPickupSalesHeaderID.SelectedText.ToString
                .MinimizeBox = False
                .MaximizeBox = False
                .ShowDialog()
                .Dispose()
                .Close()
            End With

        Catch ex As Exception
            DisplayErrorMsgLogToTable(ex, "ShowOrderDetail")
        End Try
    End Sub

Answer : DataGridView - setting the column size ?

Because you're creating the DataGridView dynamically the columns won't be available to you until the grid has completed binding. So, what you need to do is add a handler for the DataBindingComplete event and set the column sizes there:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
    AddHandler grd.DataBindingComplete, AddressOf grd_DataBindingComplete

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

        Dim grd As DataGridView = CType(sender, DataGridView)

        grd.Columns(0).Width = 80
        grd.Columns("Description").Width = 200
        grd.Columns("By Wt.").Width = 45
        grd.Columns("Inventory").Width = 55

    End Sub 
Random Solutions  
 
programming4us programming4us