asp.net - 使用存储过程过滤 Gridview

标签 asp.net gridview stored-functions

我想过滤 GridView 。我使用存储过程来绑定(bind)数据。

这是我的代码:

 <asp:TextBox ID="txtSearch" runat="server"  ></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="search"/>
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" EditRowStyle-Width="50px"  OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="TaskGridView_RowCancelingEdit" AutoGenerateColumns="False" OnRowUpdating="GridView1_RowUpdating" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10" >
<Columns>
    <asp:TemplateField HeaderStyle-Width="30px" ItemStyle-Width="30px" >
    <ItemTemplate>
        <%# Container.DataItemIndex + 1 %>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Uusername" ReadOnly="true"  HeaderStyle-Height="35px" ItemStyle-Height="30px" HeaderStyle-Width="130px" ItemStyle-Width="130px"  />
    <asp:BoundField DataField="Umail" ReadOnly="true" HeaderStyle-Height="35px" ItemStyle-Height="30px"    HeaderStyle-Width="160px" ItemStyle-Width="160px"   />
    <asp:BoundField DataField="Pname"  ReadOnly="true" HeaderStyle-Height="35px" ItemStyle-Height="30px"    HeaderStyle-Width="100px" ItemStyle-Width="100px" />
    <asp:BoundField DataField="Pfamily"  ReadOnly="true" HeaderStyle-Height="35px" ItemStyle-Height="30px"    HeaderStyle-Width="150px" ItemStyle-Width="150px"  />
    <asp:BoundField DataField="UisActiveMob" ReadOnly="true"  HeaderStyle-Height="35px" ItemStyle-Height="30px"   HeaderStyle-Width="100px" ItemStyle-Width="100px" />

</Columns>

</asp:GridView>

我像这样填充gridview:

 con.Open();
            string query = "getDataProfile";
            SqlCommand com = new SqlCommand(query, con);
            com.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter adapter = new SqlDataAdapter(com);
            DataSet dset = new DataSet();
            adapter.Fill(dset, "t1");
            var result = com.ExecuteReader();
            GridView1.EmptyDataText = "No Records Found";
            GridView1.DataSource = dset.Tables["t1"];
            GridView1.DataBind();

            con.Close();

我知道如果我使用数据源,我必须添加一个像这样的过滤器:

<FilterParameters>
        <asp:ControlParameter ControlID="CountryListBox"   PropertyName="SelectedValue" />
        <asp:ControlParameter ControlID="LastNameTextBox" PropertyName="Text" />
      </FilterParameters>
    </asp:SqlDataSource>

但是我没有使用数据源!如何在我的 gridview 中进行搜索?

最佳答案

您应该更改可以获取参数的存储过程代码。假设您的存储过程是:

//Add @Country and @LastName Parameters to stored procedure.
CREATE PROCEDURE getDataProfile (@Country INT, @LastName NVarchar(50))
As
//Your stored procedure code goes here

注意:您不需要任何SqlDataSource。更改源部分:

 <asp:Button ID="btnSearch" runat="server" Text="search"/>

致:

 <asp:Button ID="btnSearch" OnClick="btnSearch_Click" runat="server" Text="search"/>

另请注意,将连接字符串更改为您的连接字符串。 您的代码必须如下所示:

        protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();

        con.ConnectionString = "Your Connection String";
        con.Open();
        string query = "getDataProfile";
        SqlCommand com = new SqlCommand(query, con);
        com.CommandType = CommandType.StoredProcedure;

        SqlParameter param = com.CreateParameter();
        param.ParameterName = "@Country";
        param.Value = CountryListBox.SelectedValue;
        param.DbType = DbType.Int32;
        com.Parameters.Add(param);

        param = com.CreateParameter();
        param.ParameterName = "@LastName";
        param.Value = LastNameTextBox.Text;
        param.DbType = DbType.String;
        com.Parameters.Add(param);



        SqlDataAdapter adapter = new SqlDataAdapter(com);
        DataSet dset = new DataSet();
        adapter.Fill(dset, "t1");
        var result = com.ExecuteReader();
        GridView1.EmptyDataText = "No Records Found";
        GridView1.DataSource = dset.Tables["t1"];
        GridView1.DataBind();

        con.Close();
    }

关于asp.net - 使用存储过程过滤 Gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20424098/

相关文章:

Android Gridview显示蓝色背景,如何将背景更改为白色?

c# - gridview rowdatabound 事件中的 e.Row.DataItem 错误

sql - 这个 postgresql 查询如何作为表返回?

asp.net - 如何使用 OWIN OAuthBearerAuthentication 验证访问 token ?

c# - 尝试在按钮单击后触发代码中的方法

c# - 捕获 WebAPI 方法调用的响应大小(以字节为单位)

mysql - 需要帮助使用在两个单独表的列之间进行算术运算的函数将列添加到一个表

c# - ASP.NET MVC : No owin. 环境

android - 如何让 GridView 在其 LinearLayout 父级中居中?

MySQL 存储函数返回对象而不是整数