Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Datagridview fill with Data Table instead of Data Set questions

Posted on 2008-10-24
8
Medium Priority
?
1,056 Views
Last Modified: 2013-11-26
The code snipet is a sub wich does a query fills a DataTabe and assigns it to a Datagridview.datasource it works fine.
I have the following questions
1.-What is the difference by doing it with a Data Set instead of Data Table is it better..? "code snipet expected with DataSet
2.-How can I display 2 Data Table or Data Set in the same Datagridview "Code snipet expected"
3.-If I want to iterate trhough the Datagridview and read cell values, what's the best approuch..
     I already try with a Datagridviewrow method after the DataBindingComplete event
     I'm looking for an alternate method "code snipet expected

Thanks in advance for UR help.

DT.Clear()
        sSql = ""
        sSql = "SELECT TEMPIN.ID, TEMPIN.FECHAIN AS FECHA,"
        sSql += "TEMPIN.CB AS [CODIGO DE BARRAS], TEMPIN.POLVORIN, STAT.[DESC] AS ESTATUS, "
        sSql += " PRODUCTOS.[DESC] AS PRODUCTO"
        sSql += " FROM ((TEMPIN INNER JOIN"
        sSql += " STAT ON TEMPIN.IDSTAT = STAT.IDSTAT) INNER JOIN"
        sSql += " PRODUCTOS ON TEMPIN.TIPOID = PRODUCTOS.TIPOID)"
        ADOAdapter = New OleDb.OleDbDataAdapter(sSql, Con)
        ADOAdapter.Fill(DT)
        DataGridView1.DataSource = DT

Open in new window

0
Comment
Question by:ranhell
  • 4
  • 3
8 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 22797087
Hi ranhell,

A DataSet is like a database - it can "house" multiple tables, and their relationships.  If you are only using 1 table, then you don't need a DataSet.


>> 2.-How can I display 2 Data Table or Data Set in the same Datagridview "Code snipet expected"
     Can you clarify?  what are you looking for?

>>3.  Here's how to iterate through them:

        For Each row As DataGridViewRow In Me.DataGridView1.Rows

            Console.WriteLine(row.Cells(0).Value.ToString())

        Next

0
 
LVL 12

Expert Comment

by:techExtreme
ID: 22797163
1) If your work can be done by datatable, you shouldn't use a dataset as it will incurr extra overhead.

2) You cannot use two tables to fill a single datagrid, tho  if the tables are same, you can combine the results in a single table and bind the resultant table to the grid.

3) As suggested above, you can iterate over the gridview using foreach loop.

Enjoy Coding!
0
 
LVL 10

Author Comment

by:ranhell
ID: 22797516
VBRocks

>> 2.-How can I display 2 Data Table or Data Set in the same Datagridview "Code snipet expected"
     Can you clarify?  what are you looking for?
Yes, what I'm looking for is to fill a datagrid from 2 different tables with two different queries...is these posible...?, the thing is that on VB 6 I was using a MSFLEXGRID and I was able to show both tables in the same grid.
the attached code snipet shows both Subs and the queries i'm using.
The 2 tables I'm using are identical one for Active records and another for duplicate records.
Private Sub SHOWTEMP()
        DT.Clear()
        sSql = ""
        sSql = "SELECT TEMPIN.ID, TEMPIN.FECHAIN AS FECHA,"
        sSql += "TEMPIN.CB AS [CODIGO DE BARRAS], TEMPIN.POLVORIN, STAT.[DESC] AS ESTATUS, "
        sSql += " PRODUCTOS.[DESC] AS PRODUCTO"
        sSql += " FROM ((TEMPIN INNER JOIN"
        sSql += " STAT ON TEMPIN.IDSTAT = STAT.IDSTAT) INNER JOIN"
        sSql += " PRODUCTOS ON TEMPIN.TIPOID = PRODUCTOS.TIPOID)"
        ADOAdapter = New OleDb.OleDbDataAdapter(sSql, Con)
        ADOAdapter.Fill(DT)
        DataGridView1.DataSource = DT
 
 
        Try
        Catch Exp As Data.OleDb.OleDbException
            MsgBox("FillDataSet Procedure Error", MsgBoxStyle.Critical, "Load Report Error")
        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
        End Try
        Cursor.Current = Cursors.Default
 
 
 
    End Sub
 
    Private Sub SHOWDUP()
        'DataGridView1.DataSource = Nothing
        'DataGridView1.Refresh()
        DT.Clear()
        sSql = ""
        sSql = "SELECT DUPLICADOS.ID, DUPLICADOS.FECHAIN AS FECHA,"
        sSql += " DUPLICADOS.CB AS [CODIGO DE BARRAS], DUPLICADOS.POLVORIN, DUPLICADOS.IDSTAT AS ESTATUS,"
        sSql += " PRODUCTOS.[DESC] AS PRODUCTO"
        sSql += " FROM (DUPLICADOS INNER JOIN"
        sSql += "  PRODUCTOS ON DUPLICADOS.TIPOID = PRODUCTOS.TIPOID)"
        ADOAdapter = New OleDb.OleDbDataAdapter(sSql, Con)
        ADOAdapter.Fill(DT)
        DataGridView1.DataSource = DT
 
        Try
        Catch Exp As Data.OleDb.OleDbException
            MsgBox("FillDataSet Procedure Error", MsgBoxStyle.Critical, "Load Report Error")
        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
        End Try
        Cursor.Current = Cursors.Default
    End Sub

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 27

Expert Comment

by:VBRocks
ID: 22799506
ranhell - yes, it is possible, but I'm not sure the result will be exactly what you are looking for.

Consider this:

        Dim table1 As New DataTable()
        table1.Columns.Add("table1_Col1")

        Dim table2 As New DataTable()
        table2.Columns.Add("table2_Col 2")

        'add data to both tables:
        For i As Integer = 1 To 5
            table1.Rows.Add("Item " & i.ToString())
            table2.Rows.Add("Item " & i.ToString())
        Next

        table1.Merge(table2)   '< MERGE the 2 tables together

        'Print to output window:
        For Each row As DataRow In table1.Rows

            Console.WriteLine(row.Item(0).ToString().PadRight(10) & row.Item(1).ToString())

        Next

        Stop  'View Output window

        Me.DataGridView1.DataSource = table1


The above example will produce the following results:
Col1   Col2
Item 1    
Item 2    
Item 3    
Item 4    
Item 5    
          Item 1
          Item 2
          Item 3
          Item 4
          Item 5


Is that what you're wanting?

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 22799552
If you want them in the same column(s), then you may have to manually load the grid:

    Private Sub loaddata()

        Dim table1 As New DataTable()
        table1.Columns.Add("table1_Col1")

        Dim table2 As New DataTable()
        table2.Columns.Add("table2_Col 2")

        'add data to both tables:
        For i As Integer = 1 To 5
            table1.Rows.Add("Item " & i.ToString())
            table2.Rows.Add("Item " & i.ToString())
        Next


        Me.DataGridView1.Columns.Clear()

        'Add "Col1" to the DataGridView
        If Me.DataGridView1.Columns.Count = 0 Then _
            Me.DataGridView1.Columns.Add("Col1", "Col1")

        loadgrid(table1)
        loadgrid(table2)

    End Sub

    Private Sub loadgrid(ByVal table As DataTable)

        For Each row As DataRow In table.Rows

            Me.DataGridView1.Rows.Add(row.Item(0))

        Next

    End Sub

0
 
LVL 27

Accepted Solution

by:
VBRocks earned 1500 total points
ID: 22799607
Just as quick note:  If the columns in the 2 datatables are named EXACTLY the same, then when you MERGE them together, it will combine the 2 columns together into 1 column.  

Here's an example:

        Dim table1 As New DataTable()
        table1.Columns.Add("Col1")       '<  Column name is EXACTLY the same

        Dim table2 As New DataTable()
        table2.Columns.Add("Col1")       '<  Column name is EXACTLY the same

        'add data to both tables:
        For i As Integer = 1 To 5
            table1.Rows.Add("Item " & i.ToString())
            table2.Rows.Add("Item " & i.ToString())
        Next


        table1.Merge(table2)                  '< MERGE tables together here

        For Each row As DataRow In table1.Rows

            Debug.WriteLine(row.Item(0).ToString())

        Next

        Stop  'View Output Window


Outputs:

Col1
Item 1
Item 2
Item 3
Item 4
Item 5
Item 1
Item 2
Item 3
Item 4
Item 5
0
 
LVL 10

Author Comment

by:ranhell
ID: 22824020
I haven't had the time to try your code, but I seems that I will lead me to what I'm looking for,
give some time befoere I accept your solution.
Thanks
0
 
LVL 10

Author Closing Comment

by:ranhell
ID: 31509677
thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question