根据另一个GridView的检查值过滤verview



我有两个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;
                        }
                    }
                }
            }
        }

问题是您将每个选定的复选框的第二个网格绑定,该网格本质上将始终替换已绑定的数据,因此您只能看到上一个选定的复选框的结果。您需要将结果结合起来并绑定在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;
    }
}

最新更新