自动将值从文本框复制到datagridview

时间:2016-07-31 09:42:17

标签: c# datagridview textbox

在按钮单击事件中,我将此代码复制到datagridview中的三列中。在我的textbox4中,我手动输入行数。有可能自动完成吗?我的意思是选择下一行并复制值?这是我的代码:

private void button1_Click(object sender, EventArgs e)
    {

        int row = int.Parse(textBox4.Text);

        dataGridView1["HomeOdd", row].Value = textBox1.Text;
        dataGridView1["DrawOdd", row].Value = textBox2.Text;
        dataGridView1["AwayOdd", row].Value = textBox3.Text;

        dataGridView1.Refresh();


    }

谢谢,祝你星期天太棒了!

1 个答案:

答案 0 :(得分:2)

如果要在datagridview

的末尾添加数据
private void button1_Click(object sender, EventArgs e)
{
    //it will at end of datagridview
    int row = (DataGridView1.Rows.Count)+1;

    dataGridView1["HomeOdd", row].Value = textBox1.Text;
    dataGridView1["DrawOdd", row].Value = textBox2.Text;
    dataGridView1["AwayOdd", row].Value = textBox3.Text;

    dataGridView1.Refresh();
}

您还可以添加选择

private void button1_Click(object sender, EventArgs e)
{
   //its give you selected row no
    int row = (datagridview.CurrentCell.RowIndex)+1;

    dataGridView1["HomeOdd", row].Value = textBox1.Text;
    dataGridView1["DrawOdd", row].Value = textBox2.Text;
    dataGridView1["AwayOdd", row].Value = textBox3.Text;

    dataGridView1.Refresh();
}