Question

I have a simple gridview on the client side:-

        <asp:GridView ID="gvw_Details" runat="server" EmptyDataText="No Previous Enrollments were Found." SkinID="gridviewSkin2">
        </asp:GridView>

I am attaching SQL data to it via the SQLDataAdapter from C# Server Side:-

       using (SqlConnection cn = new SqlConnection(DBConnect.SqlServerConnection))
       {
            cn.Open();
            #region Pulls Existing Enrollments from SQL
            DataTable dt = new DataTable();
            using (SqlCommand cm = cn.CreateCommand())
            {
                // Create SQL Statement
                StringBuilder ct = new StringBuilder();
                ct.AppendLine("SELECT DISTINCT Key, Date, "
                + "NAME_First + ' ' + NAME_MI + ' ' + NAME_Last, "
                RESULT_VALUE, RESULT_UNITS ");
                ct.AppendLine("FROM [test].[dbo].[test]");
                ct.AppendLine("WHERE Date = @Date and ID = @ID");
                cm.Parameters.AddWithValue("@Date", _Requestdate);
                cm.Parameters.AddWithValue("@ID", _ID);
                cm.CommandType = CommandType.Text;
                cm.CommandText = ct.ToString();
                // Execute 
                cm.ExecuteNonQuery();

            #region Populate Gridview with extracted Data.
                SqlDataAdapter dr = new SqlDataAdapter(cm);
                dr.Fill(dt);
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                gvw_Details.DataSource = ds;
                gvw_Details.DataBind();
             #endregion
            }
            #endregion

Now Because my column names on the GridView are auto assigned via the SQL database column names, this process works great expect on the part where I am adding three SQL column name, first name + MI + last name to make 1 column of the GridView. Here although there is no run time errors, the gridview column name is given a default name = "column1".... I want it to say NAME. How can I do this?

Was it helpful?

Solution

I want it to say NAME. How can I do this?

ct.AppendLine("SELECT DISTINCT Key, Date, " +
    "NAME_First + ' ' + NAME_MI + ' ' + NAME_Last AS Name, " +
    "RESULT_VALUE, RESULT_UNITS ");

Simply ask your database system to return the calculated column with the name you require using the alias syntax AS

OTHER TIPS

You should add this into your code behind, and change the column index (at the minute it is set to 0) to correspond to the column header you want to change:

protected void gvw_Details_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // control flow - check e.Row.RowType is header
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Text = "NAME";
    }
}

Add in OnRowDataBound to the aspx code:

<asp:GridView ID="gvw_Details" runat="server" OnRowDataBound="gvw_Details_RowDataBound" EmptyDataText="No Previous Enrollments were Found." SkinID="gridviewSkin2">
        </asp:GridView>

Or alternatively, you can string concatenate the fields together in the SQL query itself and use the as keyword instead. [as in the below answer]

Making use of the OnRowDataBound event is a great way of modifying the presentation of data after binding to the datasource has taken place before rendering of the asp.net control.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top