将字符串从 datagridview 传递到另一种形式的文本框 [英] Passing String From datagridview to textbox in another form

查看:24
本文介绍了将字符串从 datagridview 传递到另一种形式的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了 2 个表单,其中一个加载了 datagridview,用户单击视图以选择他们想要的值.我可以将值显示在与 datagridview 相同的表单上的消息框中,但是当我尝试将其传递到另一个表单时,它显示为 NULL.我如何让它显示在文本框中.这是我的代码.当我调试代码时,它首先正确地传递了该值,但是当它完成运行时,它显示为空值.我尝试了许多不同的方法来做到这一点,也尝试在不同的类上使用公共变量.

I have set up a 2 forms one loads up a datagridview and the user clicks on the view to select the value they want. I can get the value displaying inside a messagebox on the same form as the datagridview However when i try to pass it across to another form it appears NULL. How would i get it displaying inside the textbox. Here is my code. When i debug the code it first passes across the value correctly but when it finishes running it shows as a null value. I have tried a number of different ways of doing this tried to do it using public variables on the different classes also.

与TextBox形成一个

Form one with the TextBox

 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;         
    }

带有 dataGridView 的 Form2

Form2 with the 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 属性来做到这一点,这里是代码示例.

I have been trying to do this with the Get and Set Property's here is the code sample for this.

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

推荐答案

我不确定我们是否在考虑相同的事情,但请检查一下.您的 Form1 是 DataGridView,Form2 是您的 TextBox.

I'm not sure if we're thinking about the same thing, but check this out. You've got Form1 where's DataGridView and Form2 where's your TextBox.

那么让我们在 Form1 中创建 Form2...

So let's create Form2 inside Form1...

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

...并获取 DataGridView 的选定单元格

... and get DataGridView's selected cell

form2.value = getDataGridValue();

然后,让我们在 Form2 中声明属性,我们将向其传递用户选择的值.

Then, let's declare property inside Form2, to which we'll be passing value selected by user.

private string _value;

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

请注意,我们使用的是 INotifyPropertyChanged,因此在 Form2 中包含此声明

Notice that we're using INotifyPropertyChanged so include this declaration inside Form2

public partial class Form2 : Form, INotifyPropertyChanged

创建公共事件

public event PropertyChangedEventHandler PropertyChanged;

并在我们的用户点击 DataGridView 中的某些内容时引发它

And raise it wherever our user clicks something in DataGridView

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

然后在 Form2Load 上订阅您的新事件

Then on Form2Load subscribe to your new event

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 中选择值并在 Form2 上的 TextBox 中获取此值,但正如我所说,我不确定我们是否在考虑相同的事情...

For me it works fine, I'm selecting value in DataGridView on Form1 and getting this value inside TextBox on Form2, but as I said, I'm not sure if we're thinking about the same thing...

这篇关于将字符串从 datagridview 传递到另一种形式的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆