Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
dt1 looks like:

AdminNo      ModuleCode
111411H      EG1001
111411H      Eg1003
111380Y      EG2011


dt2 looks like:

PaperNo    Module1    Module2   Module3 ....
1          EG1001     
2          EG1003     EG1001
3          EG2011


i want to check for each modulecode in dt1 to each row and column of dt2.
Example: is EG1001 appearing in col1, col2, col3 ...

i have codes as follows:

VB
Dim dt3 As New DataTable
dt3.Columns.Add("AdminNo", GetType(String))
dt3.Columns.Add("PaperNo", GetType(Integer))

Dim curmodule As String = String.Empty

For Each dr2 As DataRow In dt2.Rows

    curmodule = dr2("ModuleCode1").ToString

    For Each dr1 As DataRow In dt1.Rows
        If curmodule = dr1("ModuleCode").ToString Then


            Dim dt3row As DataRow
            dt3row = dt3.NewRow
            dt3row("AdminNo") = dr1("AdminNo")
            dt3row("PaperNo") = dr2("PaperNo")
            dt3.Rows.Add(dt3row)
            Me.DataGridView3.DataSource = dt3




its only checking against one column in dt2.
anyone help me out, thanks!
Posted
Comments
Pheonyx 24-Jul-13 3:41am    
Based on what you have put, you have to hard code each of the individual checks for the different columns. What exactly are you trying to achieve, and is your "dt" structure your database structure?
12345_abcde 24-Jul-13 3:55am    
yes is in database structure.

like for example:

EG1001 in dt1, check with dt2, col1,2,3,4,5,6,7,8,9 to see it matches any.

Change the loop to reverse. for example

VB
For Each dr1 As DataRow In dt1.Rows
    var module=dr1("ModuleCode1").ToString
    For Each dr2 As DataRow In dt2.Rows</pre>

If curmodule = dr1("ModuleCode").ToString Then
//do the logic


Hope this helps
 
Share this answer
 
Comments
12345_abcde 24-Jul-13 3:59am    
i changed to this :

For Each dr1 As DataRow In dt1.Rows

curmodule = dr1("ModuleCode").ToString

For Each dr2 As DataRow In dt1.Rows
Dim x, y As Integer
If curmodule = dr2.Item(y) Then


Dim dt3row As DataRow
dt3row = dt3.NewRow
dt3row("AdminNo") = dr1("AdminNo")
dt3row("PaperNo") = dr2("PaperNo")
dt3.Rows.Add(dt3row)

but it doesnt work.
Jameel VM 24-Jul-13 4:28am    
what's the pblm?
12345_abcde 24-Jul-13 5:17am    
the form sort of hang, data didnt load out. is my code correct?
i set y as unknown column as i dont know how many columns/what are the columns.
i edit my code to :
Dim dt3 As New DataTable
      dt3.Columns.Add("AdminNo", GetType(String)) '/*Add column AdminNo
      dt3.Columns.Add("PaperNo", GetType(Integer))

      Dim curmodule As String = String.Empty
      For Each dr1 As DataRow In dt1.Rows
          curmodule = dr1("ModuleCode").ToString
          For Each dr2 As DataRow In dt2.Rows
              Dim found As Boolean
              found = False
              For i = 0 To dt2.Columns.Count - 1

                  If curmodule = dr2(i).ToString Then
                      found = True
                      Dim dr3 As DataRow
                      dr3 = dt3.NewRow
                      dr3("AdminNo") = dr1("AdminNo")
                      dr3("PaperNo") = dr2("PaperNo")
                      dt3.Rows.Add(dr3)
                      DataGridView3.AutoGenerateColumns = True
                      Me.DataGridView3.DataSource = dt3

                  End If
              Next
          Next
      Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900