C# 如何编程按钮以添加具有值的DataGrid行?

C# 如何编程按钮以添加具有值的DataGrid行?,c#,winforms,datagridview,submit,C#,Winforms,Datagridview,Submit,我的程序右侧有一个电子邮件字段、密码字段和DataGridView。还有一个提交按钮。submit按钮编辑DataGridView中的预设行,但如果单击两次,则只会重新编辑第一行的内容 dataGridView1.Rows[0].Cells[0].Value = txtEmail.Text; dataGridView1.Rows[0].Cells[1].Value = txtPass.Text; 我想知道如何编程该按钮以添加一个值为txtmail和txtPass的新行。是否有一套通过按钮添加行

我的程序右侧有一个电子邮件字段、密码字段和DataGridView。还有一个提交按钮。submit按钮编辑DataGridView中的预设行,但如果单击两次,则只会重新编辑第一行的内容

dataGridView1.Rows[0].Cells[0].Value = txtEmail.Text;
dataGridView1.Rows[0].Cells[1].Value = txtPass.Text;
我想知道如何编程该按钮以添加一个值为
txtmail
txtPass
的新行。是否有一套通过按钮添加行的方法?完成后,如何编辑最近添加的行?这将在每次提交数据时显示一个包含新电子邮件和新通行证的新行


有什么想法吗?

假设您的
DataGridView
只有两列,并且它的
DataSource
属性没有绑定到某个集合或
DataTable
,这应该可以工作:

private void btnAddInputToGrid_Click(object sender, EventArgs e)
{
    // add the new row, get its index
    var newIndex = dataGridView1.Rows.Add(txtEmail.Text, txtPass.Text);

    // select just the new row
    dataGridView1.ClearSelection();
    dataGridView1.Rows[newIndex].Selected = true;

    // select the cell you're interested in and put it in edit mode
    dataGridView1.CurrentCell = dataGridView1.Rows[newIndex].Cells[0];
    dataGridView1.BeginEdit(false);
}

最好是在C#方面非常有经验的人,谢谢。人们如何编辑我的帖子,但却不能回答他们修改过的问题,真是太可笑了。谢谢你,格兰特,这很有帮助,而且成功了。但是,我的DataGrid以一个空行开始?所以当我使用你的代码时,第一个添加到列表中的帐户就在下面?如何删除此默认行?再次感谢