Solved

Datagridview fill with Data Table instead of Data Set questions

Posted on 2008-10-24
8
1,046 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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