数据表行.IndexOf()

发布于 2024-08-05 22:39:15 字数 815 浏览 13 评论 0原文

我试图找到一行,然后从数据表中删除该行。我不断得到 nIndex = -1。有人有建议吗?

protected void cbxSelected_CheckedChanged(object sender, EventArgs e)
{
 CheckBox checkbox = (CheckBox)sender;
 GridViewRow row = (GridViewRow)checkbox.NamingContainer;
 string sSampleId = row.Cells[1].Text;

 if (!string.IsNullOrEmpty(sSampleId))
 {
  DataTable dt;

  if (ViewState["dtTemp"] != null)
  {
   dt = (DataTable)ViewState["dtTemp"];
  }
  else
  {
   dt = new DataTable();
   dt.Columns.Add("sample_id");
  }

  DataRow dr;
  string[] drow = new string[] { sSampleId };
  dr = dt.NewRow();
  dr.ItemArray = drow;

  if (checkbox.Checked == true)
  {
   dt.Rows.Add(dr);
  }
  else if (checkbox.Checked == false)
  {
   int nIndex = dt.Rows.IndexOf(dr);
   dt.Rows.RemoveAt(nIndex);
  }

  ViewState.Add("dtTemp", dt);
 }
}

I am trying to find a row and then delete that row from a datatable. I keep getting nIndex = -1. Anyone have suggestions?

protected void cbxSelected_CheckedChanged(object sender, EventArgs e)
{
 CheckBox checkbox = (CheckBox)sender;
 GridViewRow row = (GridViewRow)checkbox.NamingContainer;
 string sSampleId = row.Cells[1].Text;

 if (!string.IsNullOrEmpty(sSampleId))
 {
  DataTable dt;

  if (ViewState["dtTemp"] != null)
  {
   dt = (DataTable)ViewState["dtTemp"];
  }
  else
  {
   dt = new DataTable();
   dt.Columns.Add("sample_id");
  }

  DataRow dr;
  string[] drow = new string[] { sSampleId };
  dr = dt.NewRow();
  dr.ItemArray = drow;

  if (checkbox.Checked == true)
  {
   dt.Rows.Add(dr);
  }
  else if (checkbox.Checked == false)
  {
   int nIndex = dt.Rows.IndexOf(dr);
   dt.Rows.RemoveAt(nIndex);
  }

  ViewState.Add("dtTemp", dt);
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2024-08-12 22:39:15

由于您创建了一个新的 DataRow,因此

dr = dt.NewRow();

您不会在 DataTable 中找到它。

您想要删除数据表中的行:

int nIndex = dt.Rows.IndexOf(row);

而不是

int nIndex = dt.Rows.IndexOf(dr);

编辑:
或者也许不是。您可能必须循环遍历整个 DataTable 并比较列中的值:

for (var i = dt.Rows.Count; i >= 0; i--)
  if (dt.Rows[i].Cells[1].Text == sSampleId) {
    dt.Rows.Remove(i);
    break;
  }
}

Since you create a new DataRow

dr = dt.NewRow();

you won't find it in the DataTable.

You want to remove the row that IS in the DataTable:

int nIndex = dt.Rows.IndexOf(row);

not

int nIndex = dt.Rows.IndexOf(dr);

Edit:
Or maybe not. You'll probably have to loop through the entire DataTable and compare the value in the column:

for (var i = dt.Rows.Count; i >= 0; i--)
  if (dt.Rows[i].Cells[1].Text == sSampleId) {
    dt.Rows.Remove(i);
    break;
  }
}
岁月打碎记忆 2024-08-12 22:39:15

要查找并删除 ADO.NET DataTable 中给定 ID 的行:

DataRow[] found = dt.Select("sample_id = " + sample_id);
if (found.Length < 0)
{
   found[0].Delete();
}

请注意,DeleteRemove 之间存在差异。 Delete 将行的状态更改为 RowState.Deleted,这使其在大多数情况下从 DataTable 中消失。但它仍然存在。如果您使用数据适配器将 DataTable 与基础数据库表同步,这一点很重要:当您调用其 Update 方法时,适配器将删除基础行,并且然后才从 Rows 集合中删除该行。

如果您不使用数据适配器,则可以使用Remove 来删除行。

To find and delete a row in an ADO.NET DataTable given its ID:

DataRow[] found = dt.Select("sample_id = " + sample_id);
if (found.Length < 0)
{
   found[0].Delete();
}

Note that there's a difference between Delete and Remove. Delete changes the row's state to RowState.Deleted, which makes it disappear from the DataTable for most purposes. But it still exists. This is important if you're using a data adapter to sync the DataTable up with an underlying database table: the adapter will delete the underlying row when you call its Update method and only then remove the row from the Rows collection.

If you're not using a data adapter, it's OK to use Remove to remove a row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文