如何允许C#中的DataGridView中的CRUD操作
本文关键字:中的 CRUD 操作 DataGridView 何允许 | 更新日期: 2023-09-27 17:57:50
我在将数据库(我使用的是Northwind)绑定到DataGridView时遇到了很多麻烦。我尝试过各种方法,但没有一种方法适用于所有操作,只有一些。我也在其他网站上询问过,但到目前为止,我还没有得到任何有用的建议。
有没有一个教程真正涵盖了所有CRUD操作(或者几个教程的组合涵盖了所有操作)?
尤其是删除操作让我头疼,因为我得到的唯一提示是将我的删除代码放入某个DataGridView事件中,但问题是我找不到确定用户到底想删除什么的方法,并且不会为删除键触发KeyDown事件。
谢谢!
编辑:非常感谢。这份文件很有帮助。不过,我还有一个问题,我有一个DataTable作为DataGridView的DataSource。为了更新它以执行用户输入的CRUD操作,我需要手动将数据插入到DataTable中吗?还是只需使用适配器的DeleteCommand/InsertCommand/etc属性构建一个常规SQL命令,然后在update方法中将尚未修改的DataTable作为参数传递就足够了?
也就是说,这会给我带来想要的结果吗?将用户刚刚输入到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#库&应用程序,让你创建你的对象,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("'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();
}
}
}
}
}