0

I've two tabs with Tab1 doing plain validation and Tab2, having a datagridview with its source being a datatable filled with data from a csv. When I navigate from Tab1 to Tab2, the transition is not smooth. When I switch from Tab2 to Tab1 and then back to Tab2, the grid is reloading the data and the form is freezing for a while. Is there a way we can enable caching of the datagrid content for it not to reload every time?

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()); }
    }
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
skrubber
  • 1,095
  • 1
  • 9
  • 18

1 Answers1

0

Thanks @Rakitic. I implemented this method to cache the data file and called the method in the Form_Load event:

   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;
    }
skrubber
  • 1,095
  • 1
  • 9
  • 18