2011-12-28 52 views
0

我是C#/数据库领域的新手。我刚刚创建了一个C#项目,将它连接到数据库并从表中填充数据网格。从Datagrid更新/删除数据

到目前为止,我没有编写任何代码,只是使用Visual C#向导和几个拖放操作。我的网格现在显示从表中检索到的数据,但我无法更新或删除行。 那么,如何更新或删除数据库中的行并验证它? 这是我的网页Form1.cs中:

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; 

namespace Learn 
{ 
    public partial class Form1 : Form 
    { 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void userBindingNavigatorSaveItem_Click(object sender, EventArgs e) 
     { 
      this.Validate(); 
      this.userBindingSource.EndEdit(); 
      this.tableAdapterManager.UpdateAll(this.usersDataSet); 
     }  

     private void Form1_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'usersDataSet.User' table. You can move, or remove it, as needed. 
      this.userTableAdapter.Fill(this.usersDataSet.User); 
     } 

     private void userDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 

     } 

     private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e) 
     { 
      //Code to delete an item 
     } 

     private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) 
     { 
      //Code to add an item 
     } 
    } 
} 

所以,我现在需要进行更新和删除,任何帮助将不胜感激。

+2

哪个UI框架? WinForms,WPF,Silverlight,ASP.NET WebForms,LightSwitch? – 2011-12-28 15:30:31

+0

桌面,一个窗体。 – 2011-12-28 15:36:18

+1

为您添加了“winforms”标签。 – 2011-12-28 15:37:59

回答

0

若要从表中删除仅仅是一个事情,

connStr = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\Set.mdb;Persist Security Info=False"); 
     try 
     { 
      //Empty the table 
      sql = "Delete from " + table; 
      using (OleDbConnection conn = new OleDbConnection(connStr)) 
      { 
       conn.Open(); 
       using (OleDbCommand cmd1 = new OleDbCommand(sql, conn)) 
       { 
        cmd1.ExecuteNonQuery(); 
       } 
      } 
     } 

我不知道剩下的作为验证数据我有一个类似的相关问题打开。