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

Why would DataGridView.ColumnCount be valid on one form and be 0 when referenced from

P: 1
I use the following code to populate and format a DataGridView on one form:
Expand|Select|Wrap|Line Numbers
  1.         Dim qryString As String = "SELECT tblBasicData.DataField1 AS Item, tblBasicData.DataField2 AS Hazard, " &
  2.             "tblHIDChecklist.[Exists (Y/N)] AS [Exists], tblHIDChecklist.Description, tblBasicData.DataOrder " &
  3.             "FROM tblBasicData INNER JOIN tblHIDChecklist ON tblBasicData.BasicDataUID = tblHIDChecklist.BasicDataUID " &
  4.             "WHERE (((tblHIDChecklist.Export)=True)) " &
  5.             "UNION " &
  6.             "SELECT tblHIDDetail.HIDDetailUID AS Item, tblHIDDetail.Description AS Hazard, " &
  7.             "tblHIDDetail.Quantity AS [Exists], tblHIDDetail.Units AS Descripition, tblBasicData.DataOrder " &
  8.             "FROM (tblBasicData INNER JOIN tblHIDChecklist ON tblBasicData.BasicDataUID = tblHIDChecklist.BasicDataUID) " &
  9.             "INNER JOIN tblHIDDetail ON tblHIDChecklist.HIDChecklistUID = tblHIDDetail.HIDChecklistUID " &
  10.             "ORDER BY DataOrder;"
  11.  
  12.         ConnDB()
  13.         Dim cmd As New System.Data.OleDb.OleDbCommand(qryString, Conn)
  14.         dapDGV.SelectCommand = New OleDb.OleDbCommand(qryString, Conn)
  15.  
  16.         Dim cmb As New System.Data.OleDb.OleDbCommandBuilder(dapDGV)
  17.         dapDGV.Fill(dasDGV)
  18.         dgvExport.DataSource = dasDGV.Tables(0)
  19.         Conn.Close()
  20.         dgvExport.Columns("DataOrder").Visible = False
  21.         If dasDGV.Tables(0).Rows.Count > 0 Then
  22.             'Sequence through dataset and remove UIDs from any Detail records.
  23.             'Convert any -1 or 0s to blanks in all checklist records
  24.             For iRow As Integer = 0 To dgvExport.Rows.Count - 1
  25.                 If Len(dgvExport.Rows(iRow).Cells("Item").Value) = 36 Then
  26.                     dgvExport.Rows(iRow).Cells("Item").Value = ""
  27.                 End If
  28.                 If dgvExport.Rows(iRow).Cells("Exists").Value = -1 Then
  29.                     dgvExport.Rows(iRow).Cells("Exists").Value = ""
  30.                 ElseIf dgvExport.Rows(iRow).Cells("Exists").Value = 0 Then
  31.                     dgvExport.Rows(iRow).Cells("Exists").Value = ""
  32.                 End If
  33.             Next
  34.         Else
  35.             MsgBox("No records have been identified for export.")
  36.         End If
  37.  
  38.         MsgBox(dgvExport.ColumnCount)
  39. End Sub
I open a 2nd form using Form2.Show() where the first step is :
Expand|Select|Wrap|Line Numbers
  1. MsgBox(frmHazCatExport.dgvExport.ColumnCount)
When I test the routine, the first form shows data and the msgbox displays that there are 5 columns in the datagridview. However, when I open the 2nd form the msgbox displays that the columncount is 0. I am running this test because I'm trying to access columns in the datagridview but I am only able to access the form and the DGV itself. Anything beyond that (i.e., rows, columns) generates an error I assume because form2 thinks there are 0 columns in the DGV on the first form. The error generated is "Reference to a non-shared member requires an object reference." I'm confused. How can the form1 tell me there are 5 columns and the "next" step tell me there are 0 columns?
May 10 '17 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
When you open form2 do you have a proper reference to the instantiated form1?

Is your Grid View public?

Did you consider adding a property "column count" in Form 1 that you can access in Form 2?
May 11 '17 #2

Post your reply

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

Browse more Visual Basic .NET Questions on Bytes