INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Header filters and conditional formatting

Header filters and conditional formatting

(OP)
I have this code in my aspx page:

CODE

<asp:TemplateField ItemStyle-Wrap="false">
    <HeaderTemplate>
        Company Name:
        <asp:DropDownList ID="ddlCompany" runat="server" OnSelectedIndexChanged="Company_SelectedIndexChanged" AutoPostBack="true" AppendDataBoundItems="true">
            <asp:ListItem Text="All" Value="All"></asp:ListItem>
        </asp:DropDownList>
    </HeaderTemplate>
    <ItemTemplate>
        <asp:HyperLink ID="CompanyName" runat="server" NavigateUrl='<%#Eval("URL")%>' Text='<%#Eval("Company Name") %>' SortExpression="Company Name"></asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
    <HeaderTemplate>
        Syspro Code:
        <asp:DropDownList ID="ddlSyspro" runat="server" OnSelectedIndexChanged="Syspro_SelectedIndexChanged" AutoPostBack="true" AppendDataBoundItems="true">
            <asp:ListItem Text="All" Value="All"></asp:ListItem>
        </asp:DropDownList>
    </HeaderTemplate>
    <ItemTemplate >
        <asp:Label ID="lblSysproCode" runat="server" Text='<%# Eval("Syspro Code") %>' />
    </ItemTemplate>
</asp:TemplateField> 

And then in the code behind:

CODE

protected void gvCredit_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        DropDownList ddlCompany;
        ddlCompany = (DropDownList)e.Row.FindControl("ddlCompany");
        if (!(ViewState["FilterCompany"] == null))
        {
            ddlCompany.SelectedValue = (string)ViewState["FilterCompany"];
        }
        else
        {
            ddlCompany.SelectedIndex = 0;
        }
        DropDownList ddlSyspro;
        ddlSyspro = (DropDownList)e.Row.FindControl("ddlSyspro");
        if (!(ViewState["FilterSyspro"] == null))
        {
            ddlSyspro.SelectedValue = (string)ViewState["FilterSyspro"];
        }
        else
        {
            ddlSyspro.SelectedIndex = 0;
        }
    }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int? pdx = Convert.ToInt16(e.Row.Cells[10].Text);
                if (pdx != null)
                {
                    if (pdx < 55)
                    {
                        e.Row.Cells[10].BackColor = System.Drawing.Color.FromName("#DC143C"); // This will make row back color red 
                    }
                    else if (pdx < 70)
                    {
                        e.Row.Cells[10].BackColor = System.Drawing.Color.FromName("#FFFF99"); // This will make row back color yellow
                    }
                    else if (pdx >= 70)
                    {
                        e.Row.Cells[10].BackColor = System.Drawing.Color.FromName("#CCFFCC"); // This will make row back color mintgreen; blue is 99CCFF, c6efce
                    }
                }
                else
                {
                    e.Row.Cells[10].BackColor = System.Drawing.Color.FromName("#DC143C"); // This will make row back color red 

                }
            }
}

protected void Company_SelectedIndexChanged(object sender, EventArgs e)
{
    ViewState["FilterCompany"] = null;
    DropDownList ddlCompany = (DropDownList)sender;
    ViewState["FilterCompany"] = ddlCompany.SelectedValue.ToString();
    this.BindGridView();
}

protected void Syspro_SelectedIndexChanged(object sender, EventArgs e)
{
    ViewState["FilterSyspro"] = null;
    DropDownList ddlSyspro = (DropDownList)sender;
    ViewState["FilterSyspro"] = ddlSyspro.SelectedValue.ToString();
    this.BindGridView();
}

private void BindCompanyList(DropDownList ddlCompany)
{
    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["credit"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    SqlDataAdapter sda = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand("SELECT DISTINCT [Company Name] FROM [CreditReports].[dbo].[Customers]");
    cmd.Connection = con;
    con.Open();
    ddlCompany.DataSource = cmd.ExecuteReader();
    ddlCompany.DataTextField = "Company Name";
    ddlCompany.DataValueField = "Company Name";
    ddlCompany.DataBind();
    con.Close();
    ddlCompany.Items.FindByValue(ViewState["FilterCompany"].ToString()).Selected = true;
}

private void BindSysproList(DropDownList ddlSyspro)
{
    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["credit"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    SqlDataAdapter sda = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand("SELECT DISTINCT [Syspro Code] FROM [CreditReports].[dbo].[Customers]");
    cmd.Connection = con;
    con.Open();
    ddlSyspro.DataSource = cmd.ExecuteReader();
    ddlSyspro.DataTextField = "Syspro Code";
    ddlSyspro.DataValueField = "Syspro Code";
    ddlSyspro.DataBind();
    con.Close();
    ddlSyspro.Items.FindByValue(ViewState["FilterSyspro"].ToString()).Selected = true;
} 

The issue I am running into is the section here:

CODE

if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int? pdx = Convert.ToInt16(e.Row.Cells[10].Text);
                if (pdx != null)
                {
                    if (pdx < 55)
                    {
                        e.Row.Cells[10].BackColor = System.Drawing.Color.FromName("#DC143C"); // This will make row back color red 
                    }
                    else if (pdx < 70)
                    {
                        e.Row.Cells[10].BackColor = System.Drawing.Color.FromName("#FFFF99"); // This will make row back color yellow
                    }
                    else if (pdx >= 70)
                    {
                        e.Row.Cells[10].BackColor = System.Drawing.Color.FromName("#CCFFCC"); // This will make row back color mintgreen; blue is 99CCFF, c6efce
                    }
                }
                else
                {
                    e.Row.Cells[10].BackColor = System.Drawing.Color.FromName("#DC143C"); // This will make row back color red 

                }
            } 

which is breaking in two ways. 1) When it hits a null value it stops 2) when I enable the conditional formatting the drop down list for filtering by column are empty. If I remove the conditional formatting piece, everything works great, but my GM wants it to look like his Excel sheet. Is that enough info?

Any thoughts?

Thanks,
Willie

RE: Header filters and conditional formatting

Have you traced through your code. I'm sure if you do you will find the problem.

RE: Header filters and conditional formatting

(OP)
I ended up handling the NULL values and then everything worked great, both the conditional formatting and the header filtering.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close