Link to home
Start Free TrialLog in
Avatar of ranhell
ranhellFlag for Mexico

asked on

Datagridview fill with Data Table instead of Data Set questions

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

Avatar of VBRocks
VBRocks
Flag of United States of America image

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

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!
Avatar of ranhell

ASKER

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

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?

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

ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ranhell

ASKER

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
Avatar of ranhell

ASKER

thanks