Reputation: 2207
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.
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;
}
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();
}
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;
}
}
Upvotes: 2
Views: 3580
Reputation: 32445
if you need only string SupplierID in your Form2 then create a variable of supplierID in Form2
private String supplierID;
independ on purpose of this data create a properties for this variable
then create a new custom constructor for Form2
public void Form2(String supplierid)
{
this.supplierID = supplierid;
}
in Form1 when you create instance of Form2 just use your custom constructor
//somwhere in Form1
Form2 frm2 = New Form2(SelectedSupllierID)
frm2.ShowDialog() //…
Upvotes: 1
Reputation: 2372
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.
So let's create Form2 inside Form1...
Form2 form2 = new Form2();
form2.show();
... and get DataGridView's selected cell
form2.value = getDataGridValue();
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);
}
}
Notice that we're using INotifyPropertyChanged so include this declaration inside Form2
public partial class Form2 : Form, INotifyPropertyChanged
Create public event
public event PropertyChangedEventHandler PropertyChanged;
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));
}
}
Then on Form2Load subscribe to your new event
private void Form2_Load(object sender, EventArgs e)
{
PropertyChanged += new PropertyChangedEventHandler(Form2_PropertyChanged);
}
And do the stuff
void Form2_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
this.InvokeIfRequired((value) => textBox1.Text = value, e.PropertyName);
}
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...
Upvotes: 2
Reputation: 7082
You cannot pass the string
by creating Form New Instance
by some reason
try this one.. in your Form1
try to show the Form2 by this code.
using (var f = new Form2
{
Owner = this
}) f.ShowDialog();
then in Form2
event, it should be like this
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();
and from Form1
public void FillTextBoxes(string SupplierID)
{
supplierVO _SupplierVo = new supplierVO();
ListOfSuppliers _ListOfSuppliers = new ListOfSuppliers();
//SupplierID = _ListOfSuppliers.SupplierCode;
MessageBox.Show(SupplierID);
txtSupplierCode.Text = SupplierID;
}
Upvotes: 0
Reputation: 2709
The following line in this code is your issue.
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;
}
You are getting the SupplierID from the parameters of the method. Why are you updating the SupplierID with empty value. When you create a new object of ListOfSuppliers, then the SupplierCode in that class is empty and you are assigning an empty value to your SupplierID.
Also I can't see the purpose of the variable _ListOfSuppliers???
Upvotes: 0