Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot access columns in runtime create DataGridView 1

Status
Not open for further replies.

VDavid

Programmer
Sep 14, 2003
118
VE
Hi,

Here I am, wondering what I'm missing in my code.

Maybe I should just ask if there is a problem or a must-do-code that have to be present when one tries to create a datagridview in runtime and then tries to access the columns. I always gets zero for the columns count even thought I can see the information displayed.
Code:
Dim DGVLista As DataGridView
DGVLista = New DataGridView()
da2.Fill(ds2, "list")
DGVLista.DataSource = ds2
DGVLista.DataMember = "list"
If DGVLista.Columns.Count > 0 Then [red][b]always zero[/b][/red]
  DGVLista.Columns(0).Visible = False
End If

If this is true, then please forget the rest of the post, if not, them be patient while reading the rest of this post.


Background:
It's not easy to explain, I'm trying to parametrize some process, and to do this I'm retrieving a list of records from the database that contains "metadata" of different information.
One example, take a list of groups, the user selects one group and a list of possible things to do with that group is displayed in the options panel (the list of options comes from the database), if the user selects one option, from the database I retrieve the suboptions, and they get populate in datagridviews inside TabPages. Each datagridview, tabpage and button is create at runtime.

List of Groups
Admin
Power User

Options
Group Privileges
Group Father

[highlight]Assign[/highlight]
Delete User
[highlight]Remove[/highlight]
Create user
Modify User


My problem is, after creating the datagridview the columns count is always zero. In the screen I see the information and is correct, but I cannot access the columns through code.

I would appreciate any help.


Code:
Dim ds As New DataSet
Dim ds2 As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim da2 As OleDb.OleDbDataAdapter
Dim Tab As TabPage
Dim Boton As Button
Dim DGVLista As DataGridView

da = New OleDb.OleDbDataAdapter("exec USUARIOS.dbo.OPCIONXUSUARIO_select_opcion_tab_sp " & DGVOpciones.Item("PKOPCION", DGVOpciones.CurrentRow.Index).Value, Conexion)
da.Fill(ds, "tab")
Dim QUERY_BOTON(ds.Tables("tab").Rows.Count) As String

TCListas.TabPages.Clear()
Var.Indice = 0
While Var.Indice < ds.Tables("tab").Rows.Count
    Tab = New TabPage
    Tab.Text = ds.Tables("tab").Rows(Var.Indice).Item("PROCESO")
    If ds.Tables("tab").Rows(Var.Indice).Item("QUERY_LISTA") IsNot DBNull.Value Then
        DGVLista = New DataGridView()
        da2 = New OleDb.OleDbDataAdapter(ds.Tables("tab").Rows(Var.Indice).Item("QUERY_LISTA") & " " & PK & ", " & Var.Sesion.PKCICLO & ", " & Var.Sesion.PKUSUARIO, Conexion)
        da2.Fill(ds2, "listas" & Var.Indice)
        DGVLista.AutoGenerateColumns = True
        DGVLista.DataSource = ds2
        DGVLista.DataMember = "listas" & Var.Indice
        DGVLista.ReadOnly = True
        DGVLista.AllowUserToResizeColumns = True
        DGVLista.AllowUserToOrderColumns = True
        DGVLista.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
        DGVLista.RowHeadersVisible = False
        DGVLista.MultiSelect = False
        DGVLista.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DGVLista.BorderStyle = BorderStyle.Fixed3D
        DGVLista.Anchor = AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right + AnchorStyles.Top
        DGVLista.Width = 190
        If ds.Tables("tab").Rows(Var.Indice).Item("QUERY_BOTON") Is DBNull.Value Then
            DGVLista.Height = 90
        Else
            DGVLista.Height = 60
        End If
        DGVLista.Left = 6
        DGVLista.Top = 6
        If DGVLista.Columns.Count > 0 Then  [red][b]always zero[/b]
            DGVLista.Columns(0).Visible = False
        End If
        Tab.Controls.Add(DGVLista)
    End If
    ds2.Dispose()
    If ds.Tables("tab").Rows(Var.Indice).Item("QUERY_BOTON") IsNot DBNull.Value Then
        Boton = New Button
        Boton.Text = ds.Tables("tab").Rows(Var.Indice).Item("BOTON")
        Boton.Width = 136
        Boton.Height = 23
        Boton.Left = 45
        Boton.Top = 70
        Boton.Anchor = AnchorStyles.Bottom
        Tab.Controls.Add(Boton)
        QUERY_BOTON(Var.Indice) = ds.Tables("tab").Rows(Var.Indice).Item("QUERY_BOTON")
    End If

    Me.TCListas.TabPages.Add(Tab)

    Var.Indice = Var.Indice + 1
End While

Thanks for the help.
David

 
Hi,

Thanks for the replay.

But I have done that and it didn't work.

Also, according to the help if the datagridview is populate trough a datasource the AutoGenerateColumns property is set to true by default. I did tried it anyway just to be sure :)

Any other idea will be appreciated.

David

 
I doubt this will help, but try moving this line of code above your If statement

Tab.Controls.Add(DGVLista)
 
Hi, let me try your suggestion and I'll get back to you on Monday.

Thanks
 
Hi,
Getting back from the weekend.

I tried your suggestion, still not working. But I have another clue, not only I can't access the columns, but neither the rows. It's like the datagridview hasn't been initialize. I will with this theory and search for solutions.

Any help will be appreciated.

David
 
I tried a generic test. The following code yields the expected results - 2 columns and 3 rows. Give it a try on a test form and see if it works. Sometimes its something simple to overlook, and if you have a working reference, you might be able to find the issue.

Code:
        Dim dgv As New DataGridView
        dgv.Size = Me.ClientSize
        Me.Controls.Add(dgv)

        Dim ds As New DataSet
        ds.Tables.Add("list")
        Dim dt As DataTable = ds.Tables(0)
        dt.Columns.Add("Column1", System.Type.GetType("System.Int32"))
        dt.Columns.Add("Column2", System.Type.GetType("System.String"))

        Dim dr1 As DataRow = dt.NewRow
        dr1.Item(0) = 0
        dr1.Item(1) = "Item 0"

        Dim dr2 As DataRow = dt.NewRow
        dr2.Item(0) = 1
        dr2.Item(1) = "Item 1"

        dt.Rows.Add(dr1)
        dt.Rows.Add(dr2)

        dgv.DataSource = ds
        dgv.DataMember = "list"

        MessageBox.Show("Columns:  " & dgv.Columns.Count.ToString & ControlChars.CrLf & "Rows:  " & dgv.Rows.Count.ToString)
 
Thank you!!!

With your example I was able to find the problem.
I was missing: Me.Controls.Add(dgv)

Thank you again.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top