-1

I have a search form in my program. When the user double-clicks a cell (of the dgv) on the search form, I want the program to close that form and jump to the item on the main form.

I'm doing this by identifying each item with a unique id.

I'm trying to pass the value of the rows id to the other form. The problem is that, it says that I'm passing the value zero each time. But when I insert some message boxes on the search form, it says that the integer 'id' has successfully been assigned to the variable on the main form: public int CellDoubleClickValue { get; set; }

Here is my code:

Search Form:

    private int id;

    private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        this.rowIndex1 = e.RowIndex;
        this.id = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
        invmain inv = new invmain();
        inv.CellDoubleClickValue = this.id;
        this.DialogResult = DialogResult.OK;
        this.Close();
        //MessageBox.Show(inv.CellDoubleClickValue.ToString()); 
        //Above, it shows it got assigned successfully.
    }

Main Form:

    public int CellDoubleClickValue { get; set; }

    private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
        search form1 = new search();
        form1.ShowDialog();

        if (form1.DialogResult == DialogResult.OK)
        {
            MessageBox.Show(CellDoubleClickValue.ToString());
        }//Here it shows that the value is: 0
John
  • 447
  • 2
  • 8
  • 20
  • Make property of id field and get value from it in main form – Ehsan Sajjad Apr 01 '15 at 16:19
  • possible duplicate of [How to pass values between forms in c# windows application?](http://stackoverflow.com/questions/1205195/how-to-pass-values-between-forms-in-c-sharp-windows-application) – Orace Apr 01 '15 at 16:19
  • @Orace This is not a duplicate, because I've used this method before and it's worked. But for some reason its not working now. – John Apr 01 '15 at 16:20
  • @EhsanSajjad What do you mean by that? (I've already tried making 'id' as `{ get; set; }`. – John Apr 01 '15 at 16:22
  • It looks like you're dealing with two instances of the main form and expecting them to be the same instance. I'm guessing the first instance is created on app start-up and the second instance is created in `searchdgv_CellDoubleClick`. – Daniel Pratt Apr 01 '15 at 16:23
  • @Orace If it is a duplicate, you should be able to tell me the solution, right? – John Apr 01 '15 at 16:23
  • @DanielPratt No, that is in fact the first and only instance of the main form. – John Apr 01 '15 at 16:29
  • 1
    @John I've given you a solution that will work, but I don't think it will help with your confusion. Your mental model of how forms work is not correct. – Daniel Pratt Apr 01 '15 at 16:45
  • 1
    First of all, there is an instance of the 'main' form that is created in code that you don't show here. I'm fairly certain that is happening in app start-up code. That instance of the main form in turn creates and displays the search form (in the `searchToolStripMenuItem_Click` event handler). Then you create a _second_ instance of the main form in the `searchdgv_CellDoubleClick` event handler in the search form. You are evidently thinking that the declaration `invmain inv = new invmain();` creates a reference to the already open main form, but it does not. It is a separate instance. – Daniel Pratt Apr 01 '15 at 16:48

1 Answers1

3

I suggest you do as follows:

Search Form:

public int SelectedId { get; set; }

private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    this.rowIndex1 = e.RowIndex;
    this.SelectedId = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
    this.DialogResult = DialogResult.OK;
    this.Close();
}

Main form:

private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
    search form1 = new search();
    form1.ShowDialog();

    if (form1.DialogResult == DialogResult.OK)
    {
        int selectedId = form1.SelectedId;
        // Do whatever with selectedId...
    }
Daniel Pratt
  • 12,007
  • 2
  • 44
  • 61