C# 从数据集中完全删除一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18471189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting a row completely from a dataset
提问by CodeNinja
I have a remove button on my gridview. On Clicking the remove button , the row should be completely removed from the session. I am currently doing the following :
我的 gridview 上有一个删除按钮。单击删除按钮后,该行应从会话中完全删除。我目前正在做以下事情:
protected void gvMainLog_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Remove")
{
GridViewRow rowSelect = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int rowindex = rowSelect.RowIndex;
DataSet ds = ((DataSet)Session["old"]);
ds.Tables[0].Rows[rowindex].Delete();
Session["old"] = ds;
gvMainLog.DataSource = Session["old"];
gvMainLog.DataBind();
}
The problem is that :
问题是:
ds.Tables[0].Rows[rowindex].Delete();
removes only the content in that row. When I look at the dataset , it shows an empty row.
仅删除该行中的内容。当我查看 dataset 时,它显示一个空行。
Is there a way I can remove the entire row, without it showing an empty row ?
有没有办法可以删除整行而不显示空行?
采纳答案by Oscar
Try calling
试试打电话
ds.AcceptChanges()
after row.Delete().
在 row.Delete() 之后。
回答by User
If you want to remove a single data row, RemoveAt is the easier option:
如果要删除单个数据行,RemoveAt 是更简单的选择:
ds.Tables[0].Rows.RemoveAt(rowIndex);
Oscar's answer is also correct, according to the remarks for RemoveAt on MSDN:
根据MSDN 上 RemoveAt的评论,Oscar 的回答也是正确的:
When a row is removed, all data in that row is lost. You can also call the Delete method of the DataRow class to just mark a row for removal. Calling RemoveAt is the same as calling Delete and then calling AcceptChanges.
删除一行时,该行中的所有数据都将丢失。您还可以调用 DataRow 类的 Delete 方法来仅标记要删除的行。调用 RemoveAt 与调用 Delete 然后调用 AcceptChanges 相同。