Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

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.
   <div>
------------------------- aspx -----------------------                                   
   <asp:GridView runat="server" ID="grdHighLight"  caption="Students" onrowdatabound="grdHighLight_RowDataBound" CellSpacing="-1" GridLines="None" >
            
    </asp:GridView>

------------------------- 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");
            LoadData();
            //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;
        try
        {
            con.Open();
            grdHighLight.EmptyDataText = "No Records Found";
            grdHighLight.DataSource = cmd.ExecuteReader();
            grdHighLight.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
    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

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

Thank you.