?
Solved

Loop table columns and delete

Posted on 2007-07-27
3
Medium Priority
?
382 Views
Last Modified: 2010-04-23
i want to loop through a datarow and it the value is 0 i would like to delete that column.

i have got so far but when i delete the column it causes a problem in the loop statement. i have come across this recently in another situation an i was told to loop backwards, but how can i do this when looping a list of columns.

my current code is;
        Dim dci As Integer = 0

        For Each c As DataColumn In ds.Tables("Grid").Columns
            If drw.Item(dci) = "0" Then
                ds.Tables("Grid").Columns.Remove(c)
            End If
            dci = dci + 1
        Next


thanks
0
Comment
Question by:Marcusw
  • 2
3 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 19581908
Try this:

       Dim dci As Integer = 0
       Dim dt As DataTable = ds.Tables("Grid")
       For index As Integer = dt.Columns.Count - 1 To 0 Step -1
            Dim c As DataColumn = dt.Columns(index)
            If drw(c).ToString() = "0" Then
                dt.Columns.Remove(c)
            End If
            dci += 1
        Next

Bob
0
 
LVL 33

Expert Comment

by:hongjun
ID: 19581910
Try

For i = ds.Tables("Grid").Columns.Count - 1 To 0
      If drw.Item(dci) = "0" Then
            ds.Tables("Grid").Columns.Remove(i)
      End If
      dci = dci + 1
Next
0
 
LVL 33

Expert Comment

by:hongjun
ID: 19581917
Yes you will need the "Step -1".
I left it out.

hongjun
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

807 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