【问题标题】:Filter gridview based on the checked value from the another gridview根据来自另一个 gridview 的检查值过滤 gridview
【发布时间】:2018-02-14 18:43:03
【问题描述】:

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

                    }
                }
            }
        }

【问题讨论】:

  • 请发布您的所有代码
  • 您是否尝试将您对第二个网格的查询更改为类似 [where columnName IN (@yourvariables) 的查询?
  • @briskovich 谢谢,我没有那样尝试。你能输入吗,因为我没有经历过。

标签: c# asp.net gridview


【解决方案1】:

问题是您为每个选中的复选框绑定了第二个网格,这基本上总是替换已绑定的数据,因此您只能看到最后选中的复选框的结果。需要合并结果并在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;
    }
}

【讨论】:

  • @hankor,谢谢。我尝试了您的第二个选项,但抛出一个错误:System.Data.SqlClient.SqlParemterCollection 不包含“选择”的定义(您是否缺少 using 指令或程序集引用?)。它在这一行中引发错误: str += "(" + String.Join(",", cmd.Parameters.Select(x => x.Name)) + ")";
  • hankor,谢谢它的工作!但也想知道我是否必须在 texbox 的两个日期之间选择这些记录,我将如何放置查询?这是查询:WHERE TransactionDate BETWEEN @ From AND @ To
  • @Turing:不确定我是否理解。您可以将其附加到查询中。我还在上面的示例中添加了它
猜你喜欢
  • 2012-05-02
  • 2021-05-21
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 2019-01-04
  • 1970-01-01
相关资源
最近更新 更多