Windows窗体在gridview更新期间无响应 [英] Windows form not responding during gridview updation

查看:68
本文介绍了Windows窗体在gridview更新期间无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有一个gridview在其中显示一些数据.如果要删除显示的数据,我将单击"btnStart"按钮.然后实例化名为"DeleteRows"的类,该类将显示一个表单对话框,在该对话框中,我将输入要删除的行数,然后单击确定".然后,将这许多行从数据表中删除.由于datatable是gridview的数据源,因此在
中更新对datatable所做的更改 gridview也.

关于逻辑,一切都很好.但是,如果我指定要删除的行数在1000左右或更多,则datatable.Rows.RemoveAt()将需要一些时间进行处理.

因此,直到删除操作完成

1.我的gridview表单将处于无响应"状态吗?

2. DeleteRows类显示的表单也没有关闭吗?

那么如何解决这个问题呢?源代码在下面给出



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



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,

推荐答案

您的代码中的问题

您正在删除第一行,即每次在第0个索引处

正确的代码应为

Problem in your code

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

The correct code should be

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


您可以使用多线程方法.

You could use a multi-threading approach.

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  

       }
}


这篇关于Windows窗体在gridview更新期间无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆