English 中文(简体)
如何允许C#中的DataGridView中的CRUD操作?
原标题:How to allow CRUD operations in a DataGridView in C#.NET?

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);
    }
问题回答

我创建了一个C#Lib&;应用程序,它允许您创建对象、CRUD操作(使用BLL&;DAL)和Web UI来执行CRUD操作。

http://manacodegenerator.codeplex.com

它重量轻,使用起来非常简单,它帮助我摆脱了那些无聊的重复代码创建时间。

我不断地发展它,因为它是我每天使用的工具。

希望它能有所帮助!

如果您使用的是WPF应用程序,下面是关于如何执行CRUD操作的教程:

http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx#updates

基本上,您将绑定到行删除事件中,并在您的方法中处理删除的行。

下面是一个关于使用DataGridView的文档。中是有关绑定到Delete事件和其他CRUD操作的信息:

http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc

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("
Invalid Selection
");
                        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("
Enter 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("
Student 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("
Enter 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("
Enter 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();
                }
            }
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签