将字符串从DataGridView传递到另一种形式的TextBox



我已经设置了一个2个表单,一个一个形式加载了数据杂志,用户单击视图以选择所需的值。我可以在与DataGridView相同的形式的消息框中显示值显示的值,但是当我尝试将其传递到另一个表单时,它看起来是null。我将如何在文本框中显示它。这是我的代码。当我调试代码时,它首先正确地传递了该值,但是当它完成运行时,它显示为空值。我已经尝试了多种不同的方法来尝试使用不同类别上的公共变量进行操作。

与文本框形成一个

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

form2带有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 and stele属性在此处进行此操作。

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

我不确定我们是否在考虑同一件事,但请检查一下。您已经有form1的datagridview和form2在哪里。

在哪里。

所以让我们在form1中创建form2 ...

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,因此在Form2

中包含此声明
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);
}

对我来说,它可以正常工作,我在datagridview中选择了form1的值并在form2上的文本框中获取此值,但是正如我所说,我不确定我们是否在考虑同一件事...

如果您只需要form2然后在Form2

中创建一个供应列的变量
private String supplierID;

目的是为此数据创建此变量的属性

然后为form2

创建一个新的自定义构造函数
public void Form2(String supplierid)
{
    this.supplierID = supplierid;
}

在Form1中创建Form2实例时,只需使用自定义构造函数

//somwhere in Form1
Form2 frm2 = New Form2(SelectedSupllierID)
frm2.ShowDialog() //…

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

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

您正在从该方法的参数中获取spectierID。为什么要用空值更新供应商。当您创建listofsuppliers的新对象时,该类中的供应商是空的,您正在为spectierID分配一个空值。

我也看不到变量_listofsuppliers的目的?

您无法通过某种原因创建表格New Instance来传递string

尝试这个..在您的Form1中尝试通过此代码显示Form2。

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

最新更新