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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic DataGridView - no data displayed!?

Status
Not open for further replies.

combs

Programmer
Apr 18, 2002
78
US
I have some code that creates a tabcontrol, adds as many tabpages as will be needed and places a datagridview on each tabpage. The datasource does actually hold data, but nothing is being displayed in the datagridview.... Can you tell me what I'm missing or doing wrong?

Code:
strDBLocation = My.Settings.strDatabaseLocation
        conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & strDBLocation
        Dim objCmd As New OleDb.OleDbCommand
        Dim strName As String
        strName = "GRAND & REHBERG"

        objCmd.Connection = conn
        objCmd.CommandType = CommandType.Text

        If m_da Is Nothing Then m_da = New OleDb.OleDbDataAdapter
        m_da.SelectCommand = objCmd
        conn.Open()

        objCmd.CommandText = "SELECT DISTINCT Corner FROM tblWork WHERE Intersection = '" & strName & "' And Project = '" & m_Project & "' ORDER BY Corner"
        m_da.Fill(m_Dataset, "tblCorners")

        For i = New Int16 To m_Dataset.Tables("tblCorners").Rows.Count - 1
            objCmd.CommandText = "SELECT WorkItem As Item, Description, " _
            & "Length_Est As Length, Width_Est As Width, Quantity_Est As Quantity, UnitPrice_Est As Price, " _
            & "EstimatedCost As Cost FROM tblWork WHERE Intersection = '" _
            & strName & "' And Corner = '" & m_Dataset.Tables("tblCorners").Rows(i)(0).ToString _
            & "' And Project = '" & m_Project & "'"
            m_da.Fill(m_Dataset, "CornerNum" & i)
        Next


        'MsgBox(m_Dataset.Tables("CornerNum" & 0).Rows(0)(0).ToString & vbNewLine _
        '       & m_Dataset.Tables("CornerNum" & 0).Rows(0)(1).ToString & vbNewLine _
        '       & m_Dataset.Tables("CornerNum" & 0).Rows(0)(2).ToString & vbNewLine _
        '       & m_Dataset.Tables("CornerNum" & 0).Rows(0)(3).ToString, MsgBoxStyle.Information, "DeBug:")


        'CREATE NEW TABCONTROL ON LAST TAB PAGE....
        Dim tabctrl As New TabControl
        tabctrl.Location = New Point(75, 75)
        tabctrl.Height = 400
        tabctrl.Width = 700
        tabctrl.Name = "TabControl2"
        Me.TabControl1.Controls.Item(4).Controls.Add(tabctrl)

        For i = New Int16 To m_Dataset.Tables("tblCorners").Rows.Count - 1
            tabctrl.Controls.Add(New TabPage)
            tabctrl.Controls.Item(i).Text = m_Dataset.Tables("tblCorners").Rows(i)(0).ToString
            tabctrl.Controls.Item(i).Name = m_Dataset.Tables("tblCorners").Rows(i)(0).ToString
            Dim dgv As New DataGridView
            dgv.Height = 260
            dgv.Width = 650
            dgv.Location = New Point(10, 10)
            tabctrl.Controls.Item(i).Controls.Add(dgv)
            dgv.Name = m_Dataset.Tables("tblCorners").Rows(i)(0).ToString
            dgv.DataSource = m_Dataset.Tables("CornerNum" & i)
            dgv.Columns("Price").DefaultCellStyle.Format = "c"
            dgv.Columns("Cost").DefaultCellStyle.Format = "c"
            dgv.Columns.Item("Item").Width = 45
            dgv.Columns.Item("Description").Width = 236
            dgv.Columns.Item("Length").Width = 50
            dgv.Columns.Item("Width").Width = 50
            dgv.Columns.Item("Quantity").Width = 70
            dgv.Columns.Item("Price").Width = 70
            dgv.Columns.Item("Cost").Width = 85
        Next

        'CLEAN UP THE DATASETS SO THEY ARE EMPTY WHEN USED AGAIN...
        For i = New Int16 To m_Dataset.Tables("tblCorners").Rows.Count - 1
            m_Dataset.Tables("CornerNum" & i).Clear()
        Next
        m_Dataset.Tables("tblCorners").Clear()

        m_da.Dispose()
        objCmd.Dispose()
        conn.Close()
        conn.Dispose()
 
You already have
Code:
m_da.Fill(m_Dataset, [COLOR=red]"CornerNum"[/color] & i)
in the tablename.

So when you address the datagriview datasource you don't need
Code:
 dgv.DataSource = m_Dataset.Tables([COLOR=red]"CornerNum"[/color] & i)
here again.





Zameer Abdulla
 
I mean you may change it to
Code:
dgv.DataSource = m_Dataset.Tables(m_Dataset.Tables("tblCorners").Rows(i)(0).ToString)


Zameer Abdulla
 
ZmrAbdulla - Thanks for your response. Unfortunately, that was not the solution..... However, I did find the problem and it's working now. I will post the working code below.

The problem was that I had re-used some code where I was writing the data to textboxes and then clearing the datatables. That worked fine in that case because the data was written to the form before the tables were cleared.

In this case, I was clearing the tables and it - in turn - was clearing the data from the datagridview.


Code:
strDBLocation = My.Settings.strDatabaseLocation
        conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & strDBLocation
        Dim objCmd As New OleDb.OleDbCommand
        Dim strName As String
        strName = "GRAND & REHBERG"

        objCmd.Connection = conn
        objCmd.CommandType = CommandType.Text

        If m_da Is Nothing Then m_da = New OleDb.OleDbDataAdapter
        m_da.SelectCommand = objCmd
        conn.Open()

        objCmd.CommandText = "SELECT DISTINCT Corner FROM tblWork WHERE Intersection = '" & strName & "' And Project = '" & m_Project & "' ORDER BY Corner"
        m_da.Fill(m_Dataset, "tblCorners")

        For i = New Int16 To m_Dataset.Tables("tblCorners").Rows.Count - 1
            objCmd.CommandText = "SELECT WorkItem As Item, Description, " _
            & "Length_Est As Length, Width_Est As Width, Quantity_Est As Quantity, UnitPrice_Est As Price, " _
            & "EstimatedCost As Cost FROM tblWork WHERE Intersection = '" _
            & strName & "' And Corner = '" & m_Dataset.Tables("tblCorners").Rows(i)(0).ToString _
            & "' And Project = '" & m_Project & "'"
            m_da.Fill(m_Dataset, "CornerNum" & i)
        Next

[COLOR=green]
        'MsgBox(m_Dataset.Tables("CornerNum" & 0).Rows(0)(0).ToString & vbNewLine _
        '       & m_Dataset.Tables("CornerNum" & 0).Rows(0)(1).ToString & vbNewLine _
        '       & m_Dataset.Tables("CornerNum" & 0).Rows(0)(2).ToString & vbNewLine _
        '       & m_Dataset.Tables("CornerNum" & 0).Rows(0)(3).ToString, MsgBoxStyle.Information, "DeBug:")


        'CREATE NEW TABCONTROL ON LAST TAB PAGE....[/color]
        Dim tabctrl As New TabControl
        tabctrl.Location = New Point(75, 75)
        tabctrl.Height = 400
        tabctrl.Width = 700
        tabctrl.Name = "TabControl2"
        Me.TabControl1.Controls.Item(4).Controls.Add(tabctrl)

        For i = New Int16 To m_Dataset.Tables("tblCorners").Rows.Count - 1
            tabctrl.Controls.Add(New TabPage)
            tabctrl.Controls.Item(i).Text = m_Dataset.Tables("tblCorners").Rows(i)(0).ToString
            tabctrl.Controls.Item(i).Name = m_Dataset.Tables("tblCorners").Rows(i)(0).ToString
            Dim dgv As New DataGridView
            dgv.Height = 260
            dgv.Width = 650
            dgv.Location = New Point(10, 10)
            dgv.Name = m_Dataset.Tables("tblCorners").Rows(i)(0).ToString
            tabctrl.Controls.Item(i).Controls.Add(dgv)
            dgv.DataSource = m_Dataset.Tables("CornerNum" & i)
            dgv.Columns("Price").DefaultCellStyle.Format = "c"
            dgv.Columns("Cost").DefaultCellStyle.Format = "c"
            dgv.Columns.Item("Item").Width = 45
            dgv.Columns.Item("Description").Width = 236
            dgv.Columns.Item("Length").Width = 50
            dgv.Columns.Item("Width").Width = 50
            dgv.Columns.Item("Quantity").Width = 70
            dgv.Columns.Item("Price").Width = 70
            dgv.Columns.Item("Cost").Width = 85
            dgv.Update()
        Next
[COLOR=green]
        'CLEAN UP THE DATASETS SO THEY ARE EMPTY WHEN USED AGAIN...[/color]
[COLOR=red][B]PROBLEM:[/B]
        'For i = New Int16 To m_Dataset.Tables("tblCorners").Rows.Count - 1
        'm_Dataset.Tables("CornerNum" & i).Clear()
        'Next
        'm_Dataset.Tables("tblCorners").Clear()
[/color]
        m_da.Dispose()
        objCmd.Dispose()
        conn.Close()
        conn.Dispose()

Once the red colored code is commented out, it works as intended. I hope this may help someone else, although probably not many people will make this bone-headed mistake.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top