按钮到Datagridview到文本框

时间:2016-02-14 04:38:09

标签: c# button datagridview 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();
       }
 }

按钮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;
    }

如果我点击button1,它是正确的,但当我点击button2时,button1会接到电话!

3 个答案:

答案 0 :(得分:1)

如果我理解正确,你会在if语句中遇到问题。

如果button1上的文字为1,则会执行您的if语句,无论点击哪个按钮,该文本都会保持为真。

要解决此问题,请使用整数变量并在button1_Clickbutton2_Click事件中保存其中的不同值,并在if语句中使用这些值,而不是按钮上的文本。 / p>

这可以是示例代码:

按钮点击事件:

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;
}

if语句:

        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();
       }

答案 1 :(得分:0)

因为buttn1仍然有TEXT==1而且我不知道buttn2下的代码是什么..!

你操了吗?

答案 2 :(得分:0)

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();

        }
    }

我只是简单地控制你的方法点击EventHandler。