[英]updating/editing data from dataGridView to ms access database in c#
I have a problem that am trying to solve for hours. 我有一个试图解决数小时的问题。 Its 2 in the morning and even after reading tones of articles I still cannot solve the problem. 它是凌晨2点,即使在阅读文章的提示音后,我仍然无法解决问题。 I have c# windows app that loads data from ms access database into a dataGridView. 我有C#Windows应用程序,它将来自ms访问数据库的数据加载到dataGridView中。 I can succesfully load it, add new records and save it. 我可以成功加载它,添加新记录并保存它。 However when I try to edit/remove rows and then save it I obtain error: Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information. 但是,当我尝试编辑/删除行然后保存它时,出现错误:对于不返回任何键列信息的SelectCommand,不支持DeleteCommand的动态SQL生成。
I have no idea what to do. 我不知道该怎么做。 I'd really appreciate some help! 我真的很感谢您的帮助!
public class UserInterfaceForm: Form
{
//db connection
String connString;
OleDbDataAdapter dAdapter;
OleDbCommandBuilder cBuilder;
DataTable dTable = new DataTable();
BindingSource bSource;
//buttons
private Button SavePermitButton;
private Button loadPermitButton;
private Button exitButton;
private Button deletePermitButton;
//GridView
private DataGridView allPermitsDataGridView;
public UserInterfaceForm()
{
InitializeComponent();
}
void InitializeComponent()
{
this.SavePermitButton = new System.Windows.Forms.Button();
this.loadPermitButton = new System.Windows.Forms.Button();
this.allPermitsDataGridView = new System.Windows.Forms.DataGridView();
this.exitButton = new System.Windows.Forms.Button();
this.deletePermitButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.allPermitsDataGridView)).BeginInit();
this.SuspendLayout();
//
// SavePermitButton
//
this.SavePermitButton.Location = new System.Drawing.Point(115, 275);
this.SavePermitButton.Name = "SavePermitButton";
this.SavePermitButton.Size = new System.Drawing.Size(97, 25);
this.SavePermitButton.TabIndex = 3;
this.SavePermitButton.Text = "Save permit";
this.SavePermitButton.UseVisualStyleBackColor = true;
this.SavePermitButton.Click += new System.EventHandler(this.SavePermitButton_Click);
//
// loadPermitButton
//
this.loadPermitButton.Location = new System.Drawing.Point(12, 275);
this.loadPermitButton.Name = "loadPermitButton";
this.loadPermitButton.Size = new System.Drawing.Size(97, 25);
this.loadPermitButton.TabIndex = 4;
this.loadPermitButton.Text = "Load permit";
this.loadPermitButton.UseVisualStyleBackColor = true;
this.loadPermitButton.Click += new System.EventHandler(this.readPermitButton_Click);
//
// allPermitsDataGridView
//
this.allPermitsDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.allPermitsDataGridView.Location = new System.Drawing.Point(12, 12);
this.allPermitsDataGridView.Name = "allPermitsDataGridView";
this.allPermitsDataGridView.Size = new System.Drawing.Size(839, 239);
this.allPermitsDataGridView.TabIndex = 8;
//
// exitButton
//
this.exitButton.Location = new System.Drawing.Point(752, 275);
this.exitButton.Name = "exitButton";
this.exitButton.Size = new System.Drawing.Size(99, 25);
this.exitButton.TabIndex = 0;
this.exitButton.Text = "Exit";
this.exitButton.Click += new System.EventHandler(this.cancelButton_click);
//
// deletePermitButton
//
this.deletePermitButton.Location = new System.Drawing.Point(218, 275);
this.deletePermitButton.Name = "deletePermitButton";
this.deletePermitButton.Size = new System.Drawing.Size(97, 25);
this.deletePermitButton.TabIndex = 9;
this.deletePermitButton.Text = "Delete permit";
this.deletePermitButton.UseVisualStyleBackColor = true;
this.deletePermitButton.Click += new System.EventHandler(this.deletePermitButton_Click);
//
// UserInterfaceForm
//
this.ClientSize = new System.Drawing.Size(886, 312);
this.Controls.Add(this.deletePermitButton);
this.Controls.Add(this.allPermitsDataGridView);
this.Controls.Add(this.loadPermitButton);
this.Controls.Add(this.SavePermitButton);
this.Controls.Add(this.exitButton);
this.Name = "UserInterfaceForm";
this.Text = "Parking Permit Application";
((System.ComponentModel.ISupportInitialize)(this.allPermitsDataGridView)).EndInit();
this.ResumeLayout(false);
}
protected void cancelButton_click(object sender, EventArgs e)
{
Application.Exit();
}
private void readPermitButton_Click(object sender, EventArgs e)
{
connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Arek\Desktop\Parking Permit Application - Assignment 3\Assignment_3_OOPARDGM.accdb;
Persist Security Info=False;";
dAdapter = new OleDbDataAdapter("Select * From Permits", connString);
cBuilder = new OleDbCommandBuilder(dAdapter);
dAdapter.Fill(dTable);
bSource = new BindingSource();
bSource.DataSource = dTable;
allPermitsDataGridView.DataSource = bSource;
}
private void SavePermitButton_Click(object sender, EventArgs e)
{
dAdapter.Update(dTable);
MessageBox.Show("Database successfully saved!");
}
private void deletePermitButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in this.allPermitsDataGridView.SelectedRows)
{
allPermitsDataGridView.Rows.RemoveAt(item.Index);
}
}
}//class
看来您的“许可证”表没有关键字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.