C# 如何将签入行添加到数据表中
我正在网格视图(gvrow)的“单击事件”复选框中编写代码。使用以下代码 但我的检查行应该单独进入数据表(dt)。若检查后有任何未检查的行,它不应该进入数据表 下面显示的代码是添加行,但不是选中行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
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);