Windows Develop Bookmark and Share   
 index > Windows Forms General > add tabpages with datagridview to tab control
 

add tabpages with datagridview to tab control

i have a form with a tab control it has no tab pages I would like to add a page for each table in my dataset and add a dgv with the datasource of each table in my ds

the datagridview are displaying the same data what am i doing wrong?

Private Sub display()
For i As Integer = 0 To ds.Tables.Count - 1

Dim newPage As New TabPage(ds.Tables(i).TableName)
Dim dgv As New DataGridView()
dgv.DataSource = ds.Tables(i)
newPage.Controls.Add(dgv)
TabControl1.TabPages.Add(newPage)
Next
End Sub

McC
John Basedow  Tuesday, May 05, 2009 2:21 PM
Here you go:

Imports System.IO
Public Class Form1
    Private folderToScan As String = "C:\Temp\"
    Private ds As New DataSet
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateDs()
        display()
    End Sub
    Private Sub display()
        For i As Integer = 0 To ds.Tables.Count - 1

            Dim newPage As New TabPage(ds.Tables(i).TableName)
            Dim dgv As New DataGridView()
            dgv.Dock = DockStyle.Fill
            dgv.DataSource = ds.Tables(i)
            newPage.Controls.Add(dgv)
            TabControl1.TabPages.Add(newPage)
        Next
    End Sub
    Private Sub CreateDs()
        Dim myFiles() As String = getFiles()

        For i As Integer = 0 To myFiles.Length - 1
            Dim dsTemp As New DataSet
            dsTemp.ReadXml(myFiles(i))

            Dim dt As DataTable = dsTemp.Tables(0)
            dsTemp.Tables.Remove(dt)
            dt.TableName = Path.GetFileNameWithoutExtension(myFiles(i))
            ds.Tables.Add(dt)
        Next

    End Sub

    Private Function getFiles() As String()

        Return Directory.GetFiles(folderToScan, "*Emails.xml")
    End Function
End Class

Tergiver  Tuesday, May 05, 2009 4:51 PM
There's nothing wrong with your code, so I can only assume that there's something wrong with your DataSet.
Tergiver  Tuesday, May 05, 2009 2:57 PM
I just checked and the 2 tables are different.

what i will try now is seting the datasource out of that loop, how do i cast the control as a dgv?

For Each dt As DataTable In ds.Tables
For Each tp As TabPage In TabControl1.TabPages
For Each c As Control In tp.Controls
If TypeOf (c) Is DataGridView Then
((DataGridView)c).datasource = dt
End If
Next
Next
Next
McC
John Basedow  Tuesday, May 05, 2009 3:07 PM
I built a test app. I used the Northwind database and created a dataset with two tables. I filled the tables with their respective adapters. I then used your code, word for word, and produced the expected result.

The problem is not in that code, it's in the data set.
Tergiver  Tuesday, May 05, 2009 3:33 PM
Perhaps if you show the code for filling your data set, we can spot something there?
Tergiver  Tuesday, May 05, 2009 3:36 PM
you are right thats where the prob is here is the code,
Private Sub CreateDs()
Dim dsTemp As New DataSet

Dim myFiles() As String = getFiles()
Dim dt As DataTable

For i As Integer = 0 To myFiles.Length - 1
dt = New DataTable(myFiles(i).Replace(".xml", ""))
dsTemp.ReadXml(folderToScan & myFiles(i))
dsTemp.Tables(0).TableName = myFiles(i).Replace(".xml", "")
dt = dsTemp.Tables(0).Copy
dsTemp.Tables.Clear()
ds.Tables.Add(dt)
Next

End Sub

McC
John Basedow  Tuesday, May 05, 2009 3:37 PM
When you run it, are the tab page's names correct? Or are all the tab page names the same as well as containing the same data in their dgvs?
Tergiver  Tuesday, May 05, 2009 3:50 PM
the tab names are correct, i think the issue is related to the .Copy

this is really strange, i made the following change:
Private Sub CreateDs()
Dim myFiles() As String = getFiles()
Dim dt As DataTable

For i As Integer = 0 To myFiles.Length - 1
dt = New DataTable(myFiles(i).Replace(".xml", ""))
Dim dsTemp As New DataSet
dsTemp.ReadXml(folderToScan & myFiles(i))

dt = dsTemp.Tables(0).Copy
dsTemp = Nothing

'Here i get an error saying table name "Emails" already belongs to the dataset, but the file read is "Sent Emails"
ds.Tables.Add(dt)
Next

End Sub

Even though i am clearing the ds it still reads the same table!

McC
John Basedow  Tuesday, May 05, 2009 3:52 PM

Use remove instead:

dt = dsTemp.Tables(0)
dsTemp.Tables.Remove(dt)
ds.Tables.Add(dt)

Tergiver  Tuesday, May 05, 2009 4:01 PM

Oops, forgot the table name:

Private Sub CreateDs()
     Dim myFiles() As String = getFiles()
     Dim dt As DataTable
     For i As Integer = 0 To myFiles.Length - 1
        Dim dsTemp As New DataSet
        dsTemp.ReadXml(folderToScan & myFiles(i))
        dt = dsTemp.Tables(0)
        dsTemp.Tables.Remove(dt)
        dt.TableName = myFiles(i).Replace(".xml", "")
        ds.Tables.Add(dt)
    Next
End Sub

Tergiver  Tuesday, May 05, 2009 4:05 PM
Nope! still same message:A DataTable named 'Emails' already belongs to this DataSet.
and it is adding Emails 2x


i tried

For i As Integer = 0 To myFiles.Length - 1
Dim dsTemp As New DataSet
Dim dt As New DataTable

dsTemp.ReadXml(folderToScan & myFiles(i))

dt = dsTemp.Tables(0)
dsTemp.Tables.Remove(dt)
ds.Tables.Add(dt)
Next

and still doesnt work!
McC
John Basedow  Tuesday, May 05, 2009 4:10 PM
Yea, check my edited post. I had forgotten to name the table.
Tergiver  Tuesday, May 05, 2009 4:10 PM
changing the name of the table is ok, but its still the same data!! GRRRRRRRRRRRRRR
McC
John Basedow  Tuesday, May 05, 2009 4:13 PM
Heh.. are you certain those XML files don't have the same data?
Tergiver  Tuesday, May 05, 2009 4:13 PM
LOL yep

from line 1 sent emails
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<Emails>
<Sender>Kerr Michael</Sender>

from line1 emails

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<Emails>
<Sender>Plaut Joel</Sender>


McC
John Basedow  Tuesday, May 05, 2009 4:16 PM
i made these changes, and I have a watch on the myFiles(i), the name of the file read changes
but when dt = dsTemp.Tables(0) it ends up with Emails in stead of the file read!
Private Sub CreateDs()
Dim myFiles() As String = getFiles()

For i As Integer = 0 To myFiles.Length - 1
Dim dt As DataTable
Dim dsTemp As New DataSet

dt = New DataTable(myFiles(i).Replace(".xml", ""))

dsTemp.ReadXml(folderToScan & myFiles(i))
'dsTemp.Tables(0).TableName = myFiles(i).Replace(".xml", "")
dt = dsTemp.Tables(0)
dsTemp.Tables.Remove(dt)
ds.Tables.Add(dt)
dsTemp = Nothing
dt = Nothing
Next

End Sub

McC
John Basedow  Tuesday, May 05, 2009 4:31 PM
Yea, your problem is definately there. I just wrote this test app (creating e1.xml and e2.xml as per the snippets you gave). It works fine.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ds As New DataSet()
        CreateDs(ds)
        display(ds)

    End Sub

    Private Sub CreateDs(ByRef ds As DataSet)
        Dim dt As DataTable
        For i As Integer = 1 To 2
            Dim dsTemp As New DataSet
            dsTemp.ReadXml(String.Format("C:\\Temp\\e{0}.xml", i))
            dt = dsTemp.Tables(0)
            dsTemp.Tables.Remove(dt)
            dt.TableName = String.Format("e{0}", i)
            ds.Tables.Add(dt)
        Next
    End Sub

    Private Sub display(ByRef ds As DataSet)
        For i As Integer = 0 To ds.Tables.Count - 1

            Dim newPage As New TabPage(ds.Tables(i).TableName)
            Dim dgv As New DataGridView()
            dgv.DataSource = ds.Tables(i)
            newPage.Controls.Add(dgv)
            TabControl1.TabPages.Add(newPage)
        Next
    End Sub
End Class

  • Edited byTergiver Tuesday, May 05, 2009 4:35 PMWhy do I have problems with that code inserter?
  •  
Tergiver  Tuesday, May 05, 2009 4:33 PM
here is all the code, can you see something wrong?

Imports System.IO
Public Class Form1
Private folderToScan As String = "C:\Documents and Settings\kerrm\My Documents\Visual Studio 2008\Projects\Emailer\Emailer\bin\Debug\"
Private ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateDs()
display()
End Sub
Private Sub display()
For i As Integer = 0 To ds.Tables.Count - 1

Dim newPage As New TabPage(ds.Tables(i).TableName)
Dim dgv As New DataGridView()
dgv.Dock = DockStyle.Fill
dgv.DataSource = ds.Tables(i)
newPage.Controls.Add(dgv)
TabControl1.TabPages.Add(newPage)
Next
End Sub
Private Sub CreateDs()
Dim myFiles() As String = getFiles()

For i As Integer = 0 To myFiles.Length - 1
Dim dt As DataTable
Dim dsTemp As New DataSet

dt = New DataTable(myFiles(i).Replace(".xml", ""))

dsTemp.ReadXml(folderToScan & myFiles(i))

dt = dsTemp.Tables(0)
dsTemp.Tables.Remove(dt)
ds.Tables.Add(dt)
dsTemp = Nothing
dt = Nothing
Next

End Sub

Private Function getFiles() As String()

Dim files() As String
Dim di As New DirectoryInfo(folderToScan)
Dim aryFi As FileInfo() = di.GetFiles("*Emails.xml")


For i As Integer = 0 To aryFi.Length - 1
ReDim Preserve files(i)
files(i) = aryFi(i).Name.ToString
Next
Return files
End Function
End Class


McC
John Basedow  Tuesday, May 05, 2009 4:36 PM
Here you go:

Imports System.IO
Public Class Form1
    Private folderToScan As String = "C:\Temp\"
    Private ds As New DataSet
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateDs()
        display()
    End Sub
    Private Sub display()
        For i As Integer = 0 To ds.Tables.Count - 1

            Dim newPage As New TabPage(ds.Tables(i).TableName)
            Dim dgv As New DataGridView()
            dgv.Dock = DockStyle.Fill
            dgv.DataSource = ds.Tables(i)
            newPage.Controls.Add(dgv)
            TabControl1.TabPages.Add(newPage)
        Next
    End Sub
    Private Sub CreateDs()
        Dim myFiles() As String = getFiles()

        For i As Integer = 0 To myFiles.Length - 1
            Dim dsTemp As New DataSet
            dsTemp.ReadXml(myFiles(i))

            Dim dt As DataTable = dsTemp.Tables(0)
            dsTemp.Tables.Remove(dt)
            dt.TableName = Path.GetFileNameWithoutExtension(myFiles(i))
            ds.Tables.Add(dt)
        Next

    End Sub

    Private Function getFiles() As String()

        Return Directory.GetFiles(folderToScan, "*Emails.xml")
    End Function
End Class

Tergiver  Tuesday, May 05, 2009 4:51 PM
Note that I changed your folderToScan path.

Also, for the record, I'm not a VB programmer. In fact, I hate VB. Not too shabby for a VB-hater eh?
Tergiver  Tuesday, May 05, 2009 4:54 PM
Dude u rock! are you C#? I hate vb to but..... ya gotta pay the bills!

do the issue was with reading the file names? what was wrong?

McC
John Basedow  Tuesday, May 05, 2009 4:56 PM
I'm not sure. I saw that messy getFiles() function and immediately recognized that Directory.GetFiles would do the same thing, so I replaced it.
Tergiver  Tuesday, May 05, 2009 5:00 PM
Thank you so much for this help, I have never had such help from anywhere !!!

McC
John Basedow  Tuesday, May 05, 2009 5:01 PM

You can use google to search for other answers

Custom Search

More Threads

• Window Form - Label - Localization - Telugu language
• execute application based on time clock
• Callback Delegate on UI thread?
• how to paint in particular area
• ImageList
• Rendering a form into an image
• Scrolling larger "canvas"
• How do you stop the automatic reorder of rows in a datatable? (when u click on a column)
• ??? How to set a default image viewer
• How to pass a list[] into a listbox?