C#保存datagridview中的更改

时间:2016-11-02 19:42:49

标签: c# winforms datagridview

我有DataGridView,当我按下修改按钮然后进行相同的更改并在刷新后保存它有与更改前相同的值。

按钮1 - 插入新记录 按钮2 - 刷新DataGridView 按钮3 - 允许更新DataGridView中的记录 按钮4 - 保存更改 按钮5 - 取消更改

{{1}}

2 个答案:

答案 0 :(得分:0)

这是一个相当全面的CRUD脚本。

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SqlCommand sCommand;
        SqlDataAdapter sAdapter;
        SqlCommandBuilder sBuilder;
        DataSet sDs;
        DataTable sTable;        

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True";
            string sql = "SELECT * FROM Stores";
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            sCommand = new SqlCommand(sql, connection);
            sAdapter = new SqlDataAdapter(sCommand);
            sBuilder = new SqlCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "Stores");
            sTable = sDs.Tables["Stores"];
            connection.Close();
            dataGridView1.DataSource = sDs.Tables["Stores"];
            dataGridView1.ReadOnly = true;
            save_btn.Enabled = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void new_btn_Click(object sender, EventArgs e)
        {
            dataGridView1.ReadOnly = false;
            save_btn.Enabled = true;
            new_btn.Enabled = false;
            delete_btn.Enabled = false;
        }

        private void delete_btn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                sAdapter.Update(sTable);
            }
        }

        private void save_btn_Click(object sender, EventArgs e)
        {
            sAdapter.Update(sTable);
            dataGridView1.ReadOnly = true;
            save_btn.Enabled = false;
            new_btn.Enabled = true;
            delete_btn.Enabled = true;
        }
    }
}

答案 1 :(得分:0)

所以在我的情况下,我想做同样的事情,以便用户可以更改员工的状态并将其保存到数据源中!我将我之前的一些知识和试验与 Brad Larson 的回答结合起来,我最终做到了这一点,这对我来说很有魅力!

    SqlCommand sCommand;
    SqlDataAdapter sAdapter;
    SqlCommandBuilder sBuilder;
    DataSet sDs;
    DataTable sTable;
    private void StatusEmployeeForm_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'nestorConsultingDBDataSet.Employee' table. You can move, or remove it, as needed.
        string connectionString = your connection string
        string sql = "SELECT * FROM Employee";
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();
        sCommand = new SqlCommand(sql, connection);
        sAdapter = new SqlDataAdapter(sCommand);
        sBuilder = new SqlCommandBuilder(sAdapter);
        sDs = new DataSet();
        sAdapter.Fill(sDs, "Employee");
        sTable = sDs.Tables["Employee"];
        connection.Close();
        employeeData.DataSource = sDs.Tables["Employee"];
    }

    private void saveBtn_Click(object sender, EventArgs e)
    {
        this.employeeBindingSource.EndEdit();
        this.sAdapter.Update(sTable);
        MessageBox.Show("Changes saved!");
    }

请注意,您还需要插入“using.System.Data.SqlClient;”在代码的顶部,当然将 connectionString 更改为正确的连接字符串!