2016-08-31 7 views
0

私は、Tab1が平易な検証を行っている2つのタブと、そのソースがcsvのデータで満たされたデータテーブルであるDataGridviewを持つTab2を持っています。 Tab1からTab2にナビゲートすると、遷移がスムーズではありません。 Tab2からTab1に切り替えてからTab2に戻ると、グリッドがデータを読み込んでいて、しばらくフォームがフリーズしています。毎回リロードしないようにDataGridコンテンツのキャッシュを有効にする方法はありますか?TabControl DataGridView |データキャッシング

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()); } 
    } 
+0

あなたがこれまで行ってきたことを見てみましょう。 –

+0

どうにかしてコードが下がりました。それを追加しました。 – skrubber

+0

this [post](http://stackoverflow.com/a/12640453/6290553)が役に立ちます。 –

答えて

0

ありがとう@Rakitic。このメソッドを実装してデータファイルをキャッシュし、Form_Loadイベントのメソッドを呼び出しました。

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; 
    }