【发布时间】: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 谢谢,我没有那样尝试。你能输入吗,因为我没有经历过。