询问者
c#中DataGridView.DataError事件不能触发

常规讨论
-
代码如下:我在数据库中有个字段是唯一约束,但是不论我在DataGridView里面输入还是保存到数据库,DataGridView.DataError事件始终不能触发,请朋友们帮忙看看,谢谢
public partial 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();
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";
this.dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError);
}private void Form1_Load(System.Object sender, System.EventArgs e)
{
dataGridView1.DataSource = bindingSource1;
GetData("select * from Cashier");}
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 =
"Server=hp1;Database=slzDB2009;uid=sa;pwd=420123;pooling =true;connection lifetime=50;min pool size=1;max pool size=10";// 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);
dataAdapter.FillSchema(table, SchemaType.Mapped);
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.");
}
}
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
// If the data source raises an exception when a cell value is
// commited, display an error message.
if (e.Exception != null &&
e.Context == DataGridViewDataErrorContexts.Commit)
{
MessageBox.Show("CustomerID value must be unique.");
}
}- 已移动 Sheng Jiang 蒋晟Moderator 2009年3月21日 15:41 Windows表单数据绑定控件问题 (从 Visual C# 移动到 .NET Framework 一般性问题讨论区)