Click here to Skip to main content
15,905,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a gridview where some data is displayed in it. If i want to delete displayed data i will click button "btnStart". Then a class called "DeleteRows" is instantiated which will show a form dialog where i will enter number of rows to delete and then click ok. Then those many rows are removed from datatable. Because datatable is the datasource for gridview, changes made to datatable are updated in
gridview also.

With respect to logic everything is fine. But if i specify the number of rows to delete somewhere around 1000 or more, datatable.Rows.RemoveAt( ) will take some time for processing.

So Untill the delete is completed

1. my gridview form will be in "Not Responding" state ?

2. Form displayed by DeleteRows class also is not getting closed ?

So how to solve this issue ? Source code is given below



C#
gridview.datasource = datatable;

 private void btnStart_Click(object sender, EventArgs e)
 {

     int nRows = 0;

     // specify number of datarows to delete
    //this class will display a form where user will enter the number of rows to //delete

      DeleteRows deleterows = new DeleteRows( ref nRows);

      deleterows.Showdialog( );

      For(int nIndex = 0; nIndex <= nRows; nIndex++)
      {
          datatable.Rows.RemoveAt( 0 );//removes first nRows from datatable

      }
    //because datatable is gridview data source, changes made are updated in gridview also

 }


Thanks in advance,
Posted
Updated 29-Mar-12 3:06am
v2

Problem in your code

You are deleting the first row i.e. at 0th index each time

The correct code should be

C#
For(int nIndex = 0; nIndex <= nRows; nIndex++)
{
datatable.Rows.RemoveAt( nIndex  );//removes first nRows from datatable 
datatable.AcceptChanges();
 
}
 
Share this answer
 
v2
Comments
[no name] 29-Mar-12 9:17am    
And how exactly will that help with making the program responsive while deleting many rows?
bbirajdar 29-Mar-12 9:44am    
Not required. Deleting a 1000 rows takes place within a fraction of second. It will not have any effect on responsiveness.

The major mistake you are making here is the 'delete rows' logic should be written in the Dialog OK event and not in btnStart event click
code4Better 30-Mar-12 0:11am    
Hello Birajdar,
I am always deleting 0th index because i want to delete the 1st nRows. In datatable if you delete 1 row then whole rows are shifted 1 above. So there is no problem with delete part.

Deleting 1000 records will be take only fraction of seconds if the datatable from which you are deleting is not datagridview datasource. If it is datagridview datasource then it will take some time.

My issue is not logic. The grid view form gets into "Not responding" state during the deletion process. How to solve this?
code4Better 30-Mar-12 0:16am    
Hello CDP1802,
When i delete some 5000rows it will take some 30 to 60 seconds to complete. During this time if i simply click anywhere in gridview form, then form goes to "Not responding" state and it continues being Not responding state untill whole rows are deleted.
You could use a multi-threading approach.

C#
private void btnStart_Click(object sender, EventArgs e)
{
      int nRows = 0;
      Thread test = new Thread(()=>Foo(nRows)); 
      test.Start();
}

private void Foo(int nRows )
{
       DeleteRows deleterows = new DeleteRows(nRows);
 
       deleterows.Showdialog( );
 
       For(int nIndex = 0; nIndex <= nRows; nIndex++)
       {
           datatable.Rows.RemoveAt( 0 );//removes first nRows from datatable  

       }
}
 
Share this answer
 
Comments
code4Better 30-Mar-12 0:29am    
Hello Vladislav,
Already i used an asynchronous delegate to do this. I think it is same using a thread. But still the issue persist. I think issue is during deletion Focus is completly on gridview form and gridview and there are no other action being carried out. Can this be a reason ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900