编辑datagridview单元格

摘要:
以这三种类型为例,最简单的是第三种,它直接使单元格可编辑。编辑完成后,将触发CellEndEdit事件,最后处理输入数据。

编辑datagridview单元格第1张编辑datagridview单元格第2张编辑datagridview单元格第3张

以这3种为例,最简单的是第三种,直接让单元格处于可编辑状态,当完成编辑后触发CellEndEdit事件,最后对输入的数据进行处理。

编辑datagridview单元格第4张编辑datagridview单元格第5张
1 private DateTimePicker dtp = new DateTimePicker();
2 private ComBox sellstyle = new ComBox ();//设置全局变量
View Code
编辑datagridview单元格第6张编辑datagridview单元格第7张
 1 public PlanVindicateForm()
 2         {
 3             InitializeComponent();
 4             dgvReaschResult.Controls.Add(dtp);//在窗体的构造函数里将datetimepicker控件加入
 5             this.dgvReaschResult.Controls.Add(this.sellstyle);//combox控件
 6             this.sellstyle.Visible = false;//设置是否显示
 7             this.dtp.Visible = false;
 8             this.dtp.Format = DateTimePickerFormat.Custom;//设置显示格式
 9             this.dtp.CustomFormat = "HH:mm";
10             this.dtp.KeyDown += new KeyEventHandler(dtp_KeyDown);//注册控件用到的事件
11             this.sellstyle.cbTypeBox.SelectedIndexChanged += new EventHandler(cbTypeBox_SelectedIndexChanged);
12         }
View Code
编辑datagridview单元格第8张编辑datagridview单元格第9张
 1 private void dgvReaschResult_CellClick(object sender, DataGridViewCellEventArgs e)
 2         {
 3             if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
 4             {
 5                 string dataPropertyName = this.dgvReaschResult.Columns[e.ColumnIndex].DataPropertyName;
 6                 switch (dataPropertyName)
 7                 {
 8                     case "CanSellSeatCount":
 9 
10                         this.dgvReaschResult.BeginEdit(true);
11                         this.Original = int.Parse(this.dgvReaschResult.Rows[this.dgvReaschResult.CurrentCell.RowIndex].Cells[e.ColumnIndex].Value.ToString());
12                         return;
13 
14                     case "DrvTime":
15                         this.dgvReaschResult.BeginEdit(true);
16                         this._Rectangle = this.dgvReaschResult.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
17                         this.dtp.Size = new Size(this._Rectangle.Width, this._Rectangle.Height);
18                         this.dtp.Location = new Point(this._Rectangle.X, this._Rectangle.Y);
19                         this.dtp.Visible = true;
20                         this.dtp.ShowUpDown = true;
21                         this.dtp.Value = DateTime.Parse(this.SelectSchedule.DrvTime);
22                         this.dtp.Focus();
23                         this.originaTime = this.dtp.Value;
24                         return;
25 
26                     case "SellStyleName":
27                     this.dgvReaschResult.BeginEdit(true);
28                     this._Rectangle = this.dgvReaschResult.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
29                     this.sellstyle.Size = new Size(this._Rectangle.Width, this._Rectangle.Height);
30                     this.sellstyle.Location = new Point(this._Rectangle.X, this._Rectangle.Y);
31                     this.sellstyle.Visible = true;
32                     this.sellstyle.SelectedText = this.SelectSchedule.SellStyleName;
33                     this.sellstyle.Focus();
34                     this.sellStyleName = this.SelectSchedule.SellStyleName;
35                     return;
36                 }
37                 this.dtp.Visible = false;
38                 this.sellstyle.Visible = false;
39             }
40 
41         }
View Code
编辑datagridview单元格第10张编辑datagridview单元格第11张
 1  private void dgvReaschResult_CellEndEdit(object sender, DataGridViewCellEventArgs e)
 2         {
 3             if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
 4             {
 5                 try
 6                 {
 7                     string dataPropertyName = this.dgvReaschResult.Columns[e.ColumnIndex].DataPropertyName;//获取选中单元格的数据源的名称,即列名
 8                     int seats = 0;
 9                     int num2 = 0;
10                     switch (dataPropertyName)
11                     {
12                         case "CanSellSeatCount";//第3种
13                             {
14                                 //对获取到的数据进行处理
15                                 break;
16                             }
17                         case "DrvTime"://第1种
18                              //对获取到的数据进行处理
19                             break;
20 
21                         case "SellStyleName"://第2种
22                             //对获取到的数据进行处理
23                             break;
24 
25                     }
26                 }
27                 catch (Exception exception)
28                 {
29                     this.ShowError("执行失败!");
30                 }
31             }
32 
33         }
View Code

免责声明:文章转载自《编辑datagridview单元格》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C++ 系列:虚函数RedisTemplate访问Redis数据结构(二)——List下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

DataGridView合并单元格

昨天一个同事问我DataGridView单元格合并的问题,一开始按照我的设想是算出两个单元格的Rectangle,然后直接使用e.Graphics.FillRectangle(backColorBrush, rectangle)从新填充下背景色,然后在绘制显示的字符,当然这种想法是行不通的。下面是我写的一个单元格合并的方法,其实这种方法并不好,只是看上去是...

NPOI 导出Excel插入图片、设置边框样式、合并单元格、字体样式等小知识

1.设置单元格上边框为黑色实线 ICellStyle cellStyleTail = sheet.Workbook.CreateCellStyle(); cellStyleTail.BorderTop = BorderStyle.Thin; https://blog.csdn.net/ezreal_pan/article/details/81134701...

Delphi下EasyGrid使用体会

最近在编写软件的时候,非常需要一款支持多表头的StringGrid控件,朋友介绍使用EasyGrid控件,这款控件大概从04年开始就没有再更新,网上有关与它的资料也较少。但是通过其demo,此软件还是能满足需要,由于资料少,所以将自己在使用过程中的一些体会整理出来。 一、多表头合并 在EasyGrid中提供了SetMerges方法用于多表头合并,该方法原型...

SAP OLE中常用的一些方法和属性

1.ole中如何保存和退出。 1 call method of sheetname 'saveas' exporting #1 = filepath #2 =1. 2 call method of applicationname 'quit'.   2.给sheet重命名。  1 call method of sheetname 'name' = 'she...

常用控件2

组合选择框 QComboBox 是组合选择框,如下图所示 官网介绍 信号:选项改变 如果用户操作修改了QComboBox中的选项就会发出 currentIndexChanged 信号,可以这样指定处理该信号的函数 cbox.currentIndexChanged.connect(handleSelectionChange) 方法:添加一个选项 代码中可以...

导出Excel之Epplus使用教程2(样式设置)

 导出Excel之Epplus使用教程1(基本介绍)  导出Excel之Epplus使用教程2(样式设置)   导出Excel之Epplus使用教程3(图表设置)    导出Excel之Epplus使用教程4(其他设置) 1、公式计算      excel中离不开各种各样的公式计算,在Epplus中运用公式有两种方式,你都可以尝试一下: 1 2...