2011-07-28 5 views
0

2 열의 DataGridView가있는 프로그램을 만들었습니다. 첫 번째 열은 읽기 전용 텍스트 상자입니다 (사용자가 변경할 수 없음). 두 번째 열은 모든 행에 동일한 콤보 상자가 있습니다. 사용자가 콤보를 변경하는 경우양식의 요소 저장

the program before opening a file

, 그럼 내가 요소가 그가 콤보가 자신의 선택에 선택이 끝난됩니다 프로그램을 여는 다음 있도록 저장하려면, 프로그램을 닫습니다.

the program after opening a file

나는 두 개의 텍스트 파일, example1.txt 및 example2.txt 제 1 및 제 2 열의 요소를 저장 관리해야하지만 저장된 요소를 다시 배치 할 수 있도록하는 방법을 모른다 프로그램이 열릴 때 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); 
} 

감사합니다,
조지

당신은 DataSet 개체의 케이크에 내장의 일부를 활용하고 그리드 데이터를 저장할 수 있습니다

답변

2

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

"개체 참조가 개체의 인스턴스로 설정되지 않았습니다."오류가 발생합니다. –

관련 문제