根据另一个gridview中的检查值过滤gridview

时间:2018-02-14 18:43:04

标签: c# asp.net gridview

我有两个GridView。首先称为" gvImage"这是类别gridview,带有复选框供选择。第二个GridView被称为" GridView1",第二个GridView根据第一个GridView中选择的值进行填充,这里我能够在第二个GridView中填充记录,仅用于从第一个GridView中检查(选择)的一个类别,但是当我从第一个GridView(gvImage)中检查(选择)多个类别时,它不会在第二个GridView(GridView1)中填充多个类别记录。请帮我搞定,下面是我的代码背后:

 protected void btn_Submit_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvImage.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
                    if (isChecked)
                    {
                        String strConnString = ConfigurationManager.ConnectionStrings["CONNECTION1"].ConnectionString;
                        SqlConnection con = new SqlConnection(strConnString);
                        string str = "SELECT  * FROM AllProduct WHERE PId =@PId";
                        SqlCommand cmd = new SqlCommand(str, con);
                        cmd.Parameters.AddWithValue("@PId", row.Cells[1].Controls.OfType<TextBox>().FirstOrDefault().Text);
                        //this.ExecuteQuery(cmd, "SELECT");
                        DataSet objDs = new DataSet();
                        SqlDataAdapter dAdapter = new SqlDataAdapter();
                        dAdapter.SelectCommand = cmd;
                        con.Open();
                        dAdapter.Fill(objDs);
                        con.Close();
                        if (objDs.Tables[0].Rows.Count > 0)
                        {
                            cmd.CommandType = CommandType.Text;
                            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                            {
                                using (DataTable dt = new DataTable())
                                {
                                    sda.Fill(dt);
                                    GridView1.DataSource = dt;
                            GridView1.DataBind();
                            GridView1.Visible = true;
                            GridView1.Enabled = true;
                                }
                            }

                        }
                        else if (objDs.Tables[0].Rows.Count == 0)
                        {
                            GridView1.DataSource = null;
                            GridView1.Visible = false;
                        }

                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

问题是你为每个选中的复选框绑定了第二个网格,它基本上总是替换已绑定的数据,因此你只能看到最后一个选中复选框的结果。你需要结合结果并在foreach之外绑定

protected void btn_Submit_Click(object sender, EventArgs e)
{
    DataTable combinedDataTable;
    foreach (GridViewRow row in gvImage.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
            if (isChecked)
            {
                DataTable data = //Data from database for this checkbox
                if(combinedDataTable != null)
                    combinedDataTable.Merge(data);
                else
                    combinedDataTable = data;
            }
        }
    }
    GridView1.DataSource = combinedDataTable;
    GridView1.DataBind();
}

甚至更好,只需收集首先检查的复选框,然后在一个查询中获取结果:

protected void btn_Submit_Click(object sender, EventArgs e)
{
    List<string> selectedCheckboxes = new List<string>()
    foreach (GridViewRow row in gvImage.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {   
            bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
            if (isChecked)
            {
                selectedCheckboxes.Add(row.Cells[1].Controls.OfType<TextBox>().FirstOrDefault().Text);
            }
        }
    }

    String strConnString = ConfigurationManager.ConnectionStrings["CONNECTION1"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    string str = "SELECT  * FROM AllProduct WHERE PId in ";
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    List<string> parametersNames = new List<string>();
    for (int i = 0; i < selectedCheckboxes.Count; i++)
    {
        var parameterName = "@PId" + i;
        cmd.Parameters.AddWithValue(parameterName, selectedCheckboxes[i]);
        parametersNames.Add(parameterName);
    }
    str += "(" + String.Join(",", parametersNames) + ")";
    str += " AND TransactionDate BETWEEN @From AND @To";
    cmd.Parameters.AddWithValue("@From", DateTime.Parse(txtFrom.Text));
    cmd.Parameters.AddWithValue("@To", DateTime.Parse(txtTo.Text));
    cmd.CommandText = str;
    DataSet objDs = new DataSet();
    SqlDataAdapter dAdapter = new SqlDataAdapter();
    dAdapter.SelectCommand = cmd;
    con.Open();
    dAdapter.Fill(objDs);
    con.Close();
    if (objDs.Tables[0].Rows.Count > 0)
    {
        cmd.CommandType = CommandType.Text;
        using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
        {
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                GridView1.Visible = true;
                GridView1.Enabled = true;
            }
        }

    }
    else if (objDs.Tables[0].Rows.Count == 0)
    {
        GridView1.DataSource = null;
        GridView1.Visible = false;
    }
}