2013-10-31 43 views
1

好吧我完全新的WinForms,我試圖找到DataGridView1我通過如何查找另一種形式的DataGridView?

frmPledgeCreation.Controls.Find("DataGridView1", True)

另一種形式有一個表格(frmPledgeCreation)上的問題是,當我檢查的行數第二種形式的DataGridView1 ...即使在frmPledgeCreation的DataGridView1中有許多行時,我也總是得到1。

frmPledgeCreation.Controls.Find("DataGridView1", True)創建一個新的實例? 我的目標是建立從這個其他形式的這樣的事情該DataGridView中的特定列的單元格的值...

DGV.Rows(Convert.ToInt32(gDGVindex)).Cells("SecurityName").Value = GstrSearchResult.ToString()

其中gDGVindex是它具有rowIndex的全局變量..但我總是出現超出範圍的錯誤。

+0

將該DataGridView1公開並在其他窗體上使用。對於進一步的數據,你可以參考綁定到datagrid視圖的數據源 – Swati

+0

請發佈你的實際代碼。這是如此令人困惑的說什麼。順便說一下'ControlCollection.Find'方法在這種情況下不是你想要的。 –

回答

0

要訪問DataGridView1

在frmPledgeCreation

class frmPledgeCreation:Form 
{ 
public DataGridView dgv{get{return DataGridView1;}} 
Form1() 
{ 
    InitializeComponent(); 
} 
} 

顯示窗體2

Form2 f=new Form2(this); 
f.show(); 

在窗體2

class Form2 
{ 
frmPledgeCreation reftof1; 
Form2(frmPledgeCreation f) 
{ 
    reftof1=f; 
} 
} 
您可以在形式frmPledgeCreation參考

現在你可以使用reftof1參考

reftof1.dgv.Rows[Convert.ToInt32(gDGVindex)].Cells["SecurityName"].Value=GstrSearchResult.ToString() 
+0

不工作.. couldnt找到它 – Arbaaz

+0

@arbaaz嘗試我編輯的答案 – CASC

+0

仍然沒有運氣.. – Arbaaz

1

如果你正在做同樣的事情訪問的DataGridView的窗體2。我會在你的表單中公開一個公共函數。該功能將負責更新網格。

frmPledgeCreation.UpdateSomeCell(GstrSearchResult.ToString())

您必須擁有DataGridView1財產。

另一種方法是將DataGridView1從private/protected更改爲public。

相關問題