如何通过选择datagridview行和更新按钮来更新数据库

时间:2016-09-27 16:05:12

标签: c# datagridview

我想更新数据库记录。 我有两种形式,一种是主要形式,另一种是更新形式。 现在我想要什么?主表单包含DataGridView并更新Button。当我在Row中选择DataGridView并按更新Button时,此内容应加载到更新表单的TextBox个。

注意:我可以通过CellcontentClick活动来完成,但我想通过更新Button来完成。

public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        update_form fm = new update_form();
        DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
        fm.Show();

        fm.id_txt.Text = row.Cells[0].Value.ToString();
        fm.name_txt.Text = row.Cells[1].Value.ToString();
        fm.price_txt.Text = row.Cells[2].Value.ToString();
        fm.discount_txt.Text = row.Cells[3].Value.ToString();
        fm.quantity_txt.Text = row.Cells[4].Value.ToString();
    }

1 个答案:

答案 0 :(得分:0)

只需在“更新按钮”单击事件中添加以下代码:

if (dataGridView1.SelectedRows.Count > 0)
{
    update_form fm = new update_form();
    DataGridViewRow row = this.dataGridView1.SelectedRows[0];
    fm.Show();

    fm.id_txt.Text = row.Cells[0].Value.ToString();
    fm.name_txt.Text = row.Cells[1].Value.ToString();
    fm.price_txt.Text = row.Cells[2].Value.ToString();
    fm.discount_txt.Text = row.Cells[3].Value.ToString();
    fm.quantity_txt.Text = row.Cells[4].Value.ToString();
}

致谢。

相关问题