简体   繁体   English

如何在Windows窗体应用程序中缓存DataGridView

[英]How to cache a datagridview in windows forms application

I am working on an application in which data is retrieved from four SQL tables using joins and the retrieved data is populated to a datagridview on a windows form. 我正在开发一个应用程序,其中使用joins从四个SQL表中检索数据,并将检索到的datagridview填充到Windows窗体上的datagridview I have two Radio Buttons ALL and DrawDate , by default ALL Radio Button is selected and once the application is opened it populates all the data on to the datagridview and when I select DrawDate Radio Button , only the data related to that draw date is populated on the datagridview . 我有两个Radio Buttons ALLDrawDate ,默认情况下选择ALL单选按钮,一旦打开应用程序,它将所有数据填充到datagridview ,当我选择DrawDate Radio Button ,仅填充与该绘制日期相关的数据datagridview Everything looks fine until then but after selecting DrawDate Radio Button and if a user want to get all the data again by selecting ALL Radio Button , it again loads all data from the database server which is not preferred. 直到那时一切都很好,但是选择了DrawDate Radio Button ,如果用户希望通过选择ALL Radio Button再次获取所有数据,它将再次从数据库服务器加载所有数据,这不是首选方法。 Is there any better way I can cache the data populated once the application is opened and populate it when a user selects ALL Radio Button later when he needs it? 有什么更好的方法可以缓存应用程序打开后填充的数据,并在用户以后在需要时选择“ ALL Radio Button时填充它?

C# Code C#代码

sqlcon = GetConnectionString();
            try
            {
                sqlcon.Open();

                //var sw = Stopwatch.StartNew();

                for (int i = 0; i < dgvPaymentsReceived_Collections.RowCount; i++)
                {
                    int trademonth = Convert.ToInt32(dgvPaymentsReceived_Collections.Rows[i].HeaderCell.Value);


                    for (int j = 0; j < dgvPaymentsReceived_Collections.ColumnCount; j++)
                    {
                        int paymentmonth = Convert.ToInt32(dgvPaymentsReceived_Collections.Columns[j].HeaderCell.Value);

                        //var sw = Stopwatch.StartNew();

                        SqlCommand cmd_PaymentsReceived = new SqlCommand();

                        cmd_PaymentsReceived.Connection = sqlcon;
                        cmd_PaymentsReceived.CommandType = CommandType.StoredProcedure;
                        cmd_PaymentsReceived.CommandText = sp_PaymentsReceved_Collections;

                        cmd_PaymentsReceived.Parameters.Add(new SqlParameter("@trademonth", trademonth));
                        cmd_PaymentsReceived.Parameters.Add(new SqlParameter("@paymentmonth", paymentmonth));


                        SqlDataAdapter da_PaymentsReceived_Collections = new SqlDataAdapter();
                        DataTable dt_PaymentsReceived_Colletions = new DataTable();

                        da_PaymentsReceived_Collections.SelectCommand = cmd_PaymentsReceived;
                        da_PaymentsReceived_Collections.Fill(dt_PaymentsReceived_Colletions);

                        //sw.Stop();
                        //MessageBox.Show(sw.ElapsedMilliseconds.ToString());

                        dgvPaymentsReceived_Collections.Rows[i].Cells[j].Value = dt_PaymentsReceived_Colletions.Rows[0][0].ToString();

                    }
                }

                sqlcon.Close();
            }

在此处输入图片说明

You haven't shown us any code. 您尚未显示任何代码。 What kind of connection did you use? 您使用哪种连接? Don't bind your database source to a datagridview - use either an Enumerable / IList collection or DataSet as your data holder and then assign only the part of it (filtering it with LINQ) to your datagridview. 不要将数据库源绑定到datagridview-使用Enumerable / IList集合或DataSet作为数据持有者,然后仅将其一部分(使用LINQ过滤)分配给datagridview。 This way you don't have to create a new collection every time in memory, just iterate over the items with given condition. 这样,您不必每次都在内存中创建新的集合,只需遍历具有给定条件的项目即可。

假设您没有大量的数据,则可以将SQL结果存储在DataTable中,然后将其绑定到DataGridView

单击“ All按钮时,您可以考虑将SQL数据存储在IEnumerable数据结构中,并将该集合绑定到gridview,然后在DrawDate按钮单击期间,对集合进行适当排序。

You can hang on to your original data table and create a separate DataTable to bind to your DataGridView if you want to be able to change what is displayed. 如果您希望能够更改显示的内容,则可以保留原始数据表并创建一个单独的DataTable绑定到DataGridView。

Just fill your data table once, and then create a new DataTable to hide what you don't need. 只需填写一次数据表,然后创建一个新的DataTable即可隐藏您不需要的数据。

This might use slightly more memory depending on the size of your DataTable, but it's probably the easiest solution to implement. 根据DataTable的大小,这可能会使用更多的内存,但这可能是最容易实现的解决方案。

    DataTable viewedData = dt_PaymentsReceived_Colletions;

    if(hideIncome.Checked) {
        viewedData.Columns.Remove("Income");
        viewedData.AcceptChanges();
    }

    DataGridView1.DataSource = viewedData;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM