i wan create a datatable complex like tis 1!any1 can teach me how to do it?
The datatable consist 5 datacolumn(col1,col2,col3,col4 and col5)
the data of row of col1,col3 and col5 is fix("A","Book","100") and tis same data will insert into 30 row (when the ds.Tables(0) got 30 row)
the data of row of col2 and col4 is take from ds.Tables(0).


The example that show at txt.file is like below:
col1 ,col2 ,col3 ,col4 ,col5
A ,ds.Tables(0).Rows(i) ,Book ,ds.Tables(0).Rows(i) ,100
A ,ds.Tables(0).Rows(i) ,Book ,ds.Tables(0).Rows(i) ,100
A ,ds.Tables(0).Rows(i) ,Book ,ds.Tables(0).Rows(i) ,100
A ,ds.Tables(0).Rows(i) ,Book ,ds.Tables(0).Rows(i) ,100
A ,ds.Tables(0).Rows(i) ,Book ,ds.Tables(0).Rows(i) ,100

Thanks....
i hope can get any suggestion.Please...
Thanks:)

Dani AI

Generated

Short answer: use a nested loop and add rows with an object-array or a prebuilt template so you can repeat the same values many times without reassigning every column for each row. already showed how to create the target table and columns; below are two concise patterns that answer your follow-up about repeating rows 100 times, plus a few practical tips.

Example — repeat each source row N times (replace the source column names with your actual names):

Dim src As DataTable = ds.Tables(0)
Dim out As New DataTable("Result")
out.Columns.AddRange(New DataColumn() {New DataColumn("col1", GetType(String)), New DataColumn("col2", GetType(String)), New DataColumn("col3", GetType(String)), New DataColumn("col4", GetType(String)), New DataColumn("col5", GetType(Integer))})

Dim repeatCount As Integer = 100

For i As Integer = 0 To src.Rows.Count - 1
    Dim v2 = If(IsDBNull(src.Rows(i)("SourceCol2")), "", src.Rows(i)("SourceCol2"))
    Dim v4 = If(IsDBNull(src.Rows(i)("SourceCol4")), "", src.Rows(i)("SourceCol4"))
    Dim template As Object() = {"A", v2, "Book", v4, 100}
    For r As Integer = 1 To repeatCount
        out.Rows.Add(template)
    Next
Next

Example — repeat the entire set M times (create the 30-row block and append it several times):

Dim setsToRepeat As Integer = 5
For s As Integer = 1 To setsToRepeat
    For i As Integer = 0 To src.Rows.Count - 1
        out.Rows.Add("A", src.Rows(i)("SourceCol2"), "Book", src.Rows(i)("SourceCol4"), 100)
    Next
Next

Quick tips and caveats:

  • Call out.BeginLoadData() before bulk inserts and out.EndLoadData() after to improve speed for large counts.
  • Define column data types up front (avoid storing numbers as strings).
  • Watch for DBNull when reading source columns and convert/trim as needed.
  • If you later use a DataAdapter.Update, do not call AcceptChanges() before update because it clears row states.
  • For very large repeats consider streaming to the destination (file/DB) rather than keeping everything in-memory.

This builds on suggestion but uses template/object-array insertion to keep the code short and fast when repeating rows many times.

Recommended Answers

All 2 Replies

On the ds you neew to add a new table

ds.Tables.Add("MyNewTable")

Then on ds.Tables("MyNewtable"), you need to add 5 columns

ds.Tables("MyNewtable").Columns.Add("Col1")
ds.Tables("MyNewtable").Columns.Add("Col2")
ds.Tables("MyNewtable").Columns.Add("Col3")
ds.Tables("MyNewtable").Columns.Add("Col4")
ds.Tables("MyNewtable").Columns.Add("Col5")

Then, you need to create a row to be added to the table

Dim MyNewRow As DataRow = ds.Tables("MyNewtable").NewRow

Here you must assign the values to the columns

MyNewRow.Item("Col1") = "A"
MyNewRow.Item("Col3") = "Book"
MyNewRow.Item("Col5") = "100"
MyNewRow.Item("Col2") = ds.Tables(0).Rows(I).Item(SourceItemIndex1)
MyNewRow.Item("Col3") = ds.Tables(0).Rows(I).Item(SourceItemIndex2)

After filling it you must add the row to the table

ds.Tables("MyNewtable").Rows.Add(MyNewRow)

Hope this helps

if i wan repeat the data 100 times,how can i do?
MyNewRow.Item("Col1") = "A"

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.