2011-07-28 63 views
0

的元素我已經制作了一個程序,你有一個DataGridView有2列。第一列是隻讀文本框(用戶不能更改它)。第二列在每一行中都有相同的組合框。保存表格

the program before opening a file

如果用戶改變組合框,然後關閉該程序,我想的元素被保存,以便他打開程序的組合框會選擇去他的選擇下一次。

the program after opening a file

我已成功地在第一和第二列的元素保存兩個文本文件,example1.txt和example2.txt,但我不知道如何使保存的元素被再次置於在程序打開時在datagridview中。

另外,txt文件保存在csv文件所在的路徑中。我希望它可以保存在exe文件路徑中。

這是我到目前爲止做出:

private void button1_Click(object sender, EventArgs e) 
      { 
       string filename = ""; 
       DialogResult result = openFileDialog1.ShowDialog(); 
       if (result == DialogResult.OK) 
       { 
        filename = openFileDialog1.FileName; 
       textBox1.Text = filename; 
       string line; 
      // Read the file and display it line by line. 
      System.IO.StreamReader file = new System.IO.StreamReader(textBox1.Text); 

      stringforData = file.ReadLine();  
      while ((line = file.ReadLine()) != null) 
      { 

       fileList.Add(line.Split(';')); 
      } 

      file.Close(); 



      this.ToDataGrid(); 
     } 
    } 

private void button2_Click(object sender, EventArgs e) 
     { 

    //************* COLUMN 2 TO STRING[] ************************************ 
      string[] colB = new string[dataGridView1.Rows.Count]; 



      for (int i = 0; i < dataGridView1.Rows.Count; i++) 
       { 

        colB[i] = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value); 
        File.WriteAllLines("settings2.txt", colB); 
       } 
     //************************************************************************* 
} 
    public void ToDataGrid() 
     { 
      string[] split = stringforData.Split(';'); 


     foreach (string item in split) 
     { 
      dataGridView1.Rows.Add(item); 
     } 
     File.WriteAllLines("settings1.txt", split); 
} 

感謝,
喬治

回答

2

你可以採取的一些內置的優勢,在DataSet對象的好吃的東西和你的網格數據保存到一個XML文件,並在您再次啓動應用程序時將其讀回網格。

 //note that this will just save it in the bin folder 
     //you'll want to use a better path 
     string settingsFile = "GridSettings.xml"; 
     DataTable gridData = null; 

     public FormSaveFoo() 
     { 
      InitializeComponent(); 
      PrepareSettingsDataSource(); 
      SetUpDataSourceBindings(); 

     } 

     private void PrepareSettingsDataSource() 
     { 
      //see if have a settings file 
      if (File.Exists(settingsFile)) 
      { 
       //load up the settings 
       DataSet settings = new DataSet(); 
       settings.ReadXml(settingsFile); 
       if (settings.Tables.Count > 0) 
       { 
        gridData = settings.Tables[0]; 
       } 
      } 
      else 
      { 
       CreateSettingsTable(); 
      } 
     } 

     private void CreateSettingsTable() 
     { 
      gridData = new DataTable(); 
      gridData.Columns.Add(new DataColumn("Name")); 
      gridData.Columns.Add(new DataColumn("Text")); 
     } 

     private void SetUpDataSourceBindings() 
     { 

      dataGridView1.Columns["NameColumn1"].DataPropertyName = "Name"; 
      dataGridView1.Columns["TextColumn1"].DataPropertyName = "Text"; 
      dataGridView1.DataSource = gridData; 
     } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      //add the grid data to a dataset and then write it to a file 
      DataSet persistSettings = new DataSet(); 
      persistSettings.Tables.Add(gridData); 
      persistSettings.WriteXml(settingsFile); 
     } 
+0

不明白什麼做 'dataGridView1.Columns [ 「NameColumn1」] DataPropertyName = 「名稱」。 ' 'dataGridView1.Columns [ 「TextColumn1」] DataPropertyName = 「文本」;'' =的GridData settings.Tables [0];' 線做..... 請幫助! –

+0

dataGridView1.Columns [「NameColumn1」]。DataPropertyName =「Name」;告訴網格列鏈接到哪個數據表列。 gridData = settings.Tables [0];從XML中讀取數據集中的第一個表並將其分配給綁定到網格的數據表 – dbugger

+0

我收到錯誤「對象引用未設置爲對象的實例」。 –