2014-10-30 2 views
0

DataGridview의 선택된 행을 두 번째 형식으로 전달하려고합니다. 그런 다음 두 번째 DGV에서 항목을 선택하고 그 지점의 테이블에 정보를 전달하십시오. 을 Form1 : 형태 2에DGV 선택한 행을 두 번째 형식으로 전달

private void toolStripLabel1_Click(object sender, EventArgs e) 
    { 
     int interestsKey; 
     interestsKey = Convert.ToInt32(interestsKeyTextBox.Text); 

     DataGridViewSelectedRowCollection rows = interestAddDataGridView.SelectedRows; 

     frmDupeAdd AddDupeForm = new frmDupeAdd(interestAddDataGridView.SelectedRows); 
     AddDupeForm.Show(); 
    } 

내가 dgIntRows에 액세스 할 수없는 것 내가 뭘하려고했던 것은 아래의 방법으로 행을 통과합니다. 이것이 올바른 방법인지 또는 더 좋은 방법이 있는지 확실하지 않습니다.

양식 2 :

public frmDupeAdd(DataGridViewSelectedRowCollection selectedRows) 
     { 
      InitializeComponent(); 
      DataGridViewSelectedRowCollection dgIntRows = selectedRows; 
     } 

    private void btnAddAdds_Click(object sender, EventArgs e) 
     { 
      DataGridViewSelectedRowCollection rows = dgCaseInt.SelectedRows; 
      dgCaseInt.Columns[4].Visible = false; 
      dgCaseInt.Columns[5].Visible = false; 

      foreach (DataGridViewRow row in rows) 
      { 

       DataClasses1DataContext db = new DataClasses1DataContext(); 
       int interestkeyint = Convert.ToInt16(row.Cells[4].Value); 
       InterestAdd newinterestadd = new InterestAdd(); 
       newinterestadd.InterestsKey = interestkeyint; 
       foreach (DataGridViewRow row in dgIntRows) 
       { 
        newinterestadd.CaseNumberKey = (string)row.Cells[5].Value; ; 
        newinterestadd.streetNum = (string)row.Cells[2].Value; 
        newinterestadd.Direction = (string)row.Cells[2].Value; 
        newinterestadd.Add1 = (string)row.Cells[3].Value; 
        newinterestadd.Suffix = (string)row.Cells[4].Value; 
        newinterestadd.Unit = (string)row.Cells[5].Value; 
        newinterestadd.City = (string)row.Cells[6].Value; 
        newinterestadd.State = (string)row.Cells[7].Value; 
        newinterestadd.Zip = (string)row.Cells[8].Value; 
        db.InterestAdds.InsertOnSubmit(newinterestadd); 
        db.SubmitChanges(); 
       } 


      } 


     } 
+0

사용

public frmDupeAdd(DataGridViewSelectedRowCollection selectedRows) { InitializeComponent(); DataGridViewSelectedRowCollection dgIntRows = selectedRows; } 

의. 생성자가 변수를 끝내면 범위를 벗어납니다. 클래스의 'private'필드로 선언하고 생성자에서이 필드에 할당하면 다른 메서드에서 액세스 할 수 있습니다. – helrich

+0

기술적으로 더 매력적인 방법은 양식 특정 데이터를 전달하지 않고 두 형식 모두에서 사용되는 데이터 공급자를 사용하는 것입니다. 그런 다음 두 양식에서 항목 선택을 전달하거나 자동으로 동기화하는 데 사용할 수 있습니다. – Sinatr

+0

@Sinatr 그게 무슨 뜻이야? 데이터 공급자는 두 가지 형식 모두에서 사용됩니다. 내가 가지고있는 것은 사용자가 주소를 추가하는 DGV가있는 양식이며 해당 주소 중 하나 이상을 선택하고 버튼을 누르고 해당 사례의 다른 관심사를 선택하고 선택된 주소에 주소를 추가 할 수 있기를 원합니다. 이해. 그러면 나중에 사용자가 관심사 이름을 입력하고 해당 이름과 연관된 주소를 찾고 해당 주소를 현재 레코드/관심사에 추가 할 수있는 후속 조치가있게됩니다. – korrowan

답변

0

대신 당신은 생성자에서`dgIntRows`를 선언하고

private DataGridViewSelectedRowCollection dgIntRows = new DataGridViewSelectedRowCollection() 
    public frmDupeAdd(DataGridViewSelectedRowCollection selectedRows) 
     { 
        InitializeComponent(); 
        dgIntRows = selectedRows; 
     } 
관련 문제