Hi,

i have a code where I dynamically generate the columns name and bind it it datatable. now I am iterating thru loop for each column and fill the datatable. My Problem is when I fill for first column row is already added to the position(e.g 0,1). I want help with the code if row at some position already exist in datatable I should not write

dr = dt.NewRow()
 dt.Rows.Add(dr)

As it generates duplicate rows. Please help me with the code vb.net 2010.

Thanks

Clear the datatable after adding the row using dt.Clear() method

Thanks for the reply, but I think I didn't put my problem cleraly. Here's the code

For i = 0 To column count
For r As Integer = 0 To  row(rows in each column)
                        dr = dt.NewRow()
                      
                        dt.Rows.Add(dr)
                         dt.Rows(r).Item(i) = ds.Tables(0).Rows(r).Item(0)
                    Next
                          Next

Now my prob is if row diesn't exist for that col it should add otherwise it should place the record value at that position
e.g column 1 may have 3 records col2 may have 6 records.
Now for column 2 it should place first 3 values in the rows which already added for col1 at col2 positon and for rest 3 values it should add the rows and place the values at col2 position. Please help someone as i 've spent hours in finding solution

Thanks

' Create a new table & add columns.
Dim myDataTable As New DataTable("Users")
myDataTable.Columns.Add("ID", System.Type.GetType("System.Int32"))
myDataTable.Columns.Add("Name", System.Type.GetType("System.String"))
myDataTable.Columns.Add("Surname", System.Type.GetType("System.String"))

' Add the new table.
Me.ds.Tables.Add(myDataTable)

For Each row As DataGridViewRow In Me.myGrd.Rows

' Fill datarow.
Dim mydRow As DataRow = myDataTable.NewRow()
mydRow(1) = "ID"
mydRow(2) = "NAME"
mydRow(3) = "Surname"

Me.ds.Tables(0).Rows.Add(mydRow)

next

I used a grid you can use what ever you have, such as lists, arrays, other datasets

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.