0

I am trying to delete a row in my gridview but im having a problem with the delete extension method.

Error im getting:

'System.Web.UI.WebControls.GridViewRow' does not contain a definition for 'Delete' and no extension method 'Delete' accepting a first argument of type 'System.Web.UI.WebControls.GridViewRow' could be found (are you missing a using directive or an assembly reference?)

Here is my code:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Submit"))
    {
        GridViewRow oItem = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
        int RowIndex = oItem.RowIndex;
        GridView1.Rows[RowIndex].Delete();
    }
}

1 Answers1

0

Try to do this using a different approach by looping thru the DataGridView's Selected Rows

if (e.CommandName.Equals("Submit"))
{
    GridView1.DeleteRow(GridView1.SelectedIndex)
    //you could null the datasource here and reassign if necessary here prior to calling DataBind()
    GridView1.DataBind();
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • @DJKRAZE there is nothing method like `Rows.RemoveAt` in GridView just checked now – Dgan Nov 26 '14 at 19:36
  • @DJKRAZE even i tried this one too it just `Deletes the record at the specified index from the data source.` not from gridview – Dgan Nov 26 '14 at 19:43
  • are you rebinding your data to refresh the view..? if it deletes the record then Issue a `GridView1.DataBind – MethodMan Nov 26 '14 at 19:44
  • @DJKRAZE ya i called `GridView1.DataBind();` after `DeleteRow` can you provide some link to msdn which denotes this – Dgan Nov 26 '14 at 19:48
  • can you also post the updated code..it's hard to see what you have done vs what you have stated what you have tried..? I would love to see how the datasource is being applied ..GridView1.DataSource= null` you could call then reassign the GridView DataSource, then call Databind on the Gridview ..also MSDN has all the examples necessary for the GridView.DeleteRow Method – MethodMan Nov 26 '14 at 19:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65716/discussion-between-ganesh-devlekar-and-dj-kraze). – Dgan Nov 26 '14 at 19:54
  • I think that you could do this a bit different not sure why you're having such difficulty here is an alternative way to do it using a stored procedure http://stackoverflow.com/questions/5539243/how-to-delete-a-selected-row-from-datagridview-and-database – MethodMan Nov 26 '14 at 19:55