Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am retrieving data from SQL Server to a grid view in ASP.Net. Below is the header of the grid view after I retrieved the data from the database.

C#
Time| Atlanta_1| Atlanta_2| Atlanta_3|


I want to replace the name of the cities with "City" dynamically e.g.

C#
Time| City_1| City_2| City_3|

So basically I want to replace a part of the header with "City" and I want to do it while binding it in a grid view NOT in SQL Server. Below is the code how I am retrieving data from the database to my gridview

What I have tried:

SqlConnection con = new SqlConnection("My Connection");

         string s = "My Stored Procedure"

               con.Open();
               SqlDataAdapter da = new SqlDataAdapter(s,con);
               DataSet ds = new DataSet();

               da.Fill(ds);

               gridView1.DataSource = ds;
               gridView1.DataBind();

               con.Close();
Posted
Updated 7-Aug-18 9:14am

1 solution

could change the column names inside the DataTable before binding:

C#
var ds = GetDataSet();
foreach(DataColumn col in ds.Tables[0].Columns)
{
  col.ColumnName = col.ColumnName.Replace("Atlanta_", "City_");
}
gridView1.DataSource = ds;
gridView1.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900