【问题标题】:Getting a dropdown to interact with a gridview获取下拉菜单以与 gridview 交互
【发布时间】:2013-08-13 10:24:33
【问题描述】:

我有一个看起来像这样的网格视图:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  PageSize="10"
        DataKeyNames="id" 
        OnRowEditing="GridView1_RowEditing"
        OnRowCancelingEdit="GridView1_RowCancelingEdit"
        OnRowUpdating="GridView1_RowUpdating"
        OnPageIndexChanging="GridView1_PageIndexChanging"
        OnPageIndexChanged="GridView1_PageIndexChanged"
        AllowPaging="True">

工作正常。我想在 gridview 上方添加一个下拉框,作为数据库结果的过滤器:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
    <asp:ListItem Value="0">Show all</asp:ListItem>
    <asp:ListItem Value="1">In queue</asp:ListItem>
    <asp:ListItem Value="2">Being worked on</asp:ListItem>
    <asp:ListItem Value="3">Complete</asp:ListItem>
    <asp:ListItem Value="4">Declined</asp:ListItem>
</asp:DropDownList>

如何让下拉菜单与 gridview 交互以相应地更新结果?请记住,gridview 已启用分页,因此在单击页面时应记住下拉选项,并且还应根据页码和下拉选择记住数据库结果。

更新:

我在 BindData 进入 Page_Load 的地方按如下方式填充 gridview:

private void BindData()
{

    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connString"]);
    SqlDataAdapter da = new SqlDataAdapter(@"sql query here", con);

    DataTable dt = new DataTable();
    da.Fill(dt);

    GridView1.DataSource = dt;

    GridView1.DataBind();

}

Page_Load 如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindData();
    }
}

【问题讨论】:

  • 如何用数据填充 gridview?
  • @Andrei,我已将更新添加到问题中以回答您的问题。

标签: c# asp.net gridview


【解决方案1】:

在您的BindData() 方法中,如果已选择一个过滤器值,则需要从DropDownList1 传递过滤器值并相应地修改SQL。您可能还需要在 SQL 语句中传递请求的页码,以便可以在您的SELECT 中过滤结果(取决于您如何进行分页,因为您没有说明)。

然后在您的AutoPostBack 方法中为DropDownList1 调用BindData()

这样每次更改下拉列表值时,它都会调用您的BindData() 方法并传递任何相关过滤器和页码以获得相关结果。

【讨论】:

    【解决方案2】:

    可以在 DropDownList1 的 indexchnage 事件中编写如下代码

    DataView dataView = dt.DefaultView;
    dataView.RowFilter = "NewsDate2 = '" + DropDownList1.SelectedValue + "'";
    
    GridView1.DataSource = dataView;
    GridView1.DataBind();
    

    【讨论】:

      猜你喜欢
      • 2013-02-13
      • 1970-01-01
      • 2022-01-20
      • 2022-07-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多