C# 添加、更新、删除DataGrid中表上的数据

C# 添加、更新、删除DataGrid中表上的数据,c#,winforms,datagridview,sql-server-ce,C#,Winforms,Datagridview,Sql Server Ce,我已经使用以下代码将数据添加到表中(使用DataGrid上的SqlCE): private void btn_functions_Click(object sender, EventArgs e) { string conSTR = @"Data Source=C:\Users\Zet\DB\javascript_db.sdf"; SqlCeConnection connection = new SqlCeConnection(co

我已经使用以下代码将数据添加到表中(使用DataGrid上的SqlCE):

private void btn_functions_Click(object sender, EventArgs e)
        {
            string conSTR = @"Data Source=C:\Users\Zet\DB\javascript_db.sdf";
            SqlCeConnection connection = new SqlCeConnection(conSTR);

            string sql = "SELECT * FROM Functions";
            connection.Open();

            SqlCeCommand cmd = new SqlCeCommand(sql, connection);
            SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            //for datagridview for js
            dg_JS1.DataSource = ds.Tables[0];
            connection.Close();
        }
(您可以在此处查看结果:)

问题是我不知道如何将特定单元格直接添加/编辑/删除到datagrid本身。并直接保存到数据库中的我的表中

示例场景将有3个按钮用于添加/编辑/删除,然后假设我在单元格上添加文本,然后当我按btn_添加时,数据应添加到datagrid并直接到表中,如果我按btn_编辑,我更新的所有数据应从datagrid更新到db,如果删除,我在单元格中选择的所有数据/文本将自动删除,与表中的相同

或者其他想法可以是

知道我该怎么做吗


提前多谢!更多的力量

我没按按钮就解决了这个问题。在下面的代码中,我将给出一个连接和更新如何与mysql数据库(运行时更新)一起工作的示例:

代码

DataTable dt = null;
DataGridView dgv1 = null;
如果表单加载,则必须将dt变量设置为新的datatable:

    private void Form1_Load(object sender, EventArgs e)
{
    dt = new DataTable();



    using (MySqlConnection conn = new MySqlConnection("datasource=localhost;port=3306;username=root;password=1234"))
    {
        conn.Open();
        using (MySqlCommand cmd = new MySqlCommand())
        {
            cmd.Connection = conn;
            cmd.CommandText = "select *from try.data ;";
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            da.Fill(dt);
        }
        conn.Close();
    }

    dgv1 = new DataGridView();
    dgv1.AllowUserToAddRows = false;
    dgv1.CellEndEdit += new DataGridViewCellEventHandler(dgv_CellEndEdit);
    dgv1.CellValidating += new DataGridViewCellValidatingEventHandler(dgv_CellValidating);
    dgv1.Dock = DockStyle.Fill;
    dgv1.DataSource = dt;

    this.Controls.Add(dgv1);
}
您必须设置两个事件:CellValidating

private void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        InitializeComponent();
        if (e.ColumnIndex == 0)
        {
            dgv1.CancelEdit();
        }
    }
以及CellValidating事件:

private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        string id = dt.Rows[e.RowIndex]["Eid"] + "";
        string col = dt.Columns[e.ColumnIndex].ColumnName;
        string data = dgv1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value + "";

        string sql = string.Format("UPDATE `try`.`data` SET `{0}` = '{1}' WHERE Eid = {2};", col, data, id);

        using (MySqlConnection conn = new MySqlConnection("datasource=localhost;port=3306;username=root;password=1234"))
        {
            conn.Open();
            using (MySqlCommand cmd = new MySqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }
    }

这实际上适用于MySql,但在Sql中是“相等”的组件,如SqlConnection或SqlCommand。。。我希望这能解决你的问题。祝你今天愉快

您必须反过来使用DataTable和DataGrid。将DataTable绑定到DataGrid,当您更改DataTable时,它将反映在DataGrid中。我如何才能做到这一点?对不起,有点混淆了你在@Mikedklerk说的话