在 datagridView控件中怎么可以让它实现自动保存用户修改过的信息呢?
下面有一个例示代码:
程序的界面如下:
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace dataGridView自动修改
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlDataAdapter adapter = null;
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
string command = "select * from Books";
string connection = "server=chujuServer ; database=Library2 ; integrated security=sspi ";
SqlConnection cn = new SqlConnection(connection);
adapter = new SqlDataAdapter(command, cn);
adapter.Fill(ds, "Books");
this.dataGridView1.DataSource = ds.Tables["Books"];
}
private void button1_Click(object sender, EventArgs e)
{
SqlCommandBuilder buder = new SqlCommandBuilder(adapter);
adapter.Update(ds.Tables["Books"]);
//刷新一次数据表
this.dataGridView1.DataSource = ds.Tables["Books"];
//提示信息出现
this.label1.Visible = true;
this.timerAlreadySave.Start();
}
/// <summary>
/// 以保存信息计时器的触发事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timerAlreadySave_Tick(object sender, EventArgs e)
{
this.label1.Visible = false;
this.timerAlreadySave.Stop();
}
}
}
主要是利用 sqlDataAdapter 和 sqlCommandBuilder的功能