2013-10-31 27 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。

相关问题