首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TabControl DataGridView |数据缓存

TabControl DataGridView |数据缓存
EN

Stack Overflow用户
提问于 2016-08-31 13:07:19
回答 1查看 61关注 0票数 0

我有两个选项卡,一个是Tab1做普通验证,另一个是Tab2,它有一个datagridview,它的源是一个用来自csv的数据填充的datatable。当我从Tab1导航到Tab2时,转换并不顺利。当我从Tab2切换到Tab1,然后又切换回Tab2时,网格正在重新加载数据,表单冻结了一段时间。有没有一种方法可以让我们缓存datagrid内容,而不是每次都重新加载?

代码语言:javascript
复制
private void Tab_Selected(Object sender, TabControlEventArgs e)
    {
        if (e.TabPage == Tab1)
        {
            WindowState = FormWindowState.Maximized;

            Grid.Size = new System.Drawing.Size(1920, 1080);
            Tab.Size = new Size(1920, 1080);
            Grid.Dock = DockStyle.Fill;

            try
            {
                foreach (string f in Directory.GetFiles(dir, "HSPP*")) 
                {                          
                    LoadData(f); 
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception raised:" + ex.Message);
            }
        }
        else
        {
            Tab.BindingContext = this.BindingContext;
            this.WindowState = FormWindowState.Normal;
        }
    }
   public void LoadData(String FileName)
    {

        string header_text = "<a bunch of headers>";
        string[] headers;
        string[] fields;

        ArrayList schoolids = new ArrayList();
        DataTable dt = new DataTable();
        BindingSource source = new BindingSource();
        TextFieldParser tp = new TextFieldParser(FileName);
        DataRow dr;

        Grid.AllowUserToAddRows = false;
        headers = header_text.Split(new char[] { ',' });
        tp.TextFieldType = FieldType.FixedWidth;
        tp.SetFieldWidths(new int[33] { 10, 8, 8, 35, 35, 35, 35, 20, 15, 40, 40, 30, 30, 40, 25, 3, 12, 15, 10, 10, 8, 25, 15, 40, 60, 80, 3, 5, 5, 5, 41, 1, 30 });

        try
        {
            for (int i = 0; i < headers.Length; i++)
                dt.Columns.Add(headers[i]);

            while (!tp.EndOfData)
            {
                dr = dt.NewRow();
                fields = tp.ReadFields();

                for (int i = 0; i < fields.Count(); i++)
                    dr[i] = fields[i].ToUpper();

                dt.Rows.Add(dr);

            }

            source.DataSource = dt;
            Grid.DataSource = source;
            foreach (DataGridViewRow row in Grid.Rows)
            {
                if (!schoolids.Contains(row.Cells[8].Value))
                    schoolids.Add(row.Cells[8].Value);
            }
            ValidateData(schoolids);
        }
        catch (Exception e) { MessageBox.Show(e.ToString()); }
    }
EN

回答 1

Stack Overflow用户

发布于 2016-08-31 23:16:35

谢谢@Rakitic。我实现了这个方法来缓存数据文件,并在Form_Load事件中调用了这个方法:

代码语言:javascript
复制
   private Boolean CacheDataFile(String file) 
    {   
        CacheItemPolicy policy = new CacheItemPolicy();
        List<string> filepaths = new List<string>();
        string filecontents = cache["insidefile"] as string;
        string path = new FileInfo(file).FullName;

        filepaths.Add(path);
        policy.ChangeMonitors.Add(new HostFileChangeMonitor(filepaths));

        filecontents = File.ReadAllText(path);
        cache.Set("insidefile", filecontents, policy);            
        return true;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39241164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档