編輯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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM