By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 2,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

DataGridView

100+
P: 102
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
Expand|Select|Wrap|Line Numbers
  1. 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?

Expand|Select|Wrap|Line Numbers
  1.  Dim strSQL As String
  2.  
  3.         strSQL = "Select * FROM Evaluations WHERE FY='" & my_CurrentFY & "' AND MD_Delete='0' AND ID_Supplier=" & my_IDSupplier & " Order By FY DESC, QTR"
  4.  
  5.  
  6.         Try
  7.             Dim connectionString As String = SQL_ConnectionStr2
  8.  
  9.             Dim connection As New SqlConnection(connectionString)
  10.             Dim dataadapter As New SqlDataAdapter(strSQL, connection)
  11.             Dim ds As New DataSet()
  12.             connection.Open()
  13.             dataadapter.Fill(ds, "Authors_table")
  14.             connection.Close()
  15.             DGV_CurrentRatings.DataSource = ds
  16.             DGV_CurrentRatings.DataMember = "Authors_table"
  17.             DGV_CurrentRatings.Columns.Item("ID_Evaluations").Width = 0
  18.             DGV_CurrentRatings.Columns.Item("ID_Supplier").Width = 0
  19.             DGV_CurrentRatings.Columns.Item("FYStatus").Width = 0
  20.             DGV_CurrentRatings.Columns.Item("StatusDateTime").Width = 0
  21.             DGV_CurrentRatings.Columns.Item("EvalStatus").Width = 0
  22.             DGV_CurrentRatings.Columns.Item("EvalStatusDate").Width = 0
  23.             DGV_CurrentRatings.Columns.Item("FY").Width = 40
  24.  
  25.             With DGV_CurrentRatings.ColumnHeadersDefaultCellStyle
  26.                 .Font = New Font(DGV_CurrentRatings.Font, FontStyle.Bold)
  27.             End With
  28.  
  29.         Catch EX As Exception
  30.             MsgBox(EX.Message)
  31.             Return False
  32.             Exit Function
  33.         End Try
Dec 8 '15 #1
Share this Question
Share on Google+
3 Replies


100+
P: 299
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.
Dec 8 '15 #2

100+
P: 102
The query does return rows and the column is spelled correctly, if this is what you mean.
Dec 8 '15 #3

100+
P: 299
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.
Expand|Select|Wrap|Line Numbers
  1. DataGridView1.Columns.Item(0).Width = 0
Dec 10 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.