WinForm中DataGridView显示更新数据--人性版

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


选择DataGridView中的某一行,离开后,若数据变化则更新到数据库,若没变则不做操作.精髓部分:当点击一个单元格,如果已经换了行并且数据改变,会更新数据到数据库,并从数据库加载数据,再把之前点击的那个单元格设为选定状态,使得操作体验良好.代码如下:



  1 namespace UI
  2 {
  3     public partial class FormStockList : Form
  4     {
  5         public FormStockList()
  6         {
  7             InitializeComponent();
  8             this.dgvStockList.AutoGenerateColumns = false;//自动产生列设置为false;
  9         }
 10
 11         //加载商品ID的下拉列表的数据源和DGV的数据源.
 12         private void FormStockList_Load(object sender, EventArgs e)
 13         {
 14             //把DataGridView中名为goodsId的下拉列表列取出来,设置重要属性.
 15             DataGridViewComboBoxColumn col = this.dgvStockList.Columns["goodsId"] as DataGridViewComboBoxColumn;
 16             col.DataSource = new BLL.GoodsBLL().GetModelList("");
 17             col.DisplayMember = "Name";
 18             col.ValueMember = "GoodsId";
 19             col.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
 20
 21             LoadDataByPageIndex();//加载DataGridView的数据源.
 22         }
 23
 24         int cellRowIndex;//全局变量,存储单元格行号
 25         int cellColumnIndex;//全局变量,存储单元格列号
 26
 27         int PreSelectRowIndex;//存储行号,以后用来判断是否换行.
 28         bool isFirstSelectRow = true;//标识是否给变量PreSelectRowIndex赋过值.没为PreSelectRowIndex赋过值,则为true.
 29         private void dgvStockList_CellClick(object sender, DataGridViewCellEventArgs e)
 30         {
 31             //DataGridView dgv = (DataGridView)sender;
 32             cellRowIndex = this.dgvStockList.CurrentCell.RowIndex;//为cellRowIndex赋值
 33             cellColumnIndex = this.dgvStockList.CurrentCell.ColumnIndex;//为cellColumnIndex赋值
 34             if (isFirstSelectRow)//若isFirstSelectRow为true,说明PreSelectRowIndex是未赋过值
 35             {
 36                 PreSelectRowIndex = e.RowIndex;//把当前行号赋值给PreSelectRowIndex
 37                 isFirstSelectRow = false;//说明已经给PreSelectRowIndex赋值
 38             }
 39             else//如果已经给PreSelectRowIndex赋值
 40             {
 41                 if (e.RowIndex != PreSelectRowIndex)//当前选的单元格的行号和存储的行号不一样,说明换行了.
 42                 {
 43                     PreSelectRowIndex = e.RowIndex;//存储新行的行号.
 44                     SelectionRowChanged();//换行时要执行的方法.可能或重新绑定dgvStockList的数据源.
 45                     this.dgvStockList.Rows[cellRowIndex].Cells[cellColumnIndex].Selected = true;//针对可能重新绑定dgvStockList的数据源的情况,重新设置当前选中的单元格为重新绑定数据源前用户已选的单元格.
 46                 }
 47             }
 48             this.dgvStockList.BeginEdit(true);//dgvStockList_CellClick点击事件会选择一个单元格,BeginEdit让当前单元格可以编辑,能触发dgvStockList_CellBeginEdit事件.
 49         }
 50
 51         ////换行时判断是否值有变化,如果有,就把行数据更新到数据库.重新加载数据源.
 52         private void SelectionRowChanged()
 53         {
 54             if (isEdit)//判断该行是否编辑过
 55             {
 56                 if (temp.GoodsId != stock.GoodsId || temp.SCount != stock.SCount || temp.StockPrice != stock.StockPrice)//判断编辑过后该行的数据是否改变,如果改变了,就更新数据到数据库.
 57                 {
 58                     new BLL.StockBLL().Update(stock);//把变动后的行数据更新到数据库.
 59                     LoadDataByPageIndex();//重新加载dgvStockList的数据源.
 60                 }
 61             }
 62             isEdit = false;//重置isEdit的状态.
 63             isFirst = true;//重置isFirst的状态.
 64         }
 65
 66         Model.Stock stock = null;//存储变动后的行数据的实体对象.
 67         Model.Stock temp = null;//存储变动前的行数据的实体对象
 68         bool isFirst = true;//标识是否是第一次编辑该行.
 69         bool isEdit = false;//标识一行是否被编辑过.如果没有,以后就不用更新行数据到数据库.
 70
 71         private void dgvStockList_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
 72         {
 73             isEdit = true;//标识该行被编辑.
 74             stock = this.dgvStockList.SelectedRows[0].DataBoundItem as Model.Stock;//把该行的对象用存入变量stock
 75             if (isFirst)//如果是第一次要开始编辑该行,即该行还没有被编辑过.
 76             {
 77                 temp = new Model.Stock();//存储该行最初的数据.
 78                 temp.GoodsId = stock.GoodsId;
 79                 temp.SCount = stock.SCount;
 80                 temp.StockPrice = stock.StockPrice;
 81                 isFirst = false;//标识该行不再是第一次被编辑了.
 82             }
 83         }
 84         int pageIndex = 1;//页索引
 85         int pageCount = 4;//每页显示几条数据
 86         private void LoadDataByPageIndex()
 87         {
 88             this.dgvStockList.DataSource = new BLL.StockBLL().GetStockListByPage(pageIndex, pageCount);
 89         }//加载dgvStockList的数据.
 90
 91         //点击上一页
 92         private void btnPre_Click(object sender, EventArgs e)
 93         {
 94             pageIndex--;
 95             if (pageIndex < 1)
 96             {
 97                 MessageBox.Show("已经第一页了"); pageIndex++; return;
 98             }
 99             LoadDataByPageIndex();
100         }
101
102         //点击下一页
103         private void btnNext_Click(object sender, EventArgs e)
104         {
105             pageIndex++;
106             if (pageIndex > Model.Save.TPageCount)//Model.Save.TPageCount存的是总的页数.
107             {
108                 MessageBox.Show("已经最后一页了"); pageIndex--; return;
109             }
110             LoadDataByPageIndex();
111         }
112     }
113 }
 
时间: 2024-08-27 20:08:54

WinForm中DataGridView显示更新数据--人性版的相关文章

C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法

下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumn CheckBox是否选中 在判断DataGridView中CheckBox选中列的时候,用DataGridViewRow.Cells[0].FormattedValue.ToString()=="True"语句时存在问题,当我们直接点击CheckBox时,结果显示未选中,但是如果我们在点击其他单元格时,结果显示选中.而用

解决cefsharp在winform中不显示tooltipText问题(网页元素的title提示)

1.监听网页属性改变事件 webView.PropertyChanged += webView_PropertyChanged; 2.拖一个ToolTip控件到窗体 3.获取TooltipText并显示出来 //隐藏toolTip if (this.IsHandleCreated) { this.BeginInvoke(new MethodInvoker(() => { if (this.IsHandleCreated && !this.IsDisposed) { if (this.

SQL中使用UPDATE更新数据时一定要记得WHERE子句

我们在使用 SQL 中的 UPDATE 更新数据时,一般都不会更新表中的左右数据,所以我们更新的数据的 SQL 语句中会带有 WHERE 子句,如果没有WHERE子句,就回更新表中所有的数据,在 mysql 中,我们可以设置sql_safe_updates 这个自带的参数来解决,,当该参数开启的情况下,我们必须在 UPDATE 语句后携带 WHERE 条件,否则就会报错.set sql_safe_updates=1; 表示开启该参数.下面是开启sql_safe_updates参数后不带  WHE

winform中DataGridView实现分页功能

http://liyaguang20111105.blog.163.com/blog/static/19929420220146283255809/ 在winform的设计中,要实现对DataGridView控件的分页功能,需要两个控件:BindingSource.BindingNavigator,根据需求可对BindingNavigator进行自由的扩展,下图的示例则是根据一般需求对分页功能的实现.红色区域是对BindingNavigator控件扩展后的效果. 具体实现过程 : //窗体构造方

C#winform中datagridview导出Excel

只需要传入datagridview的name即可. 1 //导出Excel()方法 2 public void ToExcel(DataGridView dataGridView) 3 { 4 //实例化一个Excel.Application对象 5 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); 6 7 if (excel == null)

winform中DataGridView添加ComboBox的最终解决方案(点击ComboBox默认显示当前行的内容)

第一: 数据绑定ComBoBox控件 先在窗体设计时拖一个ComBoBox控件,然后在里面的ITEMS设好你要下拉项,或者从数据库中的表绑定,这个估计都会. 第二: // 将下拉列表框加入到DataGridView控件中,这句放在绑定DataGridView之后写. 在窗体的Load方法中加入:g_DataGridView.Controls.Add(g_ComBoBox);也就是把ComBoBox控件添加到DataGridView控件中第三: 在DataGridView控件的CurrentCel

winform中DataGridView的数据实现导出excel

1,窗体设计 首先需要引入程序集:Microsoft.Office.Interop.Excel  (如果没有引用过的需要右键添加引用再搜索就行了) 实现的方法: /// <summary> /// /// </summary> /// <param name="fileName">文件路径</param> /// <param name="myDGV">控件DataGridView</param>

C#winform中DataGridView常用的属性

1.AllowUserToAddRows属性:指示是否向用户显示添加行的选项 AllowUserToOrderColumns属性:指示是否允许通过手动对列重新定位 AllowUserToResizeColumns属性:指示用户是否可以调整列的大小 AllowUserToResizeRows属性:指示用户是否可以调整行的大小 2.ColumnHeadersVisible属性:指示是否显示列标题行    RowHeadersVisible属性:指示是否显示包含行标题的列 3.ReadOnly属性:指

WinForm中DataGridView验证单元格输入的是数字

转载:http://www.cnblogs.com/ganqiyin/archive/2013/02/18/2915491.html 事件:DataGridView验证单元格输入的是数字,DataGridView源数据是从数据库读取的. 需求:当用户输入的不是数字的时候需要提示信息(数据是直接绑定数据库的,因此dataGridView有自己的报错功能,我们需要屏蔽掉它,显示自己的错误提示!) 实现: 选择DataGridView的CellValidating事件 (1)  验证整数: 1 pri