按钮到 Datagridview 到文本框
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接到电话!
因为 buttn1
仍然有 TEXT==1
我不知道你的代码在 buttn2
..!
你处理了吗?
如果我没理解错的话,你的 if 语句有问题。
如果 button1
上的文本是 1
,则执行您的 if 语句,无论单击哪个按钮,它都保持为真。
为了解决这个问题,在 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();
}
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();
}
}
我刚刚做了简单的方法来控制您的点击事件处理程序。
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接到电话!
因为 buttn1
仍然有 TEXT==1
我不知道你的代码在 buttn2
..!
你处理了吗?
如果我没理解错的话,你的 if 语句有问题。
如果 button1
上的文本是 1
,则执行您的 if 语句,无论单击哪个按钮,它都保持为真。
为了解决这个问题,在 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();
}
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();
}
}
我刚刚做了简单的方法来控制您的点击事件处理程序。