tags:

views:

65

answers:

1

My DataSource is a DataTable populated from file system data in Page_Load. There is no database. How can I remove rows from the DataTable and rebind? I thought this had to be done in the GridView_RowCommand "Delete" section...

if(e.CommandName == "Delete")
    ...

When I try to access the DataRow's within

//get the datatable
DataTable dt = this.gridCPCP.DataSource as DataTable;

// Delete the record 
foreach (DataRow dr in dt.Rows)
{
    ....

dt is null. How do I reference the DataTable when deleting records?

A: 

e.CommandArgument should give you the index of the row which should be deleted. You could bind your datatable to the grid again without the deleted item.

Tim Schmelter
I cannot with this code. `dt` is null on the line with the `foreach` statement. I need to know how to reference the DataTable within this method (gridview_RowCommand). I already have code within the for loop: `if(dr["ID"].ToString() == "e.CommandArgument" { //do stuff }` but it can't get there without a reference to the DataTable.
David
You dont have a reference to the datatable after postback when you dont bind the grid to the datatable on every postback(not recommended). But you have the CommandArgument which is given from ViewState to identify what you must delete. Normally you would then delete this from a database. I dont know what you must delete because i dont know what this grid is for and what data your datatable contains. You can remove that row from the grid or make it invisible or you want to delete this item, reload the datatable and bind this new datatable to the grid(preferred).
Tim Schmelter
the rows just represent some files and a couple of their attributes. this datatable is built in page_load. the delete code will delete the file. so, when page_load is called again, it will not find that file in the file system and also not add a row to the table.
David
Then you could set the filepath as CommandArgument f.e. in RowDataBound:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspxThere you have the reference to the datatable and so the path.
Tim Schmelter
.. or when you have stored the filepath in a (in)visible column, you can access the row and its controls through the index(stored as CommandArgument):this.gridCPCP.Rows(index)
Tim Schmelter
using file name/path as the CommandArgument works.
David
can you explain where, in your link to MSDN, i can get a reference to the datatable? i actually have some code in my gridCPCP_RowDataBound handler to add some javascript onclick functionality, but nothing else.
David
e.Row.DataItem.Here is an example:http://msdn.microsoft.com/en-us/library/ms972833.aspx
Tim Schmelter