如何从文本框中的存储过程中过滤datagridview [英] how to filter datagridview from stored procedures from textbox

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

问题描述

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

someone pls give me a related sample code

推荐答案

您需要先自己尝试.

请执行以下步骤:
1.设计一个放置少量搜索参数的表单.例如,这里为您-从日期,日期日历和ID文本框.
2.现在,放置一个搜索按钮作为搜索的触发控件.在搜索按钮的单击事件中,您将在编写逻辑.
3.单击搜索按钮,获取搜索参数(如文本框中的ID,日历中的日期),使用它,形成查询,将其作为存储过程存储在数据库中并在数据库上执行
4.将SP结果返回到数据表中,然后根据需要将其绑定到网格或任何其他适当的控件中
5.现在,Datagrid将显示检索到的结果.

完成!
You need to try first by yourself.

Follow these steps:
1. Design a form where you place few search parameters. For example, here for you - from date, to date calendars & a textbox for ID.
2. Now, place a search button as the trigger control of the search. On click event of search button you would be writing your logic.
3. In search button click, get the search parameters (like the id in textbox, dates in calendar), use it, form a query, put it in database as Stored Procedure and execute on database
4. Get the SP result back in a datatable and bind it to a grid or any other appropriate control based on your needs
5. For now, Datagrid will display the retrieved result.

Done!



检查此
Hi ,
Check this
    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


坦率地说,这是一项很棒的工作,但是我想知道有关我们创建的存储过程的代码
frankly it''s a great job , but i want to know the code about the stored procedure that we created


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆