?
Solved

Datagridview fill with Data Table instead of Data Set questions

Posted on 2008-10-24
8
Medium Priority
?
1,055 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…

719 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