如何从文本框中的存储过程中过滤datagridview

如何从文本框中的存储过程中过滤datagridview

本文介绍了如何从文本框中的存储过程中过滤datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人请给我一个相关的示例代码

someone pls give me a related sample code

推荐答案


    protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        //Bring all Data in First load
        Bind();
    }
    else
    {
        if (ViewState["dt"] != null)
        {
            dt = (DataTable)ViewState["dt"];
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

}
DataTable dt = new DataTable();
DataTable search(int id)
{
    using (
          SqlConnection con =
              new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
    {

        SqlCommand cmd = new SqlCommand("usp_Search_All", con);

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("id", id);
        SqlDataAdapter adpt = new SqlDataAdapter(cmd);
        adpt.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        cmd.Dispose();
        return dt;

    }
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataSource =(DataTable)ViewState["dt"];
    GridView1.DataBind();

}
protected void Button1_Click(object sender, EventArgs e)
{
    dt = search(Convert.ToInt32(TextBox1.Text));
    ViewState.Add("dt", dt);
}
void Bind()
{
    using (
          SqlConnection con =
              new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
    {

        SqlCommand cmd = new SqlCommand("usp_Select_All", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter adpt = new SqlDataAdapter(cmd);
        adpt.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        cmd.Dispose();


    }
}


<div>


<asp:GridView ID="GridView1" runat="server" AllowPaging="True"

            onpageindexchanging="GridView1_PageIndexChanging" PageSize="5">
        </asp:GridView>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>



最好的问候
M.Mitwalli



Best Regards
M.Mitwalli



这篇关于如何从文本框中的存储过程中过滤datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 19:26