给DataGridVew添加右键菜单,并实现增删改等功能

本人以前用PB做过一些开发,挺喜欢DATAWINDOW的。在学习C#时发现给DataGirdView加上右键菜单的效果和PB中的DATAWINDOW有相同的效果。其中数据库的操作应用的SQLDataAdatper,下面源码奉上,欢迎朋友们提出指正和意见。
首先,先建立一个Form窗口取名为DataGridViewDemo 从工具箱中选择控件,并设置属性如下
DataGridView  name属性 dataGridView
TextBox            name属性 txtContent
Button              name属性  btSave  Text 属性  保存
Button              name 属性 btExit    Text属性  退出

DataGridViewDemo 类代码如下
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10. namespace C15Demo
  11. {
  12.     public partial class DataGridViewDemo : Form
  13.     {
  14.         //全局变量
  15.         private SqlDataAdapter adapter = null;
  16.         private DataTable dt = null;
  17.         public DataGridViewDemo()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.         ///
  22.         /// 初始化SQLDataAdapter类添加对数据表的增删改操作
  23.         ///
  24.         private void InitAdapter()
  25.         {
  26.             SqlConnection connection = new SqlConnection();
  27.             connection.ConnectionString = "Persist Security Info=false;Integrated Security=SSPI;Initial Catalog=Northwind;server=(local)";
  28.             adapter = new SqlDataAdapter("select ProductID,ProductName,UnitPrice,Discontinued from Products", connection);
  29.             adapter.FillLoadOption = LoadOption.OverwriteChanges;
  30.             //添加修改命令
  31.             SqlCommand UpdataCommand = new SqlCommand();
  32.             UpdataCommand.Connection = connection;
  33.             UpdataCommand.CommandText = "Update Products set ProductName = @ProductName,UnitPrice=@UnitPrice,Discontinued = @Discontinued where ProductID=@Productid ";
  34.             UpdataCommand.Parameters.Add("@ProductID", SqlDbType.Int, 4, "ProductID");
  35.             UpdataCommand.Parameters.Add("@ProductName", SqlDbType.VarChar, 50, "ProductName");
  36.             UpdataCommand.Parameters.Add("@UnitPrice", SqlDbType.Money, 20, "UnitPrice");
  37.             UpdataCommand.Parameters.Add("@Discontinued", SqlDbType.Bit, 1, "Discontinued");
  38.             adapter.UpdateCommand = UpdataCommand;
  39.             //添加新增
  40.             SqlCommand InsertCommand = new SqlCommand();
  41.             InsertCommand.Connection = connection;
  42.             InsertCommand.CommandText = "Insert into Products (ProductName,UnitPrice,Discontinued)values(@ProductName,@UnitPrice,@Discontinued) ";
  43.             InsertCommand.Parameters.Add("@ProductName", SqlDbType.VarChar, 50, "ProductName");
  44.             InsertCommand.Parameters.Add("@UnitPrice", SqlDbType.Money, 20, "UnitPrice");
  45.             InsertCommand.Parameters.Add("@Discontinued", SqlDbType.Bit, 1, "Discontinued");
  46.             adapter.InsertCommand = InsertCommand;
  47.             //添加删除
  48.             SqlCommand DeleteCommand = new SqlCommand();
  49.             DeleteCommand.Connection = connection;
  50.             DeleteCommand.CommandText = "Delete Products where ProductID=@ProductID ";
  51.             DeleteCommand.Parameters.Add("@ProductID", SqlDbType.Int, 4, "ProductID");
  52.             adapter.DeleteCommand = DeleteCommand;
  53.         }
  54.         ///
  55.         /// 初始化DataGridView并绑定列,给DataGridView添加右键菜单
  56.         ///
  57.         /// DataGrivView控件
  58.         private void InitDataGridView(DataGridView dataGridView)
  59.         {
  60.             dataGridView.AutoGenerateColumns = false;
  61.             dataGridView.AllowUserToAddRows = false  ;
  62.             dataGridView.AllowUserToDeleteRows = false  ;
  63.             dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  64.             dataGridView.EditMode = DataGridViewEditMode.EditOnEnter;
  65.             dataGridView.MultiSelect = false;
  66.             dataGridView.ScrollBars = ScrollBars.Vertical;
  67.             dataGridView.VirtualMode = true;
  68.             //添加列
  69.             DataGridViewTextBoxColumn Column1 = new DataGridViewTextBoxColumn();
  70.             Column1.DataPropertyName = "ProductID";
  71.             Column1.HeaderText = "产品 ID";
  72.             Column1.Name = "Column1";
  73.             //Column1.ReadOnly = true;
  74.             DataGridViewTextBoxColumn Column2 = new DataGridViewTextBoxColumn();
  75.             Column2.DataPropertyName = "ProductName";
  76.             Column2.HeaderText = "产品名称";
  77.             Column2.Name = "Column2";
  78.             Column2.Width = 165;
  79.             DataGridViewTextBoxColumn Column3 = new DataGridViewTextBoxColumn();
  80.             Column3.DataPropertyName = "UnitPrice";
  81.             Column3.HeaderText = "价格";
  82.             Column3.Name = "Column3";
  83.             DataGridViewCheckBoxColumn Column4 = new DataGridViewCheckBoxColumn();
  84.             Column4.DataPropertyName = "Discontinued";
  85.             Column4.HeaderText = "是否使用";
  86.             Column4.Name = "Column4";
  87.             dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { Column1, Column2, Column3 ,Column4 });
  88.             //添加右键菜单
  89.             ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
  90.             contextMenuStrip.ShowCheckMargin = false;
  91.             contextMenuStrip.ShowImageMargin = false;
  92.             contextMenuStrip.ShowItemToolTips = true;
  93.             contextMenuStrip.TextDirection = ToolStripTextDirection.Horizontal;
  94.             contextMenuStrip.Opening += new CancelEventHandler(contextMenuStrip_Opening);
  95.             dataGridView.ContextMenuStrip = contextMenuStrip;
  96.             dataGridView.CurrentCellChanged += new EventHandler(dataGridView_CurrentCellChanged);
  97.         }
复制代码

你可能感兴趣的:(SQL)