C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Databinding »

VB.NET: Comparison of data tables based on Specific Column


Posted Date: 20 Jun 2009    Resource Type: Code Snippets    Category: Databinding
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 10



The following VB.NET describes how to compare two data tables.

/*It accepts two Data Tables as Parameters and Search Column Index*/

/* It returns the rows found in both the tables and */

/* and rows found in First Table only */

Drag and drop Four GridViews

Name them as dbGridView1, dbGridView2, GridViewRowsInFirst, GridViewRowsInBoth

Populate the dbGridView1 with Some Value.

Populate the dbGridView2 With Some Value.


Call CompareDataTables(dbGridView1.DataSource, dbGridView2.DataSource, 0)




Private Sub CompareDataTables(ByVal dTable1 As DataTable, ByVal dTable2 As DataTable, ByVal SearchColIndex As Integer)

Dim dr As DataRow

Dim dtRowsInBoth = New DataTable() 'For Rows In Both

Dim dtRowsOnlyInFirst = New DataTable() 'For Rows In First Table

Dim MatchRow As DataRow

For Each dr in dTable1.Rows

MatchRow = dTable2.Rows.Find(dr.Item(SearchColIndex))

If MatchRow Is Nothing Then 'If the record is not in Table 2

dtRowsOnlyInFirst.Rows.Add(dr) 'Add to RowsInFirstTable

Else

dtRowsInBoth.Rows.Add (MatchRow) 'Add to RowsInBoth Table

End If

Next

dtRowsOnlyInFirst.AcceptChanges()

dtRowsInBoth.AcceptChanges()


'Now Populate the GridView With the data tables

populateRowsInFirstTableGrid(dtRowsOnlyInFirst)
populateRowsInBothTableGrid(dtRowsInBoth)




End Sub

Private Sub populateRowsInFirstTableGrid(ByVal dt As DataTable)
'Bind GridView
GridViewRowsInFirst.DataSource = dt
GridViewRowsInFirst.Refresh

End Sub


Private Sub populateRowsInBothTableGrid(ByVal dt As DataTable)
'Bind GridView
GridViewRowsInBoth.DataSource = dt
GridViewRowsInBoth.Refresh


End Sub




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
VB.NET: Comparison of data tables based on Specific Column  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: C# - Sort the data table using Select Method
Previous Resource: VB.NET - Search For Records in Data Table
Return to Discussion Resource Index
Post New Resource
Category: Databinding


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use