Reputation: 63
i am retrieving table from data base and storing in data table.i need to change all column name in data table how to do it....
i want to replace _ in column name e.g
Supplier_Name //column name from data base
Supplier Name //i want to display like this
storing to data table
{
DataTable dt= new DataTable();
dt= table //table from data base
}
Upvotes: 2
Views: 4678
Reputation: 236208
dt.Columns["Supplier_Name"].Caption = "Supplier Name";
Or if names are dynamic:
foreach(DataColumn column in dt.Columns)
column.Caption = column.ColumnName.Replace("_", " ");
NOTE: DataColumn.Caption property supposed to be used for displaying columns friendly name, but developers of DataGridView control did not respect that intent. So, in order to display this changes in DataGridView, you should set headers to captions after binding to table:
foreach (DataGridViewColumn gridColumn in dataGridView1.Columns)
gridColumn.HeaderText = dt.Columns[gridColumn.HeaderText].Caption;
Upvotes: 5
Reputation: 63065
I would change the SQL statement rather than doing by coding, like below
select Supplier_Name as [Supplier Name] from MyTable
Upvotes: 1
Reputation: 1493
foreach(DataColumn column in dt.Columns)
{
column.Caption.Replace("_"," ");
//or column.ColumnName
}
Upvotes: 1