2014年7月13日 星期日

[RESOLVED] Deleting Rows in gridview


I have a gridview and i want to remove the selected rows from the gridview when click the button.The deletion of rows should not effect the database.


It should be like when i click the row gets removed .



can you please try this,


grid.Rows.Remove( grid.selectedRow );

will work hopfully





Remove ??


It is showing an error with remove



ill post my code when i get home



i think you need to pass the datatable to another datatable then bind it again to the gridview



Use viewstate to store datatable


Set gridview datasource from the datatable, modifiying the data will not affect to the db



As stated above, set the DataSource of your GridView to a DataTable and persist the DataTable in memory with ViewState, or Session.  Then in your GridView's RowCommand do this:


Private Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "Select" Then
dt1.Rows.RemoveAt(e.CommandArgument)
GridView1.DataSource = dt1
GridView1.DataBind()
ViewState("dt1") = dt1
End If
End Sub


 



private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
{
DataRow[] dr = dt.Select("ItemCode = '" + gvRow.Cells[1].Text + "'");
if (dr.Length > 0)
{
dt.Rows.Remove(dr[0]);
dt.AcceptChanges();
}
return dt;
}

here is my code for deleting a certain row, this gets the current row then in the datatable it looks for the ID that is found in the gridview then if found, it will be deleted in the datatable then rebind it again to the gridview





please mark as answer, if this solves your problem 


沒有留言:

張貼留言