Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选中从gridview1到gridview2 ASP.NET C的选定行_C#_Asp.net_Gridview_Checkbox - Fatal编程技术网

C# 选中从gridview1到gridview2 ASP.NET C的选定行

C# 选中从gridview1到gridview2 ASP.NET C的选定行,c#,asp.net,gridview,checkbox,C#,Asp.net,Gridview,Checkbox,正如标题所说,无论如何,我有一个表是sql server产品 其中包含产品id、名称、价格等信息 我所做的是将数据输入到第一个GridView类别中,我想要的是当我选中特定行或多行并单击“选择”按钮时,它将在第二个GridView中显示产品名称和价格 但它所做的是将第二个gridview中的下一个选定项替换为以前选定的项,并仅显示一行而不是多个选定项 有人能帮我吗 这是一个密码 protected void Button1_Click(object sender, EventArgs e)

正如标题所说,无论如何,我有一个表是sql server产品

其中包含产品id、名称、价格等信息

我所做的是将数据输入到第一个GridView类别中,我想要的是当我选中特定行或多行并单击“选择”按钮时,它将在第二个GridView中显示产品名称和价格

但它所做的是将第二个gridview中的下一个选定项替换为以前选定的项,并仅显示一行而不是多个选定项

有人能帮我吗

这是一个密码

protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {

            CheckBox chbox = GridView1.Rows[i].Cells[0].FindControl("CheckBox1") as CheckBox;

            if (chbox.Checked == true)
            {
                string conn = ConfigurationManager.ConnectionStrings["Test_T3ConnectionString2"].ConnectionString;
                SqlConnection con = new SqlConnection(conn);
                string query = "select prod_name,price from products where prod_id = '" + GridView1.Rows[i].Cells[1].Text + "'";
                SqlCommand cmd = new SqlCommand(query, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);

                GridView2.DataSource = dt;
                GridView2.DataBind();
            }

        }
    }

您绑定网格的次数太多

protected void Button1_Click(object sender, EventArgs e) {
List<string> checkedIDs = new List<string>();

 for (int i = 0; i < GridView1.Rows.Count; i++) {

    CheckBox chbox = GridView1.Rows[i].Cells[0].FindControl("CheckBox1") as CheckBox;

    if (chbox.Checked == true)
    {
        checkedIDs.Add("'" + GridView1.Rows[i].Cells[1].Text + "'");
    }

}


        string conn = ConfigurationManager.ConnectionStrings["Test_T3ConnectionString2"].ConnectionString;
        SqlConnection con = new SqlConnection(conn);
        string query = "select prod_name,price from products where prod_id in (" + string.Join(",", checkedIDs.ToArray()) + ")";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        GridView2.DataSource = dt;
        GridView2.DataBind();
}

每次在循环中,您似乎都在同一个GridView2实例上设置新的数据源/数据绑定。。。这将按照你说的做,只剩下列表中的最后一项了。谢谢你,阿米拉姆,它可以工作!!!这意味着我必须先将它们存储在数组中,对吗?是的,你需要先保存它们,但这段代码到底做了什么??Join,,checkedds.ToArray;它正在将列表转换为字符串。如果你有一个a,b和c的列表,你会得到a,b,c。看这里: