【问题标题】:Checked gridview contents in datatable检查数据表中的gridview内容
【发布时间】:2017-03-15 04:33:07
【问题描述】:

我需要在单击按钮时将所有复选框选中的 gridview 行的特定列存储在数据表中。我试过这段代码。但是数据表 dt 和字符串变量 str 中没有存储任何内容。如何实现我的要求..请帮助...

Protected void btnmail_click(object sender EventArgs e)
{
    datatable dt = new DataTable();
    foreach (GridViewRow gvrow in grd.Rows)
    {
        Checkbox chk = (Checkbox)gvrow.FindControl("chkrow");
        if (chk.Checked == true)
        {
            string str = gvrow.Cells[1].Text;
            dt.Rows.Add(str);
        }
    }
}

【问题讨论】:

  • 您正在尝试使用string 添加Row。您需要添加一个DataRow 对象。查看示例in the documentation

标签: c# asp.net checkbox datatable aspxgridview


【解决方案1】:

你可以使用这个sn-p。首先,您需要创建一个 DataTable 并向其中添加 Columns。然后您可以将选中的行添加到 DataTable。

protected void Button1_Click(object sender, EventArgs e)
{
    //create a datatable
    DataTable table = new DataTable();

    //add one or more colums to the datatable
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Text", typeof(string));

    //loop the gridview rows
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

        if (chk.Checked == true)
        {
            string str = GridView1.Rows[i].Cells[1].Text;

            //add a new row to the datatable
            table.Rows.Add(i, str);
        }
    }
}

附: C# 区分大小写,datatable dtCheckbox chk 永远不会起作用。您使用的是 Visual Studio 等编辑器吗?

【讨论】:

  • 我试过这个。但它不起作用...变量 str 保持为空,并且所需的值未存储在数据表中
猜你喜欢
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
  • 2013-09-12
  • 2020-10-09
  • 1970-01-01
  • 2012-02-01
  • 2014-07-05
  • 1970-01-01
相关资源
最近更新 更多