user1036322
user1036322

Reputation: 1

Filtering a GridView with drop downs

I've been stuck on this for too long, and my Google-Fu is failing me. I'm new to C# and .Net, so I'm getting pretty frustrated here.

Here's what I have so far:

A method called GetData populates a DataSet, then I create a DataTable from that.

I bind the DataTable to the GridView, which is then made sortable. The table and sorting work fine, but I need to have drop down filtering on a few of the columns, but I can't get anything to work.

My ASP:

<asp:GridView id="gvEvaluator" Runat="server" Width="750" tooltip="Evaluator Status"     AutoGenerateColumns="False" 
EnableViewState="true" 
HeaderStyle-ForeColor="#000000"
HeaderStyle-BackColor="#CCCCCC" 
FooterStyle-ForeColor="#000000" 
FooterStyle-BackColor="#CCCCCC" 
Font-Size="8pt" 
Font-Names="Verdana" 
CellSpacing="0" 
CellPadding="3" 
ShowFooter="true" 
AllowSorting="true" 
GridLines="Both" 
BorderColor="#ffffff" 
BackColor="#ffffff"
ItemStyle-HorizontalAlign="Left"
visible="true"
AllowPaging="false"
AllowCustomPaging="false"
OnSorting="GridView_Sorting">

    <Columns>
        <asp:TemplateField HeaderText="<strong>Term</strong>"
            HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Bottom"
            ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="top" >
                <HeaderTemplate>
                    <asp:DropDownList ID="ddlTerm"
                        runat="server"
                        visible="true"
                        OnSelectedIndexChanged="ddlTermChanged"
                        AutoPostBack="true"
                        DataSourceID="gvEvaluator">
                    </asp:DropDownList>
                </HeaderTemplate>
        </asp:TemplateField>
<asp:BoundField DataField="Dept" HeaderText="Dept" SortExpression="Dept></asp:BoundField>
<asp:BoundField DataField="Course" HeaderText="Course" SortExpression="Course"></asp:BoundField>
<asp:BoundField DataField="Section" HeaderText="Section"></asp:BoundField>
<asp:BoundField DataField="Evaluator" HeaderText="Evaluator" SortExpression="Evaluator"></asp:BoundField>
<asp:BoundField DataField="Type" HeaderText="Evaluator Type"></asp:BoundField>
<asp:BoundField DataField="Email_Address" Visible="false"></asp:BoundField>
<asp:BoundField DataField="Days_Since_Login" HeaderText="Days Since Login"></asp:BoundField>
<asp:BoundField DataField="Required_Work" HeaderText="Required Work" SortExpression="Required_Work"></asp:BoundField>
<asp:BoundField DataField="Total_Assigned" HeaderText="Total Assigned" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"></asp:BoundField>
<asp:BoundField DataField="Total_Not_Started" HeaderText="Total Not Started" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"></asp:BoundField>
<asp:BoundField DataField="Total_in_Progress" HeaderText="Total in Progress" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"></asp:BoundField>
<asp:BoundField DataField="Total_Complete" HeaderText="Total Complete" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"></asp:BoundField>
<asp:BoundField DataField="eval_id" Visible="false"></asp:BoundField>
<asp:TemplateField HeaderText="<strong>Need Reminder<strong>" ItemStyle-Width="250px">
                  <ItemTemplate>
                  <label for="hyplEvaluator" class="hide">Email Evaluator</label> 
                  <asp:HyperLink ID="hyplEvaluator" runat="server" CssClass="BodyLink" Text='<%# DataBinder.Eval(Container, "DataItem.Need_Reminder")%>' NavigateUrl='' />
                  </ItemTemplate>
                  </asp:TemplateField>
</Columns>
</asp:GridView>

And my C#:

protected void getEvaluatorStatus()
    {
        try
        {
            ds = GetData();
            DataTable myTable = ds.Tables[0];
            if (myTable.Rows.Count != 0)
            {
                gvEvaluator.DataSource = ds;
                gvEvaluator.DataBind();
                gvEvaluator.Visible = true;
                lblNoAssignment.Visible = false;
            }
            else
            {
                lblNoAssignment.Visible = true;
                gvEvaluator.Visible = false;
            }

            //Adds attributes to hyplEmailContact

            for (int i = 0; i < gvEvaluator.Rows.Count; i++)
            {
                string inSenderID = Convert.ToString(meth.decrypt(Session["UserID"].ToString(), Convert.ToString(Session["University"]), Convert.ToString(Session["Department"])));
                string inRecptID = Convert.ToString(gvEvaluator.Rows[i].Cells[10].Text);

                //custom string of attributes above
                string customStr = "inSenderID=" + inSenderID + ",inRecptID=" + inRecptID;

                //Adds the NavigateURL for Contact command to pass variables/attributes 
                HyperLink hyplEmailContact = (HyperLink)gvEvaluator.Rows[i].FindControl("hyplEvaluator");
                hyplEmailContact.NavigateUrl = "javascript:openEmailGeneral(" + customStr + ")";

            } //End for loop 

        }
        catch (Exception ex)
        {
            Session["Error_Code"] = ex;
            Response.Redirect("../Error.aspx");
        }

I'm just a lowly bug squasher, so the only code I personally wrote was creating the GridView (from a DataGrid), the GetData method, making it sortable, and making the data exportable.

Upvotes: 0

Views: 1457

Answers (1)

Icarus
Icarus

Reputation: 63956

Something like this should work:

  1. Handle the ddlTermChanged changed event:
  2. Grab the new selected value in the dropdown list as so

    protected void ddlTermChanged(Object sender, EventArgs e) {
        var newValue = ddlTerm.SelectedValue;
        //see step 3 below
    }
    
  3. Now filter the data and rebind it to the Gridview; something like:

    protected void ddlTermChanged(Object sender, EventArgs e) {
        var newValue = ddlTerm.SelectedValue;
        DataTable t=  GetDataByID(newValue);
        gvEvaluator.DataSource=t;
        gvEvaluator.DataBind();
    }
    

On a separate note, all the transformations you are doing on the Gridview inside the getEvaluatorStatus method should have been handled in the OnRowDataBound event. By doing it the way you did it, every time you rebind the data (as in the case of filtering) you'll have to repeat the code inside the getEvaluatorStatus to do the transformations again. If you do it OnRowDataBound you won't have to repeat code as the event is raised for every row as it is being bound.

Upvotes: 1

Related Questions