2016-02-14 66 views
1
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按鈕的DataGridView到文本框

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的,它是正確的,但是當我點擊按鈕2,按鈕1獲得來電!

+0

顯示按鈕代碼。 – Mairaj

回答

1

如果我理解正確,那麼if語句中存在問題。

如果button1上的文本是1,並且無論點擊哪個按鈕,該if語句都會被執行。

要解決此問題,請使用整數變量並將其中的不同值保存在button1_Clickbutton2_Click事件中,並在if語句中使用這些值而不是按鈕上的文本。

這可以是樣本代碼:

按鈕單擊事件:

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

string x = button1_Click();?我怎樣才能做到這一點? – Perfectionist

+0

我不明白你真正想做什麼,因爲一個方法不能存儲在一個字符串變量中!那麼,你能解釋一下你在聲明中想要做什麼嗎?string x = button1_Click();這樣我可以幫你嗎? –

+0

哇,它的作品!所以它唯一的INT畢竟是:O – Perfectionist

0

原因buttn1仍然有TEXT==1,我不知道buttn2下你的代碼是什麼..!

你有沒有處理它?

+0

我在button2中的代碼與button1相同,但「附件」是「其他」 – Perfectionist

+0

其必須立即工作:) –

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

     } 
    } 

我只是沒有簡單的方法來控制UR點擊事件處理。

+0

沒有什麼是發生,它只是禁用單元格 – Perfectionist

+0

其必須立即工作:) –

+0

錯誤,我認爲,因爲button1有7列和button2是6columns。 – Perfectionist