Solved

Datagridview fill with Data Table instead of Data Set questions

Posted on 2008-10-24
8
1,041 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 500 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now