首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

非常着急关于winform里datagridview的有关问题

2012-02-27 
非常着急,关于winform里datagridview的问题datagridview里面数据修改后,如何自动保存到数据库?我用的是acc

非常着急,关于winform里datagridview的问题
datagridview里面数据修改后,如何自动保存到数据库?
我用的是access   2003
private   void   gridMain_CurrentCellChanged(object   sender,   EventArgs   e)
{
                        Validate();
                        mainBindingSource.EndEdit();
                        mainTableAdapter.Update(pricingDataSet.Main);
}
该段代码不起作用,数据并没有保存到数据库中

[解决办法]
http://community.csdn.net/Expert/topic/5252/5252439.xml?temp=.9005243
[解决办法]
Adapter.update
[解决办法]
你不会是修改一条保存一条吧...

这样效率也太低了一点吧...

最好的做法是都修改完了,比如单击一个 "更新 "按钮,再保存到数据库中...
[解决办法]
给你个例子,如果你有MSDN,请直接看:
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_fxmclictl/html/1660f69c-5711-45d2-abc1-e25bc6779124.htm

这个例子讲得很清楚了..

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

public class Form1 : System.Windows.Forms.Form
{
private DataGridView dataGridView1 = new DataGridView();
private BindingSource bindingSource1 = new BindingSource();
private SqlDataAdapter dataAdapter = new SqlDataAdapter();
private Button reloadButton = new Button();
private Button submitButton = new Button();

[STAThreadAttribute()]
public static void Main()
{
Application.Run(new Form1());
}

// Initialize the form.
public Form1()
{
dataGridView1.Dock = DockStyle.Fill;

reloadButton.Text = "reload ";
submitButton.Text = "submit ";
reloadButton.Click += new System.EventHandler(reloadButton_Click);
submitButton.Click += new System.EventHandler(submitButton_Click);

FlowLayoutPanel panel = new FlowLayoutPanel();
panel.Dock = DockStyle.Top;
panel.AutoSize = true;
panel.Controls.AddRange(new Control[] { reloadButton, submitButton });

this.Controls.AddRange(new Control[] { dataGridView1, panel });
this.Load += new System.EventHandler(Form1_Load);
this.Text = "DataGridView databinding and updating demo ";
}

private void Form1_Load(object sender, System.EventArgs e)
{
// Bind the DataGridView to the BindingSource
// and load the data from the database.
dataGridView1.DataSource = bindingSource1;
GetData( "select * from Customers ");
}

private void reloadButton_Click(object sender, System.EventArgs e)
{
// Reload the data from the database.
GetData(dataAdapter.SelectCommand.CommandText);
}

private void submitButton_Click(object sender, System.EventArgs e)
{
// Update the database with the user 's changes.
dataAdapter.Update((DataTable)bindingSource1.DataSource);
}

private void GetData(string selectCommand)
{
try
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample


// database accessible to your system.
String connectionString =
"Integrated Security=SSPI;Persist Security Info=False; " +
"Initial Catalog=Northwind;Data Source=localhost ";

// Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);

// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;

// Resize the DataGridView columns to fit the newly loaded content.
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (SqlException)
{
MessageBox.Show( "To run this example, replace the value of the " +
"connectionString variable with a connection string that is " +
"valid for your system. ");
}
}

}

热点排行