C#Gridview搜索

时间:2017-01-29 08:43:22

标签: asp.net webforms

专家我的搜索功能不起作用。不知道我哪里出错了。

我的代码是

public partial class _Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=foc; Trusted_Connection=yes;");
protected void Page_Load(object sender, EventArgs e)
{       
    if (!IsPostBack)
    {            
        BindGrid();
        Button1_Click(Button1, null);
        TextBox1_TextChanged(TextBox1, null);
    }
}

protected void BindGrid()
{
    DataSet ds = new DataSet();        
    conn.Open();
    string cmdstr = "SELECT MIN([sddoc]) AS[sddoc],[Soldtopt],  [tradingname],  [REP],MIN([DELTXT]) AS[Preseller Text], SUM(try_cast([Orderqty] as float)) AS[Orderqty],count(distinct Orderqty) as Qtycount ,  [DlvDate], SUM(try_cast(Netvalue as float)) as Netvalue, count(distinct SDDoc) as Salesdoc , count(distinct case when Netvalue = '0' then 1 else null end) as ZeroValue ,  count(distinct SDDoc) - count(distinct case when Netvalue = '0' then 1 else null end) As Result FROM [FOC].[dbo].[foc] where  dlvdate = @dlvdate GROUP by Soldtopt,tradingname,REP,DlvDate HAVING SUM(try_cast(Netvalue as float)) = 0 and count(distinct SDDoc) = 1  and count(distinct case when Netvalue = '0' then 1 else null end) = 1";

    SqlCommand cmd = new SqlCommand(cmdstr, conn);
    cmd.Parameters.AddWithValue("@dlvdate", TextBox1.Text);
    cmd.ExecuteNonQuery();
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    adp.Fill(ds);
    conn.Close();
    gvEmployeeDetails.DataSource = ds;
    gvEmployeeDetails.DataBind();

}
protected void gvEmployeeDetails_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lblEmpID = (Label)e.Row.FindControl("lblEmpID");          
        GridView gv_Child = (GridView)e.Row.FindControl("gv_Child");

        string txtempid = lblEmpID.Text;

        DataSet ds = new DataSet();
        conn.Open();
        string cmdstr = "Select * from [FOC].[dbo].[foc] where sddoc=@sddoc";
        SqlCommand cmd = new SqlCommand(cmdstr, conn);
        cmd.Parameters.AddWithValue("@sddoc", txtempid);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(ds);
        cmd.ExecuteNonQuery();
        conn.Close();
        gv_Child.DataSource = ds;
        gv_Child.DataBind();

    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过点击按钮来绑定网格来解决问题。