将编辑的数据保存在行中

use*_*513 7 c# datagridview winforms

我在一个显示数据库表datagridview.我可以正确地将记录保存datagridview到sql中的数据库中.

现在,我想修改和更改一些记录并将这些更改保存在数据库中.我怎样才能做到这一点?我正在使用datasource附加到数据集的绑定datatable.

private void Form1_Load(object sender, EventArgs e)
{
    this.cPDM0020TableAdapter.Fill(this.cpdm_dataset.CPDM0020);
}

private void btnSave_Click(object sender, EventArgs e)
{
    string code = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString().ToUpper();
    string currency_Name = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString().ToUpper();
    string boolBase = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
    string local_per_Base = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
    string base_per_Local = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
    string insert_sql = "INSERT INTO centraldb.dbo.CPDM0020(Code,Currency_Name,Base,Local_per_Base,Base_per_Local)VAL??UES('" + 
        code + "','" + 
        currency_Name + "','" + 
        boolBase + "','" + 
        local_per_Base + "','" + 
        base_per_Local + "')";

    if (this.ExecuteSql(insert_sql))
    {
        MessageBox.Show("Record Inserted Successfully.");
    }
    else
    {
        MessageBox.Show("Insert Failed");
    }
}

public bool ExecuteSql(string command)
{

    SqlCommand sqlCommand = new SqlCommand(command, connection);
    connection.Open();
    sqlCommand.ExecuteNonQuery();
    this.cPDM0020TableAdapter.Fill(this.cpdm_dataset.CPDM0020);
    dataGridView1.DataSource = cpdm_dataset.CPDM0020;
    sqlCommand.Dispose();
    connection.Close();
    return true;
}
Run Code Online (Sandbox Code Playgroud)

我可以在数据库中轻松保存新条目datagridview,但是我无法编辑已经存在的记录.在编辑后单击"保存"按钮,它会再次显示之前的值.请帮忙.

Fre*_*cer 0

尝试使用以下代码>

try
{
    var row = gvTransactions.CurrentRow;
    int ID= int.parse(row.Cells[0].Value.ToString());
    string abc=row.Cells[0].Value.ToString();
}
catch(exception ex)
{
}
Run Code Online (Sandbox Code Playgroud)

获取这样的值。

注意:catch块中不要写任何东西

然后根据您的需要触发插入/更新查询。