使用下拉列表过滤GridView

时间:2012-02-06 19:49:22

标签: c# asp.net drop-down-menu

我被困在这个问题太久了,我的Google-Fu让我失望了。我是C#和.Net的新手,所以我在这里感到很沮丧。

这是我到目前为止所拥有的:

一个名为GetData的方法填充一个DataSet,然后我从那里创建一个DataTable。

我将DataTable绑定到GridView,然后将其排序。表格和排序工作正常,但我需要对一些列进行下拉过滤,但我无法正常工作。

我的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>

我的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");
        }

我只是一个卑微的bug squasher,所以我个人编写的唯一代码是创建GridView(来自DataGrid),GetData方法,使其可排序,并使数据可导出。

1 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

  1. 处理ddlTermChanged已更改的事件:
  2. 在下拉列表中抓取新选中的值

    protected void ddlTermChanged(Object sender, EventArgs e) {
        var newValue = ddlTerm.SelectedValue;
        //see step 3 below
    }
    
  3. 现在过滤数据并将其重新绑定到Gridview;类似的东西:

    protected void ddlTermChanged(Object sender, EventArgs e) {
        var newValue = ddlTerm.SelectedValue;
        DataTable t=  GetDataByID(newValue);
        gvEvaluator.DataSource=t;
        gvEvaluator.DataBind();
    }
    
  4. 另外,在getEvaluatorStatus方法中,您在Gridview上进行的所有转换都应该在OnRowDataBound事件中处理。通过按照您的方式执行操作,每次重新绑定数据时(如在过滤的情况下),您将不得不重复getEvaluatorStatus内的代码再次进行转换。如果你这样做OnRowDataBound,你将不必重复代码,因为在绑定时为每一行引发了事件。