-1

On one form I have a dgv with a column that has a button that a user can click, and it takes them to another form, where more information about that particular item is displayed (read-only from db).

Here is the code for the cell button click:

   private void datagridview1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        //MORE INFORMATION BUTTON DGV1
        this.rowIndex1 = e.RowIndex;
        this.ID = Convert.ToInt32(datagridview1.Rows[this.rowIndex1].Cells["id"].Value);
        moreinformation moreinfo1 = new moreinformation();
        moreinfo1.Show();
        }

I'm using public int ID { get; set; }, to pass the ID of the particular item to the other form, so that it could fetch the correct data from the database to the 'more information' form.

Here is the code for the information page:

private void moreinformation_Load(object sender, EventArgs e)
        {
        //DISPLAY RELEVANT INFO FROM DB
        invmain invmain2 = new invmain();

        Connection();
        sqlconnection.Open();

        sqlcmd = new SqlCommand("SELECT * FROM inventory_table WHERE id= " + invmain2.ID, sqlconnection);

        using (SqlDataReader sqldr = sqlcmd.ExecuteReader())
        {
            while (sqldr.Read())
            {
                txtbx1.Text = (sqldr["item"].ToString());
                txtbx2.Text = (sqldr["quantity"].ToString());
                linklbl1.Text = (sqldr["datasheet"].ToString());
                txtbx3.Text = (sqldr["description"].ToString());
                datelbl2.Text = (sqldr["date"].ToString());
            }

            sqldr.Close();
         }

        sqlconnection.Close();
       }

I checked to make sure that ID (on the first form), was properly getting set to the correct ID (based on the item). However, on the more information form, if I include some message box code to display the ID: MessageBox.Show(invmain2.ID.ToString());, it shows that ID is 0.

Whats going wrong and how can I fix it?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
John
  • 447
  • 2
  • 8
  • 20
  • 1
    You never pass the ID to the second form. The ID properties on the two forms are in no way linked automagically by .NET. You must set the ID on the more information form. – Matthew Jaspers Mar 24 '15 at 21:07
  • @BenJaspers How do I do that? ...because I've used this same method on a different form to add the items to the dgv, and it's worked. – John Mar 24 '15 at 21:10
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 24 '15 at 21:17

1 Answers1

1

The ID property should be on the moreinformation form. If you don't have it, go ahead and add public int ID { get; set; } to that form. Then you have to pass the ID to the instance of the second form. So if the you have a moreinformation.ID property, set it up as below:

private void datagridview1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        //MORE INFORMATION BUTTON DGV1
        this.rowIndex1 = e.RowIndex;
        this.ID = Convert.ToInt32(datagridview1.Rows[this.rowIndex1].Cells["id"].Value);
        moreinformation moreinfo1 = new moreinformation();
        //ADD THIS LINE
        moreinfo1.ID = this.ID;
        moreinfo1.Show();
        }
GWLlosa
  • 23,995
  • 17
  • 79
  • 116