2013-06-27 5 views
0

주제에서 설명한대로 새 Datagridview에 Row를 추가하려고합니다. 폼의 생성자에서 AllowUserToAddRows를 false로 설정하고 있습니다. 프로그래밍 방식으로 행을 추가 할 수 있지만 설정 파일에 저장되지 않은 것 같습니다. 여기 프로그래밍 방식으로 추가 된 DataGridViewRow가 손실 됨

내 양식의 코드입니다 - 나는 약간 (희망 필수적이지) 부분을 왼쪽 : PS : 내 btnAddEntry_Click()의 ​​끝에서 내 댓글에 주목 - 당신은 잃는 방법

public DataSettings() 
    { 
     InitializeComponent(); 

     //Import rows that are saved int settings 
     for (int i = 0; i < Properties.Settings.Default.colNames.Count; i++) 
     { 
      dgv.Rows.Add(new DataGridViewRow()); 
      dgv.Rows[i].Cells[0].Value = Properties.Settings.Default.colNames[i]; 
      dgv.Rows[i].Cells[1].Value = Properties.Settings.Default.colStarts[i]; 
      dgv.Rows[i].Cells[2].Value = Properties.Settings.Default.colWidths[i]; 
     } 

     //Hide "new row"-row 
     dgv.AllowUserToAddRows = false; 
    } 

    private void cancel_Click(object sender, EventArgs e) 
    { 
     this.Dispose(); 
    } 

    private void save_Click(object sender, EventArgs e) 
    { 
     Properties.Settings.Default.colNames = new System.Collections.Specialized.StringCollection(); 
     Properties.Settings.Default.colStarts = new System.Collections.Specialized.StringCollection(); 
     Properties.Settings.Default.colWidths = new System.Collections.Specialized.StringCollection(); 
     foreach (DataGridViewRow row in dgv.Rows) 
     { 
      if (row.Index < dgv.Rows.Count - 1) 
      { 
       Properties.Settings.Default.colNames.Add((String)row.Cells[0].Value); 
       Properties.Settings.Default.colStarts.Add((String)row.Cells[1].Value); 
       Properties.Settings.Default.colWidths.Add((String)row.Cells[2].Value); 
      } 
     } 
     Properties.Settings.Default.Save(); 
     this.DialogResult = DialogResult.OK; 
    } 

    private void btnAddEntry_Click(object sender, EventArgs e) 
    { 
     dgv.AllowUserToAddRows = true; 
     Dialogs.Data_AddRow newRow = new Dialogs.Data_AddRow(); 
     newRow.ShowDialog(); 
     dgv.Rows.Add(new string[] { newRow.parmName, newRow.parmStart, newRow.parmWidth }); 
     newRow.Dispose(); 
     dgv.AllowUserToAddRows = false; //If I comment out this line - It works fine. 
             //but then the "newrow"-row is visible 
    } 

    private void btnDeleteEntry_Click(object sender, EventArgs e) 
    { 
     dgv.Rows.Remove(dgv.SelectedRows[0]); 
    } 

    private void btnDeleteAll_Click(object sender, EventArgs e) 
    { 
     dgv.Rows.Clear(); 
    } 

답변

1

이 행 때문에 마지막 행의 정보 : (row.Index < dgv.Rows.Count - 1)(row.Index < dgv.Rows.Count)이어야합니다. 그렇지 않으면 그냥 제거하십시오.

마지막 행이 NewRow하지 않은 경우 검사 할 경우이 작업을 수행 할 저장할 때 :

foreach (DataGridViewRow row in dgv.Rows) 
{ 
    if (!row.IsNewRow) 
    { 
     Properties.Settings.Default.colNames.Add((String)row.Cells[0].Value); 
     Properties.Settings.Default.colStarts.Add((String)row.Cells[1].Value); 
     Properties.Settings.Default.colWidths.Add((String)row.Cells[2].Value); 
    } 
} 
+0

을 세상에 오 : D - 들으 잔뜩! 당연하지! 만약 내가 "-1"xD – Toastgeraet

관련 문제