0

want to select only, Monday 10 and Tuesday 50.

 Monday    Tuesday    Wed
 10           40       9
 20           50       6
 30           70       4

the code i have so far:

    For Each row As DataRow In table.Rows

        table2.Rows.Add(row(0), row(1))

    Next

this adds all of monday and all of tuesday, but i only want 1 data from monday and 1 data from tuesday. ?

at first i thought i needed to have column, by adding new row?

           For Each row As DataColumn In table.Columns
           table2.Rows.InsertAt(newRowb, 0)
           table2.Rows.Add(row*(0), row(1))

 ***getting mixed up with rows and columns, as am having errors on *

           Next

but had error with new row, but is there any way to add specific data

1 Answer 1

1

You can use Linq-to-DataSet:

Dim filteredRows = From row In table
                   Where  row.Field(Of Int32)("Monday") = 10 _
                   OrElse row.Field(Of Int32)("Tuesday") = 50
table2 = filteredRows.CopyToDataTable()

is there anyway doing this using loops, as this manually. using some sort of loop?

of course:

Dim table2 = table.Clone()
For Each row As DataRow In table.Rows
    Dim monCount = row.Field(Of Int32)("Monday")
    Dim tueCount = row.Field(Of Int32)("Tuesday")
    If monCount = 10 OrElse tueCount = 50 Then
        Dim newRow = table2.Rows.Add()
        newRow.ItemArray = row.ItemArray
    End If
Next
Sign up to request clarification or add additional context in comments.

3 Comments

is there anyway doing this using loops, as this is done manually. because what if the data changes then i need to hard code...?
@mali: Edited my answer. But you could also replace the values with variables in the linq query.
question from this question stackoverflow.com/questions/17789312/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.