dynamic gridview caption, c# codebehind

Posted on 2014-08-27
Last Modified: 2014-08-27
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

Question by:Mike Eghtebas
    LVL 35

    Accepted Solution

    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.
    LVL 33

    Author Comment

    by:Mike Eghtebas
    Thank you.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now