Thursday, August 3, 2017

selected column from DataTable and Rename the columns

If you want to show a selected column for DataTable not the whole dataset you can use this

VB.net
Dim selectedColumns() As String = {"Region_Name", "Country_Name"}
 Dim dt As DataTable = New DataView(dt_country).ToTable(False, selectedColumns) 'dt_country is a DataTable which I fetched from database


C#

string[] selectedColumns = {
"Region_Name",
"Country_Name"
};
DataTable dt = new DataView(dt_country).ToTable(false, selectedColumns); / dt_country is a DataTable which I fetched from database
     
   
I also want to update the column name

 dt.Columns("Region_Name").ColumnName = "Region Name"
 dt.Columns("Country_Name").ColumnName = "Country Name"

or if you want change the order of the column you can use do this
            dt.Columns("SMS API").SetOrdinal(0)
            dt.Columns("Region Name").SetOrdinal(1)
            dt.Columns("Country Name").SetOrdinal(2)


 Then pass in the grid this DataTable

No comments:

Post a Comment