2011-05-28 27 views
2

我一直在將一個數據庫(我使用Northwind)綁定到DataGridView時遇到了很多麻煩。 我試過各種方法,但都沒有爲所有操作工作,只有一些。 我也問過其他網站,但到目前爲止我還沒有得到任何有用的建議。如何在C#.NET中的DataGridView中允許CRUD操作?

是否有一個涵蓋真正所有CRUD操作的教程(或幾個涵蓋所有教程的教程的組合)?

特別是刪除操作令我頭痛,因爲我得到的唯一提示是將我的刪除代碼放入一些DataGridView事件中,但問題是我找不到一種方法來確定用戶想要刪除的內容並且KeyDown事件不會觸發刪除鍵。

謝謝!編輯: 非常感謝。該文件非常有幫助。 但我有另一個問題,我有一個DataTable作爲DataSource的DataGridView。 要更新它以執行用戶輸入CRUD操作,是否需要手動將數據插入到DataTable中,還是僅使用適配器的DeleteCommand/InsertCommand/etc屬性構建常規SQL命令,然後只傳遞尚未修改的DataTable作爲更新方法中的參數?

I.e.這會得到我想要的結果,插入一個新的行到用戶剛剛輸入到DataGridView中的數據庫表的新表?

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); 
    } 
+0

這是Windows Forms,Web應用程序嗎?什麼編程語言? – 2011-05-28 21:52:33

回答

1

我創建了一個C#庫&應用程序,它可以讓你創建你的對象,CRUD操作(使用BLL & DAL)和Web UI執行該CRUD操作。

http://manacodegenerator.codeplex.com

它的重量輕,非常簡單易用,它幫助我擺脫那些枯燥小時重複的代碼創建的。

我不斷地發展它,因爲它是我每天使用的工具。

希望它有幫助!

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Projecttest 
{ 
    class Program 
    { 
     struct student 
     { 
      public string stid; 
      public string stname; 
      public string stage; 

     }; 
     static void Main(string[] args) 
     { 
      student[] st = new student[4]; 
      int choice; 
      string confirm; 

      int count = 0; 
      Console.WriteLine("Select operation to Perform"); 
      Console.WriteLine("1. ADD"); 
      Console.WriteLine("2. UPDATE"); 
      Console.WriteLine("3. DELETE"); 
      Console.WriteLine("4. SHOW"); 
      do 
      { 
       Console.Write("enter your choice(1-4):"); 
       choice = Convert.ToInt32(Console.ReadLine()); 
       switch (choice) 
       { 
        case 1: 
         Add(st, count); 
         count++; 
         break; 
        case 2: 
         Update(st); 
         break; 
        case 3: 
         Delete(st); 
         break; 
        case 4: 
         Show(st); 
         break; 
        default: 
         Console.WriteLine("\nInvalid Selection\n"); 
         break; 
       } 

       Console.Write("Press Y or y to continue:"); 

       confirm = Console.ReadLine().ToString(); 
      } while (confirm == "Y" || confirm == "y"); 
     } 

     static void Add(student[] st, int count) 
     { 
      Console.Write("\nEnter student ID: "); 
      st[count].stid = Console.ReadLine(); 
      Console.Write("Enter student name: "); 
      st[count].stname = Console.ReadLine(); 
      Console.Write("Enter student age: "); 
      st[count].stage = Console.ReadLine(); 
     } 
     static void Show(student[] st) 
     { 
      for (int count = 0; count < st.Length; count++) 
      { 
       if (st[count].stid != null) 
       { 
        Console.WriteLine("\nStudent ID : " + st[count].stid); 
        Console.WriteLine("Student Name : " + st[count].stname); 
        Console.WriteLine("Student Age : " + st[count].stage); 
       } 
      } 
     } 
     static void Delete(student[] st) 
     { 
      Console.Write("\nEnter student ID: "); 
      string studid = Console.ReadLine(); 
      for (int count = 0; count < st.Length; count++) 
      { 
       if (studid == st[count].stid) 
       { 
        st[count].stid = null; 
        st[count].stname = null; 
        st[count].stage = null; 
       } 
      } 
     } 
     static void Update(student[] st) 
     { 
      Console.Write("\nEnter student ID: "); 
      string studid = Console.ReadLine(); 
      for (int count = 0; count < st.Length; count++) 
      { 
       if (studid == st[count].stid) 
       { 
        Console.Write("Enter student name: "); 
        st[count].stname = Console.ReadLine(); 
        Console.Write("Enter student age: "); 
        st[count].stage = Console.ReadLine(); 
       } 
      } 
     } 
    } 
}