I ve been having a lot of trouble to bind a database (I m using Northwind) to a DataGridView. I ve tried various approaches, but none worked for all operations, only some. I ve also asked on other sites, but so far I haven t gotten any helpful advice.
有没有一个教程真正涵盖了所有CRUD操作(或者几个教程的组合涵盖了所有操作)?
尤其是删除操作让我头疼,因为我得到的唯一提示是将我的删除代码放入某个DataGridView事件中,但问题是我找不到确定用户到底想删除什么的方法,并且不会为删除键触发KeyDown事件。
谢谢
EDIT: Thank you very much. That document is very helpful. I have another question though, I have a DataTable as DataSource for the DataGridView. To update it for execution of user input CRUD operations, do I need to manually insert data into the DataTable or is it enough to just built a regular SQL command with the adapter s DeleteCommand/InsertCommand/etc properties and then just pass the yet unmodified DataTable as argument in the Update method?
也就是说,这会给我带来想要的结果吗?将用户刚刚输入到DataGridView中的值插入到db表中?
private void DGV_Nwind_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
string sql = "INSERT INTO [" + table.TableName + "] VALUES ("; //sql command base
//add values to command
for (int i = 0; i < e.Row.Cells.Count; i++)
{
sql += " " + e.Row.Cells[i].ToString() + " ";
if (i < (e.Row.Cells.Count - 1))
{
sql += ", ";
}
else
{
sql += ")";
}
}
//update table
con.OleAdapter.InsertCommand = new OleDbCommand(sql);
con.OleAdapter.Update(table);
}