optimize updates to DataTable bound to DataGridView

后端 未结 7 1207
执念已碎
执念已碎 2021-02-14 19:27

I have a Form in my application that displays some data. When I first show the Form, I load some data into a DataTable then bind the DataTable to a DataGridView. I also start an

7条回答
  •  情歌与酒
    2021-02-14 19:50

    Just posting this as a solution: Making some notations to the comments and posts already. The Merge Table method mentioned by BFree is a very good method to use and I think is the right approach not too mention very simple and elegant. Here are my notes why and the big one I am not sure if any one caught was the hits on the server for the query. The op stated in his comments to BFree that he would need to copy the table to do what he needed to, of course which table that was I am not sure , because his code:

    foreach (DataRow row in data.Rows)
    

    Those rows come from his table called data where is the copy required on that - he already has it.

    Then here is something that just smacks right out on EACH Iteration of that loop :

    SlowLoadingData slow_stuff = slow_query_results[(int)row["id"]];
    

    Is the OP really querying the database each iteration of those rows (Which if it is a large table are we talking 100,000 rows +). Think of the load on the Server (his app too must generate this query request!), and also the amount of traffic it places on a network to do this! IF it is the only app maybe it's ok, but even at that it is not what I would prefer to do if I wanted to be efficient.

    If gathering the data from the database in one query seems to much - then perhaps a better method would be to page his data in and do the merge.

    SlowLoadingData Page1_SlowLoadingData = slow_query_results[Page1] as DataTable;
    data.Merge(Page1_SlowLoadingData);
    
    SlowLoadingData Page2_SlowLoadingData = slow_query_results[Page2] as DataTable;
    data.Merge(Page2_SlowLoadingData);
    

提交回复
热议问题