编辑datagridview单元格

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

1 private DateTimePicker dtp = new DateTimePicker();
2 private ComBox sellstyle = new ComBox ();//设置全局变量
View Code
 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
 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
 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

 

posted @ 2016-11-01 18:00  闲得无聊敲代码  阅读(3000)  评论(0编辑  收藏  举报