使用SQLite数据库更新Datagridview数据

时间:2014-10-24 20:14:31

标签: c# sqlite datagridview

我无法使用按钮更新数据网格视图中的数据。文本是可编辑的,但更改不会保存到SQLite数据库。有任何想法吗?

    private void ProjectsAdmin_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'seniorProjectsDataSet2.DataTable1' table. You can move, or remove it, as needed.
        this.dataTable1TableAdapter.Fill(this.seniorProjectsDataSet2.DataTable1);
    }


    private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == -1 || e.ColumnIndex != 3)  //ignore header row and any column that doesnt have file name
            return;

        var filename = dataGridView1.CurrentCell.Value.ToString();

        if (File.Exists(filename))
            Process.Start(filename);

    }



    private void updateData_Click(object sender, EventArgs e)
    {

        SQLiteConnection conn = new SQLiteConnection();
        dataGridView1.EndEdit();

        dataTable1TableAdapter.Adapter.Update(seniorProjectsDataSet.Tables[0]);


        for (int i = 0; i < seniorProjectsDataSet.Tables[0].Rows.Count; i++)
        {
            seniorProjectsDataSet.Tables[0].Rows[i].AcceptChanges();

        }

    }
}

}

1 个答案:

答案 0 :(得分:1)

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

<强> CODE

    DataTable dt = null;
    DataGridView dgv1 = null;

如果表单加载,则必须将dt变量设置为新的数据表:

        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中是“Equal”组件,如SqlConnection或SqlCommand ......我希望这可以解决您的问题。祝你今天愉快!