I have used AutoGenerateDeleteButton="true" property of the GridView, so that am getting delete btn on every row. In order to avoid an exception('fired event RowDeleting which wasn't handled ') I am removing that row programmaticaly as follows

protected void gd_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
            dt_TestSubjects.Rows.RemoveAt(e.RowIndex);         
            gd_Subjects.DataSource = dt_TestSubjects;
            gd_Subjects.DataBind();
     }

Now problem is I want to Remove the row visually but I have to access that deleted row for some reason, but I can't since it is removed already from Datatable.

I thought I can use RowState property but actully row itself is getting removed,hence end of the story!

Am I going in wrong direction or there is some other property which I need to play with?

Recommended Answers

All 9 Replies

> but I can't since it is removed already from Datatable.

Populate dataTable once (use IsPostBack property) and put it (instance of DataTable) into the Session.

Nitin Daphale: Not actually sure what you're trying to do - presumably the row deleting event is being fired because you are trying to delete the row?

So why do you need to remove the row programatically as the inbuilt delete function will take care of this - do you have more than one delete buttons?

Tell us abit more about your program design and we may be able to offer further assistance

Nitin Daphale: Not actually sure what you're trying to do - presumably the row deleting event is being fired because you are trying to delete the row?

So why do you need to remove the row programatically as the inbuilt delete function will take care of this - do you have more than one delete buttons?

Tell us abit more about your program design and we may be able to offer further assistance

Here Are The Steps

1: I use AutoGenerateDeleteButton="true"
=> I got delete btn on every row but giving exception as soon as I press any of them I got an exceptio-"The GridView 'gd_Subjects' fired event RowDeleting which wasn't handled".

2: So I handle that exception through row deleting event handler, but grid still shown the row even I use IsPostBack property at the page load.

3: Hense I had deleted row programmatically
dt_TestSubjects.Rows.RemoveAt(rowIndex);

gd_Subjects.DataSource = dt_TestSubjects;

gd_Subjects.DataBind();

4:It reflecting the grid as I wanted with one less row.
Now As we do in WinForms applications, we can access deleted datatable row using property DataRowState(Added/Deleted/Detached).
Like that only I want to remove the row from grid but want to access which row deleted and which row added so that I can Insert and Delete records into the database.

I hope above explanation makes clear picture of what I did before and what I want to do now?

Oh, Thank you guys for the replies.

I think you've misunderstood what you should be doing:

This is how it should be done...

protected void gd_OnRowDeleting(object sender, GridViewDeleteEventArgs e)    {            
... Delete from Database
gd_Subjects.DataSource = DatabaseQuery;            
gd_Subjects.DataBind();     

}

Otherwise you will always have a false reading as the row will only be deleted from the UI and not the database.

I think you've misunderstood what you should be doing:

This is how it should be done...

protected void gd_OnRowDeleting(object sender, GridViewDeleteEventArgs e)    {            
... Delete from Database
gd_Subjects.DataSource = DatabaseQuery;            
gd_Subjects.DataBind();     

}

Otherwise you will always have a false reading as the row will only be deleted from the UI and not the database.

I have already cleared that, I don't want to delete any record from database when I clicks on any 'Delete' btn. I just want to remove that row from grid(invisible) which will make that row state 'Deleted' and later on(when 'Save' btn clicked)I will iterate through datarows I should be able to check that rowstate as 'deleted' so that I can do whatever with that datarow records. (Just Like we do in Winform application).

I think you might have understood the situation.

Thanks.

Why don't you just hide it and iterate through the grid for the hidden rows?

e.g to hide the row on 'deleting'

protected void gd_OnRowDeleting(object sender, GridViewDeleteEventArgs e)    
{
    gd.Rows[e.RowIndex].Visible=false;
}

Now iterate through the Gridview looking for the hidden rows

foreach (GridViewRow gvr in gd.Rows)
{
  if (gvr.Visible==false)			
  {
    some process...
    or         
    Debug.Print(gvr.RowIndex.ToString());
  }
}

I have tried it and it does work...

Why don't you just hide it and iterate through the grid for the hidden rows?

e.g to hide the row on 'deleting'

protected void gd_OnRowDeleting(object sender, GridViewDeleteEventArgs e)    
{
    gd.Rows[e.RowIndex].Visible=false;
}

Now iterate through the Gridview looking for the hidden rows

foreach (GridViewRow gvr in gd.Rows)
{
  if (gvr.Visible==false)			
  {
    some process...
    or         
    Debug.Print(gvr.RowIndex.ToString());
  }
}

I have tried it and it does work...

Well your suggestion is workaround I don't understand why there is DataRowState as 'Deleted'.

Whatever, the suggestion works fine, it solves my problem.

Thank you very very much.

As mentioned by crappy coder, inorder to access invisible rows one can use the following code:

foreach (GridViewRow gvr in gd.Rows)
{
  if (gvr.Visible==false)			
  {
    some process...
    or         
    Debug.Print(gvr.RowIndex.ToString());
  }
}

How can one achieve the same using javascript, i.e how to iterate thru invisible rows of gridview in javascript

store your removed indexing in commay separted values and then show them as per your needs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.