Help with adding data to DataTable

Hi,

I'm using the code below to create an xml file with data only for visible columns in the Grid, but when I export the file the xml file contains data in the invisible columns, how do I fix this error? For example data in columns 1 to 10 is included in dtNew2 eventhough they don't appear in the C1Screen2.

For i As Integer = 1 To 10
            Me.C1Screen2.Splits(0).DisplayColumns(i).Visible = False
        Next
        For i = 26 To 50
            Me.C1Screen2.Splits(0).DisplayColumns(i).Visible = False
        Next

        Dim dtnew2 As New DataTable
        ds.Tables.Clear()
        For col As Integer = 0 To C1Screen2.Splits(0).DisplayColumns.Count - 1
            If C1Screen2.Splits(0).DisplayColumns(col).Visible = True Then

                dtnew2.Columns.Add(C1Screen2.Splits(0).DisplayColumns(col).Name, C1Screen2.Columns(col).DataType)

                MsgBox(C1Screen2.Columns(col).Text)
            End If
        Next
        For row = 0 To C1Screen2.Splits(0).Rows.Count - 1
            dtnew2.Rows.Add(dtnew2.NewRow())

            For col As Integer = 0 To C1Screen2.Splits(0).DisplayColumns.Count - 1
                If C1Screen2.Splits(0).DisplayColumns(col).Visible = True Then

                    For col1 As Integer = 0 To dtnew2.Columns.Count - 1
                        If C1Screen2.Splits(0).DisplayColumns(col).Visible = True Then
                            dtnew2.Rows(row)(col1) = C1Screen2(row, col1)
                        End If
                    Next
                End If
            Next

        Next

        ds.Tables.Add(dtnew2)
        ds.Tables(0).WriteXml(Application.StartupPath + "\AOP40SC2d.xml")

Open in new window

vcharlesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
The problem is within these lines:
                    For col1 As Integer = 0 To dtnew2.Columns.Count - 1
                        If C1Screen2.Splits(0).DisplayColumns(col).Visible = True Then
                            dtnew2.Rows(row)(col1) = C1Screen2(row, col1)
                        End If
                    Next

Open in new window

You probably wanted this code:
                    For col1 As Integer = 0 To dtnew2.Columns.Count - 1
                        If C1Screen2.Splits(0).DisplayColumns(col).Name = dtnew2.Columns(col1).Name Then
                            dtnew2.Rows(row)(col1) = C1Screen2(row, col)
                        End If
                    Next

Open in new window

However, please note that your method is perhaps among the slowest ones. The better approach would be to use a Copy() method of the Grid's datastore and then delete the columns from the new dataset that are not visible
vcharlesAuthor Commented:
Hi,

Can you please send me an example using Copy().

Thanks,

Victor
CodeCruiserCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank You.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.