Hi,
Seems you want to copy the data from one table to another table.
I test on my side.
If only import rows into the datatable, the datatable don’t have any column, this makes no sense. And the bound datagridview will show nothing. The datatable really has the rowcount number, but there is no data can be accessed.
If datatable has columns before importing the data, you can get the data if the column names are same as the original datatable. So if you want to get all data use ImportRow method, make sure the imported datatable has the same column names as the orginal datatable.
Dim datatable01 As DataTable = New DataTable()
Dim datatable02 As DataTable = New DataTable()
Dim datatable03 As DataTable = New DataTable()
datatable01.Columns.Add("col01")
datatable01.Columns.Add("col02")
datatable01.Rows.Add("11", "aa")
datatable01.Rows.Add("22", "bb")
datatable03.Columns.Add("col01") ' same name
datatable03.Columns.Add("col03")
For Each dr As DataRow In datatable01.Rows
datatable02.ImportRow(dr)
datatable03.ImportRow(dr)
Next
Console.WriteLine("datatable02:" & datatable02.Rows.Count) ' datatable02.rows.count =2
Console.WriteLine("datatable02:" & datatable02.Columns.Count) ' datatable02.columns.count = 0
DataGridView1.DataSource = datatable02 ' no data
DataGridView2.DataSource = datatable03 ' only col01 has data
The copy method will copy all the data including rows and columns to another datatable. You can test the following code. I don’t know why there is nothing on your side. Please check whether the DtObj table gets the data correctly.
Dim datatable01 As DataTable = New DataTable()
Dim datatable02 As DataTable = New DataTable()
datatable01.Columns.Add("col01")
datatable01.Columns.Add("col02")
datatable01.Rows.Add("11", "aa")
datatable01.Rows.Add("22", "bb")
datatable02 = datatable01.Copy()
Console.WriteLine("first:" & datatable02.Rows.Count)
Console.WriteLine("first:" & datatable02.Columns.Count)
DataGridView1.Columns.Add(New DataGridViewTextBoxColumn())
DataGridView1.DataSource = datatable02 ' You can see all the data of datatable01
The third method is the common way to add rows to datatable.
If I misunderstood you, or you have further questions, please feel free to tell me.
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.