Replace the name of the cities with "city" RRS feed

  • Question

  • User-1262319794 posted

    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.

    Time| Atlanta_1| Atlanta_2| Atlanta_3|

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

    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

    SqlConnection con = new SqlConnection("My Connection");
             string s = "My Stored Procedure"
                   SqlDataAdapter da = new SqlDataAdapter(s,con);
                   DataSet ds = new DataSet();
                   gridView1.DataSource = ds;
    Tuesday, August 7, 2018 6:08 PM


  • User753101303 posted


    So Attlanta_x are column names ? You could change the column names inside the DataTable before binding:

    var ds = GetDataSet();
    foreach(DataColumn col in ds.Tables[0].Columns)
      col.ColumnName = col.ColumnName.Replace("Atlanta_", "City_");
    gridView1.DataSource = ds;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 7, 2018 6:35 PM