开发者

Removing a row from a Gridview in Codebehind (getting exception?)

I'm trying to remove a row from my gridview manually, because for some reason, when I delete the item from the database and databind, the gridview still isn't updating. 开发者_StackOverflow中文版Here is the code I am using:

try
{
    gvCertifications.DeleteRow(int.Parse(commandArgs[1]));
}
catch (HttpException)
{
    //error
}

and for some reason, I'm always hitting that HttpException? I've tried placing this command both before and after my DataBind (because MSDN says that the HttpException occurs when the gridview isn't bound to a datasource)

The gridview is already bound to a datasource when it is loaded, so I don't know why I would be getting this exception...

Also, in case it helps, here is how I get commandArgs[]:

String[] commandArgs = e.CommandArgument.ToString().Split('|'); //before in same function as the try/catch block

and here are the commandargs from the .aspx file:

CommandArgument='<%#Eval("ThisId") + "|" + Container.DataItemIndex %>'


If you used sqldatasource as a DataSource(as in MSDN Example) then, you need to write the delete query on deletecommand property of it. Or if you used DataTabe as a DataSource, then you need to handle RowDeleting event of the gridview. Check this link for sample.


While it doesn't necessarily answer my question, it solves the issue I was trying to hack around with it:

from question:

"...for some reason, when I delete the item from the database and databind, the gridview still isn't updating..."

Gridviews have predefined commands set up, such as 'select', 'edit', and 'delete'. When I try to define my own event to run with the command name 'delete', the program will try and run parts of my event alongside it's predefined event. This is what has been causing issues.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜