Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Header filters and conditional formatting

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
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&nbsp;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
 
Have you traced through your code. I'm sure if you do you will find the problem.
 
I ended up handling the NULL values and then everything worked great, both the conditional formatting and the header filtering.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top