使用Button更改datagridview单元格中的值,然后在SQL数据库中保存更改 [英] Use Button to change value in datagridview cell and then save changes in SQL database

查看:96
本文介绍了使用Button更改datagridview单元格中的值,然后在SQL数据库中保存更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Windows窗体上有一个dataGridVew1,我使用在我的案例sda中命名的SQL Adapter从SQL填充。您可以看到我如何在下面更新我的网格(也许我做错了):

I have a dataGridVew1 on my windows form that I am populating from SQL using SQL Adapter named in my case "sda". You can see how I update my grid below (maybe I am doing something wrong):

SqlConnection con = new SqlConnection("connection string"); //defining connection
con.Open();
string sql_command = "Select * from Test_Table";
SqlCommand command = new SqlCommand(sql_command, con); // defining the command
DataSet set = new DataSet("SQL_table");
SqlDataAdaptersda = new SqlDataAdapter(command); //defining the adapter and make it accept changes
    sda.AcceptChangesDuringFill = true;
    sda.AcceptChangesDuringUpdate = true;
    set.Clear(); //just to make sure my adapter is empty
cmdBuilder = new SqlCommandBuilder(sda); //creating the command builder so I can save the changes
    sda.Fill(set, "SQL_table");  // fill the dataset
    dataGridView1.DataSource = set;
    dataGridView1.DataMember = "SQL_table"; //fill datagrid
    dataGridView1.CellValueChanged -= dataGridView1_CellValueChanged;
    dataGridView1.CellValueChanged += dataGridView1_CellValueChanged; //look for cell value changed (I am using this in other scope)





当我手动将值更改为网格的任何单元格时,我使用按钮将更改保存到数据库:





When I manually change a value to any cell of the grid I use a button to save the changes to the database using:

private void button2_Click(object sender, EventArgs e)
{
    sda.Update(set.Tables["SQL_table"]);
}





完美无缺。我添加了第二个按钮来更改所选行的数据网格中的单元格值,但是我无法保存这些更改。





It works perfectly. I have added a second button to change a cell value in the datagrid for the selected row, but I have trouble saving those changes.

 private void button3_Click(object sender, EventArgs e)
{


    if (dataGridView1.SelectedRows.Count > 0) //I am checking to see if any row is selected
    {
        row_index = this.dataGridView1.SelectedRows[0].Index; //to get the selected row index

        this.dataGridView1.Rows[row_index].Cells[2].Value = "change"; //I am changing the cell text on column 2 on the selected row
        dataGridView1.EndEdit(); //added this to try an make it work

            sda.Update(set.Tables["SQL_table"]); //trying to update the database

    }
    else
        MessageBox.Show("Please first select the row with the quote that is awaiting feedback."); // in case no row is selected

}





这不会将数据保存到数据库中。我之后也尝试按下其他保存按钮,但没有结果,即使网格中显示已更改值,也没有数据保存在数据库中。



I知道我不能将save命令放在CellValueChanged事件中,因为它不会进行最后一次更改。如何在上面提到的单独按钮中使其工作?我需要的是:1。按下按钮 - 更改单元格的值2.提交数据库中的更改。



谢谢。

Danut



This does not save the data to the database. I also tried pressing the other save button afterward but no result, no data is saved in the database even if the value "changed" is visible in the grid.

I know I cannot put the save command in the CellValueChanged event because it does not take the last change. How can I make it work in a separate button as mentioned above? What I need is to: 1. Press the button - that changes the value of a cell 2. commit the changes in the database.

Thank you.
Danut

推荐答案

这篇关于使用Button更改datagridview单元格中的值,然后在SQL数据库中保存更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆