title: Filtering a GridView with drop downs
body: <p>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.</p>
  
  <p>Here's what I have so far:</p>
  
  <p>A method called GetData populates a DataSet, then I create a DataTable from that.</p>
  
  <p>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.</p>
  
  <p>My ASP:</p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="&lt;strong&gt;Term&lt;/strong&gt;"
              HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Bottom"
              ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="top" &gt;
                  &lt;HeaderTemplate&gt;
                      &lt;asp:DropDownList ID="ddlTerm"
                          runat="server"
                          visible="true"
                          OnSelectedIndexChanged="ddlTermChanged"
                          AutoPostBack="true"
                          DataSourceID="gvEvaluator"&gt;
                      &lt;/asp:DropDownList&gt;
                  &lt;/HeaderTemplate&gt;
          &lt;/asp:TemplateField&gt;
  &lt;asp:BoundField DataField="Dept" HeaderText="Dept" SortExpression="Dept&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Course" HeaderText="Course" SortExpression="Course"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Section" HeaderText="Section"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Evaluator" HeaderText="Evaluator" SortExpression="Evaluator"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Type" HeaderText="Evaluator Type"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Email_Address" Visible="false"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Days_Since_Login" HeaderText="Days Since Login"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Required_Work" HeaderText="Required Work" SortExpression="Required_Work"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Total_Assigned" HeaderText="Total Assigned" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Total_Not_Started" HeaderText="Total Not Started" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Total_in_Progress" HeaderText="Total in Progress" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="Total_Complete" HeaderText="Total Complete" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:BoundField DataField="eval_id" Visible="false"&gt;&lt;/asp:BoundField&gt;
  &lt;asp:TemplateField HeaderText="&lt;strong&gt;Need Reminder&lt;strong&gt;" ItemStyle-Width="250px"&gt;
                    &lt;ItemTemplate&gt;
                    &lt;label for="hyplEvaluator" class="hide"&gt;Email Evaluator&lt;/label&gt; 
                    &lt;asp:HyperLink ID="hyplEvaluator" runat="server" CssClass="BodyLink" Text='&lt;%# DataBinder.Eval(Container, "DataItem.Need_Reminder")%&gt;' NavigateUrl='' /&gt;
                    &lt;/ItemTemplate&gt;
                    &lt;/asp:TemplateField&gt;
  &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>And my C#:</p>
  
  <pre><code>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 &lt; 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");
          }
  </code></pre>
  
  <p>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.</p>