HI ,
I have a DataTable of 10 column ,i want new DataTable with column from 4 to 7 only
HI ,
I have a DataTable of 10 column ,i want new DataTable with column from 4 to 7 only
Hello.
You can select certain columns from a data table variable by using DefaultView.ToTable()
If you know the column names, you can put them in like this:
Assign activity: dt1 = dt1.DefaultView.ToTable(false, "col1", "col2", "col3")
or
Assign activity: dt1 = dt1.DefaultView.ToTable(false, {"col1", "col2", "col3"})
where the columns are like an array
So the other step is creating a list of the columns you want. Typically, you could simply store them to an array, then just use that in your code like this:
Declare: columnArray = {"col1","col2","col3"}
Assign activity: dt1 = dt1.DefaultView.ToTable(false, columnArray)
or, if only know the index of the columns like 4 to 7, then you would need to create the array of columnnames using those numbers, like this:
Declare: columnIndexes = {4,5,6,7}
Assign: columnArray = dt1.Columns.Cast(Of DataColumn).Where(Function(col) columnIndexes.Contains(dt1.Columns.IndexOf(col)) ).Select(Function(col) col.ColumnName).ToArray
Assign activity: dt1 = dt1.DefaultView.ToTable(false, columnArray)
For additional info do some vb.net searches: datatable take certain columns vb.net - Google Search
I hope this helps, and also I did not exactly test my examples so sorry if there are some mistakes.
Let me know if you need further help.
Regards.
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.