[英]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();
}
}
按鈕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會接到電話!
如果我正確理解,則if語句中有問題。
如果button1
上的文本為1
,則將執行if語句,無論單擊哪個按鈕,它都保持為true。
要解決此問題,請使用整數變量,並將不同的值保存在button1_Click
和button2_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();
}
因為buttn1
仍然具有TEXT==1
,所以我不知道您在buttn2
下的代碼是buttn2
。
你處理好了嗎?
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();
}
}
我只是做了簡單的方法來控制您的Click EventHandler。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.