• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1240
  • Last Modified:

dynamic gridview caption, c# codebehind

The attribute caption="Students" need to be dynamic based on session variable Session["type"] from event in code behind (grdHighLight_RowDataBound()).

Session["type"] = 1 -->  caption="Students"
Session["type"] = 2 -->  caption="Teacher"
Session["type"] = 3 -->  caption="Parent"

Question: How could this be done?

Important: Although a gridview tag included with the aspx file but it gets populated on the run via LoadData() event in load event.

Thank you.
------------------------- aspx -----------------------                                   
   <asp:GridView runat="server" ID="grdHighLight"  caption="Students" onrowdatabound="grdHighLight_RowDataBound" CellSpacing="-1" GridLines="None" >

------------------------- aspx.cs ------------------
public partial class MasterPage2 : System.Web.UI.MasterPage
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ROD_July18ConnectionString"].ToString());
    RodCriteria rc = new RodCriteria();
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
            Session["StatType_ID"] = rc.ReadCriteria("StatIdS");
            Session["YYYY"] = rc.ReadCriteria("YYYY");
            Session["RegionID"] = rc.ReadCriteria("RegionId");
            Session["StatS"] = rc.ReadCriteria("StatS");
            //GridView gview = new GridView();
            //gview = (GridView)Page.FindControl("grdHighLight");
            //gview.Caption = "<span style='color:Green;'>" + gview.Caption + "</span>";
    private GridView setupGridView(string caption, GridView gview)

    private void LoadData()
        SqlCommand cmd = new SqlCommand("spAuunuaCountMedCtr", con);
        cmd.Parameters.AddWithValue("@RegionID", Session["RegionID"]);
        cmd.Parameters.AddWithValue("@YYYY", Session["YYYY"]);
        cmd.Parameters.AddWithValue("@StatType_ID", Session["StatType_ID"]);
        cmd.CommandType = CommandType.StoredProcedure;
            grdHighLight.EmptyDataText = "No Records Found";
            grdHighLight.DataSource = cmd.ExecuteReader();
        catch (Exception ex)
            throw ex;
    protected void grdHighLight_RowDataBound(object sender, GridViewRowEventArgs e)
        // this event or elsewhere. This event is better because I can have more control when 
        // there are two grid views on a single form.

        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "headerBracketGround";
        if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.CssClass = "highlightBracketground";
            var val = e.Row.Cells[2].Text;
            var snglVal = Single.Parse(val);
            e.Row.CssClass = snglVal > 0.0 ? "highlightBracketground" : "bracketground";

Open in new window

Mike Eghtebas
Mike Eghtebas
1 Solution
Robert SchuttSoftware EngineerCommented:
Quick and dirty way to do it:
grdHighLight.Caption = new string[] { "Students", "Teacher", "Parent" }[(int)Session["type"] - 1];

Open in new window

Obviously there are better ways to define the array, either in the class, get it from settings or a database table etc.

Also a check to make sure the Session variable contains a sane value should be added although you may already have that at the point where it's being set.

PS: I don't see why this would be done in the DataBound event, would say stick it in LoadData.
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now