Perfectionist
Perfectionist

Reputation: 54

Button to Datagridview to textbox

private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)

    {
        if (button1.text == "1")//its a category
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            textBox7.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
            textBox6.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
            textBox5.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
            textBox3.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
            textBox8.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
            textBox9.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
        }
        if (button2.text == "2")//another category
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            textBox7.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
            textBox6.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
            textBox5.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
            textBox3.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
            textBox9.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
       }
 }

the button 1

   private void button1_Click(object sender, EventArgs e)

    {
        SqlDataAdapter sda = new SqlDataAdapter(@"Select * from Accessories", con);
        DataTable dt = new DataTable ();
        sda.Fill(dt);
        dataGridView1.DataSource = dt;
    }

If I click the button1, it's right, but when I click button2, button1 get call!

Upvotes: 1

Views: 151

Answers (3)

The_Mind_Hunter
The_Mind_Hunter

Reputation: 79

cause buttn1 still have the TEXT==1 and I don't know what is your code under of buttn2..!

Did u handle it?

Upvotes: 0

Anas Imran Tasadduq
Anas Imran Tasadduq

Reputation: 594

If I understood it correctly, you have a problem in the if statements.

Your if statement gets executed if the text on button1 is 1, which remains true regardless of which button is clicked.

To solve the problem, use an integer variable and save different values in it in the button1_Click and button2_Click events, and use these values in the if statements instead of the text on the buttons.

This can be the sample code:

Button Click events:

int code = 1;
private void button1_Click(object sender, EventArgs e)
{
    //your code
    code = 1;
}
private void button1_Click(object sender, EventArgs e)
{
    //your code
    code = 2;
}

The if statements:

        if (button1.text == "1" && code == 1)//its a category
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            textBox7.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
            textBox6.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
            textBox5.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
            textBox3.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
            textBox8.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
            textBox9.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
        }
        if (button2.text == "2" && code == 2)//another category
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            textBox7.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
            textBox6.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
            textBox5.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
            textBox3.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
            textBox9.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
       }

Upvotes: 1

The_Mind_Hunter
The_Mind_Hunter

Reputation: 79

private void button1_Click(object sender, EventArgs e)
    {
        SqlDataAdapter sda = new SqlDataAdapter(@"Select * from Accessories", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        dataGridView1.DataSource = dt;


         Button1Click = true;
    }
    bool Button1Click = false;
    bool Button2Click = false;
    private void button2_Click(object sender, EventArgs e)
    {
        /////another category
         Button2Click = true;

    }

    private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (button1.text == "1" && Button1Click)//its a category
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            textBox7.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
            textBox6.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
            textBox5.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
            textBox3.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
            textBox8.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
            textBox9.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();


        }

        if (button2.text == "2" && Button2Click)//another category
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            textBox7.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
            textBox6.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
            textBox5.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
            textBox3.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
            textBox9.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();

        }
    }

I just did Simple way to Control ur Click EventHandler.

Upvotes: 0

Related Questions