DataGridView基础操作
来源:互联网 发布:今天淘宝系统出问题了 编辑:程序博客网 时间:2024/06/17 14:02
--建表if object_id('gtest') is not null drop table gtestgocreate table gtest(id int identity(1,1) not null constraint [pk_gtest] primary key,name nvarchar(20) not null constraint [uk_gtest_name] unique,price float not null)goset nocount on;declare @count intselect @count=1while @count<50begininsert into gtest values('产品'+cast(@count as nvarchar(2)),10*@count);select @count=@count+1;endgo
--前台拖个DataGridView(datagridview1)进去,不做任何其他操作
--后台,数据库操作类此略
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;using System.Diagnostics;using System.Runtime.InteropServices;using System.Reflection;using System.Text.RegularExpressions;namespace cswinformtest{ public partial class Form1 : Form { bool firstLoad = true; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataBind(); }#region datagridview command /// <summary> /// bind datagridview /// </summary> private void DataBind() { this.dataGridView1.DataSource = SQLHelper.ExecuteDataTable(SQLHelper.ConStr, CommandType.Text, "select * from gtest"); this.dataGridView1.Columns[0].ReadOnly = true; } /// <summary> /// for insert,update /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e) { if (!firstLoad) { DataGridView dgv = sender as DataGridView; //update cell immediately if (dgv.EditingControl != null) { if (dgv.CurrentCell.ColumnIndex == 2) { dgv.CurrentCell.Value = dgv.EditingControl.Text.Replace(",", ""); } else { dgv.CurrentCell.Value = dgv.EditingControl.Text; } } string id = dgv.Rows[e.RowIndex].Cells[0].Value.ToString(); string name = Replace(dgv.Rows[e.RowIndex].Cells[1].Value.ToString()); string price = dgv.Rows[e.RowIndex].Cells[2].Value.ToString().Replace(",",""); //check empty if (!CheckEmpty(name, price)) { return; } //check unique if (!CheckUnique(id,name)) { MessageBox.Show(string.Format("Product:{0} exists already",name), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //insert if (id == "") { dgv.Rows[e.RowIndex].Cells[0].Value = SQLHelper.ExecuteScalar(SQLHelper.ConStr, CommandType.Text, string.Format("insert into gtest values('{0}',{1});select SCOPE_IDENTITY();", name, price)).ToString(); } //update else { SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, string.Format("update gtest set name='{0}',price='{1}' where id={2}", name, price, id)); } } else { firstLoad = false; } } /// <summary> /// for delete /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { if (MessageBox.Show("Are your sure to delete this record?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, string.Format("delete gtest where id={0}", e.Row.Cells[0].Value)); return; } e.Cancel = true; } #endregion
#region common method /// <summary> /// chek empty; /// </summary> /// <param name="name"></param> /// <param name="price"></param> /// <returns></returns> private bool CheckEmpty(string name,string price) { if (Regex.IsMatch(name, @"^\s*$")) { MessageBox.Show("Name not allowed null", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } if (Regex.IsMatch(price, @"^\s*$")) { MessageBox.Show("Price not allowed null","Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } return true; } /// <summary> /// should be dealt in database /// </summary> /// <returns></returns> private bool CheckUnique(string id,string name) { if (Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.ConStr, CommandType.Text, string.Format("select count(1) from gtest where id<>{0} and name='{1}'", id == "" ? "0" : id, name) )) > 0) { return false; } return true; } /// <summary> /// replace ' /// </summary> /// <param name="str"></param> /// <returns></returns> private string Replace(string str) { return str.Replace("'", "''"); } #endregion }}
- DataGridView基础操作
- DataGridView操作
- Datagridview操作
- DataGridView操作
- DatagridView 操作
- DataGridView操作
- .Net基础:C#中对DatagridView部分常用操作
- DataGridView操作细则
- 操作DataGridView中的DataTable
- C# DataGridView操作
- C# 对dataGridView操作
- DataGridView相关操作
- DataGridView操作指南
- DataGridView常用操作
- 操作DataGridView控件详解
- c#datagridview数据库操作
- datagridview的一些操作
- DataGridView控件操作数据库
- Linux Shell系列(3) 正则表达式
- c语言中的强制转换 a++ ++a
- 内嵌PDF网页无法打开的解决办法
- 排序算法整理之冒泡排序
- Android中View绘制流程以及invalidate()等相关方法分析
- DataGridView基础操作
- html 超出部分用省略符号显示(兼容浏览器)
- Dojo与jQuery简单比较分析
- 《足球与互联网产品》3:从西班牙“短传渗透”理解互联网产品的流程设计
- ARC0 Encountered disk IO error 19502
- 加载png图片并转换为DIB HBITMAP
- Java基础之多线程(一)--概述、同步、死锁、单例模式
- 在suse11上安装配置vsftpd,允许local的root和oracle用户ftp
- 哪些企业为提升员工幸福感最尽心尽力?