C# Datagridview到文本框的按钮
按钮1C# Datagridview到文本框的按钮,c#,button,datagridview,textbox,C#,Button,Datagridview,Textbox,按钮1 private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { if (button1.text == "1")//its a category { int i; i = dataGridView1.SelectedCells[0].RowIndex; textBox7.Tex
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,它是对的,但是当我点击按钮2,按钮1得到呼叫 原因
buttn1
仍然有TEXT==1
,我不知道您的代码在buttn2
下是什么
你处理好了吗?如果我理解正确,你的If语句有问题 如果
button1
上的文本为1
,则执行if语句,无论单击哪个按钮,该语句都保持为true
要解决此问题,请使用整数变量并将不同的值保存在按钮1\u单击
和按钮2\u单击
事件中,并在if语句中使用这些值,而不是按钮上的文本
这可以是示例代码:
按钮单击事件:
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;
}
国际单项体育联合会声明:
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;
}
我只是做了一个简单的方法来控制你的点击事件处理程序。显示按钮的代码。我在按钮2中的代码与按钮1相同,但“附件”是“其他”它现在必须工作:)字符串x=button1_Click();?我怎么能做到呢?我不明白你到底想做什么,因为方法不能存储在字符串变量中!那么,你能解释一下你到底想在语句中做什么吗,string x=button1_Click();,所以我可以帮你吗?什么都没有发生,它只是禁用了cellclickits现在必须工作:)错误,我想,这是因为按钮1有7列,按钮2有6列。我不知道你的错误,但我认为类别first中的第一个数据表的列数比另一个类别中的数据表的列数多或少,所以请检查这些类别并确保数据表的列数和索引数相同。
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();
}
}