DataGridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sc5502
    New Member
    • Jun 2014
    • 102

    DataGridView

    Background- Front end Visual Studio 2013 using visual basic, back end MS SQL Server 2012 and I am new to visual basic.

    I am using a data grid view. The code is below. The line of code
    Code:
    DGV_CurrentRatings.Columns.Item("ID_Evaluations").Width = 0
    generates the following error message: "Object reference not set to an instance of an object." What am I doing wrong?

    Code:
     Dim strSQL As String
    
            strSQL = "Select * FROM Evaluations WHERE FY='" & my_CurrentFY & "' AND MD_Delete='0' AND ID_Supplier=" & my_IDSupplier & " Order By FY DESC, QTR"
    
    
            Try
                Dim connectionString As String = SQL_ConnectionStr2
    
                Dim connection As New SqlConnection(connectionString)
                Dim dataadapter As New SqlDataAdapter(strSQL, connection)
                Dim ds As New DataSet()
                connection.Open()
                dataadapter.Fill(ds, "Authors_table")
                connection.Close()
                DGV_CurrentRatings.DataSource = ds
                DGV_CurrentRatings.DataMember = "Authors_table"
                DGV_CurrentRatings.Columns.Item("ID_Evaluations").Width = 0
                DGV_CurrentRatings.Columns.Item("ID_Supplier").Width = 0
                DGV_CurrentRatings.Columns.Item("FYStatus").Width = 0
                DGV_CurrentRatings.Columns.Item("StatusDateTime").Width = 0
                DGV_CurrentRatings.Columns.Item("EvalStatus").Width = 0
                DGV_CurrentRatings.Columns.Item("EvalStatusDate").Width = 0
                DGV_CurrentRatings.Columns.Item("FY").Width = 40
    
                With DGV_CurrentRatings.ColumnHeadersDefaultCellStyle
                    .Font = New Font(DGV_CurrentRatings.Font, FontStyle.Bold)
                End With
    
            Catch EX As Exception
                MsgBox(EX.Message)
                Return False
                Exit Function
            End Try
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    The real meaning is that you're trying to change the property of an object that does not exist or is null. This just means that the columns were either never created or the name of the column is not a match.

    Comment

    • sc5502
      New Member
      • Jun 2014
      • 102

      #3
      Data Grid View

      The query does return rows and the column is spelled correctly, if this is what you mean.

      Comment

      • Luk3r
        Contributor
        • Jan 2014
        • 300

        #4
        That is, in fact, what I meant. The fact remains that your code thinks your column doesn't exist. The way you're trying to modify the column width is by means of checking the column header text. You could always try to use an integer in place of the header text for troubleshooting .
        Code:
        DataGridView1.Columns.Item(0).Width = 0

        Comment

        Working...