Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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# 如何将签入行添加到数据表中_C#_Asp.net - Fatal编程技术网

C# 如何将签入行添加到数据表中

C# 如何将签入行添加到数据表中,c#,asp.net,C#,Asp.net,我正在网格视图(gvrow)的“单击事件”复选框中编写代码。使用以下代码 但我的检查行应该单独进入数据表(dt)。若检查后有任何未检查的行,它不应该进入数据表 下面显示的代码是添加行,但不是选中行 protected void chkCall_CheckedChanged(object sender, EventArgs e) { foreach (GridViewRow gvrow in gvDetails.Rows) { CheckBox chk = (Che

我正在网格视图(gvrow)的“单击事件”复选框中编写代码。使用以下代码

但我的检查行应该单独进入数据表(dt)。若检查后有任何未检查的行,它不应该进入数据表

下面显示的代码是添加行,但不是选中行

protected void chkCall_CheckedChanged(object sender, EventArgs e)
{
    foreach (GridViewRow gvrow in gvDetails.Rows)
    {
        CheckBox chk = (CheckBox)gvrow.FindControl("chkCall");
        if (chk != null & chk.Checked)
        {

            //dt.Rows.Add();
            DataRow row = dt.NewRow();
            row["shopno"] = gvDetails.Rows[i].Cells[0].Text.ToString();
            row["Lineitem"] = gvDetails.Rows[i].Cells[1].Text;
            row["Suppliername"] = gvDetails.Rows[i].Cells[2].Text;
            row["Dunsnumber"] = gvDetails.Rows[i].Cells[3].Text;
            row["AgingDays"] = gvDetails.Rows[i].Cells[4].Text;
            // row["lastfollowupmail"] = gvDetails.Rows[i].Cells[7].Text;
            dt.Rows.Add(row);
            i++;
        }

    }

    GridView1.DataSource = dt;
    GridView1.DataBind();

}

请帮助我将您的
i++
置于复选框之外

protected void chkCall_CheckedChanged(object sender, EventArgs e)
{
    int i=0;
    foreach (GridViewRow gvrow in gvDetails.Rows)
    {
        CheckBox chk = (CheckBox)gvrow.FindControl("chkCall");
        if (chk != null & chk.Checked)
        {

            //dt.Rows.Add();
            DataRow row = dt.NewRow();
            row["shopno"] = gvDetails.Rows[i].Cells[0].Text.ToString();
            row["Lineitem"] = gvDetails.Rows[i].Cells[1].Text;
            row["Suppliername"] = gvDetails.Rows[i].Cells[2].Text;
            row["Dunsnumber"] = gvDetails.Rows[i].Cells[3].Text;
            row["AgingDays"] = gvDetails.Rows[i].Cells[4].Text;
            // row["lastfollowupmail"] = gvDetails.Rows[i].Cells[7].Text;
            dt.Rows.Add(row);

        }
        i++;

    }

    GridView1.DataSource = dt;
    GridView1.DataBind();

}

你可以试试这个,而不是你的代码

假设您的复选框列名为ChkBox:

DataTable tbl=((DataTable)GridView1.DataSource).Clone()//克隆结构优先
var rows=dataGridView1.rows.OfType()
.Where(r=>Convert.ToBoolean(r.Cells[“ChkBox”].Value))
.选择(r=>((DataRowView)r.DataBoundItem).Row);
foreach(行中的变量行)
tbl.进口(世界其他地区);
  DataTable tbl = ((DataTable)GridView1.DataSource).Clone();//Clone structure first
  var rows = dataGridView1.Rows.OfType<GridViewRow>()
                         .Where(r=>Convert.ToBoolean(r.Cells["ChkBox"].Value))
                         .Select(r=>((DataRowView)r.DataBoundItem).Row);
  foreach(var row in rows)
  tbl.ImportRow(row);