標籤:orm image selected begin cts 變數 hand text ide
以這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
編輯datagridview儲存格