2012-11-30 55 views
1

我已經詳盡地研究過(搜索到)這個問題的解決方案,但找不到任何答案。我的問題是,在Form1中選擇一行(爲了清晰起見),它不會將值傳遞給Form2中的DataGridView。請注意,所有列標題都會被傳送。將選定的DataGridView行轉換爲另一種形式的新DataGridView

這裏是Form1中的一個片段:

 private void customersDataGridView_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
     OrderSearchForm orderform = new OrderSearchForm(); 

     orderform.Row = this.customersDataGridView.CurrentRow; 

     orderform.MdiParent = this.MdiParent; 

     orderform.Show(); 
    } 

1和Form 2:

 private DataGridViewRow row; 

    public DataGridViewRow Row 
    { 
     get { return row; } 
     set { row = value; } 
    } 

    private void OrderSearchForm_Load(object sender, EventArgs e) 
    { 
     NorthwindDataClassesDataContext db = new NorthwindDataClassesDataContext(); 

     DataGridViewRow r = row.Clone() as DataGridViewRow; 

     foreach (DataGridViewCell cell in row.Cells) 
     { 
      this.OrderSearchgridview.Columns.Add(cell.OwningColumn.Name, 
       cell.OwningColumn.HeaderText); 

      r.Cells[cell.ColumnIndex].Value = cell.Value; 
     } 

     OrderSearchgridview.Rows.Add(r); 
    } 

所以基本上,我的問題是,我該如何顯示從Form1的DataGridView在窗體2的DataGridView行值?

我覺得我錯過了插入列後添加實際值的一個步驟,但我堅持了這一點。

+0

你在Form1中綁定到DataGridView的哪些數據? –

回答

0

最好有一個OrderSearchForm Contrucutor來存放行的值。

考慮行是有3個值像ID,姓名,年齡

爲OrderSearchForm這些Pramaeteter創建對象。

OrderSearchForm orderform = new OrderSearchForm(New User(customersDataGridView.SelectedRows[0].Cells[0].Value.ToString(),SelectedRows[0].Cells[1].Value.ToString(),SelectedRows[0].Cells[2].Value.ToString())); 

    orderform.MdiParent = this.MdiParent; 

orderform.Show(); 

1和Form 2:

private DataGridViewRow row; 
    private User userObj; 

public OrderSearchForm(User user) 
{ 
    this.userObj=user; 

} 


    public DataGridViewRow Row 
    { 
     get { return row; } 
     set { row = value; } 
    } 

    private void OrderSearchForm_Load(object sender, EventArgs e) 
    { 
     //Do Operation with User's Properties.as you Like 

     OrderSearchgridview.Rows.Add(r); 
    } 
+0

終於有工作了!感謝您對此問題的快速回復和解決方案:) – user1865299

相關問題