2012-12-20 39 views
2

我已经设置了一个2表单载入一个datagridview,并且用户单击视图以选择他们想要的值。我可以在datagridview的同一窗体中获得显示在消息框内的值但是,当我尝试将它传递给另一个窗体时,它显示为NULL。我将如何让它显示在文本框内。这是我的代码。当我调试代码时,它首先正确传递值,但是当它完成运行时,它显示为空值。我已经尝试了很多不同的方法来做到这一点,试图在不同的类上使用公共变量。将字符串从datagridview传递到另一种形式的文本框

与文本框

public void FillTextBoxes(object sender, EventArgs e, string SupplierID) 
    { 
     supplierVO _SupplierVo = new supplierVO(); 
     ListOfSuppliers _ListOfSuppliers = new ListOfSuppliers(); 
     SupplierID = _ListOfSuppliers.SupplierCode;         
     MessageBox.Show(SupplierID); 
     txtSupplierCode.Text = SupplierID;   
    } 

窗体2与DataGridView的

 // Links to the user double click of the datagrid 
    public void SelectGridInformation (object sender, EventArgs e) 
    { 
     ChangeSupplierInfo _ChangeSupplerInfo = new ChangeSupplierInfo(); 

     supplierVO _SupplierVO = new supplierVO(); 

     Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected); 

     string SelectedSupplierID = dataGridView1.SelectedCells[0].Value.ToString(); 

     SupplierCode = SelectedSupplierID; 

     _SupplierVO.SupplierCode = SelectedSupplierID; 

     _ChangeSupplerInfo.FillTextBoxes(sender, e, SelectedSupplierID); 

     this.Close(); 
    } 

我一直在试图用get和set属性的在这里做这一个表是该代码示例。

public string SupplierCode 
    { 
     get 
     { 
      return _SupplierCode; 
     } 
     set 
     { 
      _SupplierCode = value; 
     } 
    } 
+1

我的猜测是,你想用你的'ListOfSuppliers'类的现有实例,而不是创建的例如。 – PhoenixReborn

+1

最好的方法是通过使用某种消息传递实现来分离功能,然后使用发布者/订户模型。 –

+0

你在代码中放置了'FillTextBoxes'函数? – Fabio

回答

2

我不知道,如果我们正在考虑同样的事情,但看一下这个。 你已经有Form1 DataGridView和Form2哪里是你的TextBox。

因此,让我们创建窗体2内Form1中......

Form2 form2 = new Form2(); 
form2.show(); 

...并获得DataGridView中的选定单元格

form2.value = getDataGridValue(); 

然后,让我们财产申报中的Form2,这是我们会通过用户选择的值。

private string _value; 

public string value 
{ 
    get { return _value; } 
    set 
    { 
     _value = value; 
     OnPropertyChanged(_value); 
    } 
} 

请注意,我们使用INotifyPropertyChanged的所以包括内部窗体2

public partial class Form2 : Form, INotifyPropertyChanged 

本声明创建公共事件

public event PropertyChangedEventHandler PropertyChanged; 

不管走到哪里我们的用户点击DataGridView的东西提出来

protected void OnPropertyChanged(string value) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(value)); 
     } 
    } 

然后在Form2Load订阅你的新事件

private void Form2_Load(object sender, EventArgs e) 
{ 
    PropertyChanged += new PropertyChangedEventHandler(Form2_PropertyChanged); 
} 

,做的东西

void Form2_PropertyChanged(object sender, PropertyChangedEventArgs e) 
{ 
    this.InvokeIfRequired((value) => textBox1.Text = value, e.PropertyName); 
} 

对我来说它工作得很好,我选择在Form1上的DataGridView价值和获取里面的TextBox这个值窗体2,但正如我所说,我不知道,如果我们正在考虑同样的事情...

+0

感谢您的回复只是有点困惑,看到他创建一个公共事件,我需要在form1与datagrid的形式吗? – Inkey

+0

Form1 =带有dataGrid的窗体,带有TextBox的Form2 =窗体,您正在form2上创建公共事件。在Form1上,您只需初始化form2,将用户选择传递给它并显示它。其他一切(属性,事件)都在form2上。 – erem

+1

[https://dl.dropbox.com/u/96085825/suplier.zip] – erem

0

此代码中的以下行是您的问题。

public void FillTextBoxes(object sender, EventArgs e, string SupplierID) 
{ 
    supplierVO _SupplierVo = new supplierVO(); 
    ListOfSuppliers _ListOfSuppliers = new ListOfSuppliers(); 
    SupplierID = _ListOfSuppliers.SupplierCode; //This line is the problem.         
    MessageBox.Show(SupplierID); 
    txtSupplierCode.Text = SupplierID;   
} 

您正在从方法的参数中获取SupplierID。你为什么用空值更新供应商标识。当您创建一个ListOfSuppliers的新对象时,那么该类中的SupplierCode是空的,您将为您的SupplierID分配一个空值。

另外我看不到变量_ListOfSuppliers的目的?

+0

当我测试它传递了一个变量,看看我是否能以这种方式工作,但没有它的喜悦。 – Inkey

0

不能由某些原因

创建表单New Instance通过string尝试在Form1试图通过这个代码,以显示窗体2这一个..。

using (var f = new Form2 
         { 
          Owner = this 
         }) f.ShowDialog(); 

然后在Form2事件时,它应该是这样的

Form1 f0 = this.Owner as Form1; //_ChangeSupplerInfo 

    supplierVO _SupplierVO = new supplierVO(); 

    Int32 selectedRowCount = dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected); 

    string SelectedSupplierID = dataGridView1.SelectedCells[0].Value.ToString(); 

    //SupplierCode = SelectedSupplierID; 

    //_SupplierVO.SupplierCode = SelectedSupplierID; 

    f0.FillTextBoxes(SelectedSupplierID); 

    this.Close(); 

Form1

public void FillTextBoxes(string SupplierID) 
    { 
     supplierVO _SupplierVo = new supplierVO(); 
     ListOfSuppliers _ListOfSuppliers = new ListOfSuppliers(); 
     //SupplierID = _ListOfSuppliers.SupplierCode;         
     MessageBox.Show(SupplierID); 
     txtSupplierCode.Text = SupplierID;   
    } 
1

,如果你需要在你的窗体2 唯一字符串供应商ID,然后在窗体2

创建供应商ID的变量
private String supplierID; 

independ这个数据的目的,此变量

创建属性然后创建一个新的自定义构造函数的Form2

public void Form2(String supplierid) 
{ 
    this.supplierID = supplierid; 
} 
在Form1中

当你创建实例的窗体2只使用您的自定义构造函数

//somwhere in Form1 
Form2 frm2 = New Form2(SelectedSupllierID) 
frm2.ShowDialog() //… 
相关问题