【问题标题】:Button to Datagridview to textbox按钮到 Datagridview 到文本框
【发布时间】:2016-02-14 04:38:08
【问题描述】:
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会被调用!

【问题讨论】:

  • 显示按钮代码。

标签: c# button datagridview textbox


【解决方案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();
       }

【讨论】:

  • 字符串 x = button1_Click();?我该怎么做?
  • 我不明白你真正想要做什么,因为方法不能存储在字符串变量中!那么,能否请您解释一下您在语句中实际想要做什么,string x = button1_Click();,以便我可以帮助您?
【解决方案2】:
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。

【讨论】:

  • 什么都没有发生,它只会禁用 cellclick
  • 现在必须工作 :)
  • 错误,我认为是因为 button1 有 7 列,而 button2 有 6 列。
  • 我不知道您的错误,但我认为您在类别中的第一个数据表首先比另一个类别中的数据表具有更多或更少的列。所以检查这些类别并确保您的数据表具有相同的列索引。
【解决方案3】:

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

你处理了吗?

【讨论】:

  • 我在 button2 中的代码与 button1 相同,但“附件”是“其他”
  • 现在必须工作 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
  • 2016-12-06
相关资源
最近更新 更多