Hi!
I am designing a form which contains a datagridview which can display all customer orders. I also have a button inside the datagridview so that if a user clicks the button it will open another form which contains the items which the customer ordered in a datagridview.
I am using the code below on the first form.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

                    Item i = new Item();
                    i.Show();



        }

Item is the name of the second form.
The second form contains the following code in the form load event.

SqlConnection con = new SqlConnection(Properties.Settings.Default.Stock_ControlConnectionString);
            try
            {
                con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Error with the database Connection");
            }
            try
            {
                string query = "Select * From Order_Items_Table ";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
            }
            catch (Exception)
            {
                MessageBox.Show("Error Occured");
            }

        }

The code in the second form will display all the items on the Order Items Table. I want a c# code which can pass the orderID value which is the first cell value in the datagrid from the first form to the second form so that the item form will display the data according to the orderID
I hope someone can help me out in this.
Thanks in advance.
Mirfath

You can declare a public variable say var1 in the second form and call as below

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    Item i = new Item();
    i.var1 = "your value"; 
    i.Show();
}

In your firs form you do this

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        Item i = new Item();
        i.GetItem = dataGridView1[0].value(); // this get the first index of the grid which is the id you want to pass to other form.
        i.show();
     }

In your second form:

    private int id;
    public int GetItem
    {
        get{ return id;}
        set{ id = value;}
    } // now the id variable has contains it from your first form.

Hope this can help with your problem.sorry for my poor english.

Hi,
I think you had binded your gridview with database then use following code.
/*
Create One Property in Item Form which Hold the Value which you give from datagridview
*/

private int _itemId;
public int ItemID
{
get{ return _itemId;}
set{ _itemId = value;}
}


 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// Click on ColumnHeader and RowHeader 
if (e.ColumnIndex < 0 || e.RowIndex < 0) { return; }
Item _item = new Item();
_item.ItemID = dataGridView1["ID", e.RowIndex].Value;/* Column Name "ID" Which Contains Value which you Clicked*/
_item.Show();
}

And Use/Pass _itemId Value in your FormLoad Event/or Pass to Select Statement to retrive Data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.