locked
Renaming Column Names in Grid View in User friendly format in UI only. RRS feed

  • Question

  • User816268407 posted

    I am using normal WebForm and displaying Grid View, but in the UI the column names displayed are as it is like in the Database. But I want them to be more readable i.e. employee_target_cases To Target Employee Cases in the UI. I don't want to rename the columns in the database. Is there any way to display them in more readable format just for the UI.

    Wednesday, April 7, 2021 4:02 PM

Answers

  • User475983607 posted

    I am using normal WebForm and displaying Grid View, but in the UI the column names displayed are as it is like in the Database. But I want them to be more readable i.e. employee_target_cases To Target Employee Cases in the UI. I don't want to rename the columns in the database. Is there any way to display them in more readable format just for the UI.

    You have the ability to set the columns headers to whatever test you like using the standard GridView templates.  The official GridView documentation covers the details.  It's all point and click.

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/data-access/accessing-the-database-directly-from-an-aspnet-page/inserting-updating-and-deleting-data-with-the-sqldatasource-cs

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 7, 2021 4:11 PM
  • User-939850651 posted

    Hi Shreyas09,

    I am using normal WebForm and displaying Grid View, but in the UI the column names displayed are as it is like in the Database.

    If this is the case, I guess you may have used GridView.AutoGenerateColumns Property to make the framework automatic Generate these columns, it will use the column name consistent with the database to display.

    If you need to modify the displayed column names, you could try these two methods:

    • Adjust the HeaderText of columns: You need to set AutoGenerateColumns to false, and set columns and its HeaderText one by one.

            Something like this:

    <Columns>
            <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" 
                InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" />
            <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
                SortExpression="CompanyName" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
        </Columns>
    • Add GridView.RowDataBound event, and then determine whether it is a HeaderRow in GridView and modify its content.

    Something like this:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (DataControlFieldHeaderCell cell in e.Row.Cells) 
    {
              if (cell.Text == "Some Unreadable ColumnName")
    {
                  cell.Text = "new columnName you want to display";
                }
        } } }

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 8, 2021 6:26 AM

All replies

  • User475983607 posted

    I am using normal WebForm and displaying Grid View, but in the UI the column names displayed are as it is like in the Database. But I want them to be more readable i.e. employee_target_cases To Target Employee Cases in the UI. I don't want to rename the columns in the database. Is there any way to display them in more readable format just for the UI.

    You have the ability to set the columns headers to whatever test you like using the standard GridView templates.  The official GridView documentation covers the details.  It's all point and click.

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/data-access/accessing-the-database-directly-from-an-aspnet-page/inserting-updating-and-deleting-data-with-the-sqldatasource-cs

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 7, 2021 4:11 PM
  • User-939850651 posted

    Hi Shreyas09,

    I am using normal WebForm and displaying Grid View, but in the UI the column names displayed are as it is like in the Database.

    If this is the case, I guess you may have used GridView.AutoGenerateColumns Property to make the framework automatic Generate these columns, it will use the column name consistent with the database to display.

    If you need to modify the displayed column names, you could try these two methods:

    • Adjust the HeaderText of columns: You need to set AutoGenerateColumns to false, and set columns and its HeaderText one by one.

            Something like this:

    <Columns>
            <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" 
                InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" />
            <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
                SortExpression="CompanyName" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
        </Columns>
    • Add GridView.RowDataBound event, and then determine whether it is a HeaderRow in GridView and modify its content.

    Something like this:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (DataControlFieldHeaderCell cell in e.Row.Cells) 
    {
              if (cell.Text == "Some Unreadable ColumnName")
    {
                  cell.Text = "new columnName you want to display";
                }
        } } }

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 8, 2021 6:26 AM
  • User816268407 posted

    Thanks alot for your detail explanation! 

    Thursday, April 8, 2021 3:55 PM