编辑datagridview单元格

时间:2023-03-09 15:31:14
编辑datagridview单元格

编辑datagridview单元格编辑datagridview单元格编辑datagridview单元格

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

 private DateTimePicker dtp = new DateTimePicker();
private ComBox sellstyle = new ComBox ();//设置全局变量
 public PlanVindicateForm()
{
InitializeComponent();
dgvReaschResult.Controls.Add(dtp);//在窗体的构造函数里将datetimepicker控件加入
this.dgvReaschResult.Controls.Add(this.sellstyle);//combox控件
this.sellstyle.Visible = false;//设置是否显示
this.dtp.Visible = false;
this.dtp.Format = DateTimePickerFormat.Custom;//设置显示格式
this.dtp.CustomFormat = "HH:mm";
this.dtp.KeyDown += new KeyEventHandler(dtp_KeyDown);//注册控件用到的事件
this.sellstyle.cbTypeBox.SelectedIndexChanged += new EventHandler(cbTypeBox_SelectedIndexChanged);
}
 private void dgvReaschResult_CellClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.RowIndex > -) && (e.ColumnIndex > -))
{
string dataPropertyName = this.dgvReaschResult.Columns[e.ColumnIndex].DataPropertyName;
switch (dataPropertyName)
{
case "CanSellSeatCount": this.dgvReaschResult.BeginEdit(true);
this.Original = int.Parse(this.dgvReaschResult.Rows[this.dgvReaschResult.CurrentCell.RowIndex].Cells[e.ColumnIndex].Value.ToString());
return; case "DrvTime":
this.dgvReaschResult.BeginEdit(true);
this._Rectangle = this.dgvReaschResult.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
this.dtp.Size = new Size(this._Rectangle.Width, this._Rectangle.Height);
this.dtp.Location = new Point(this._Rectangle.X, this._Rectangle.Y);
this.dtp.Visible = true;
this.dtp.ShowUpDown = true;
this.dtp.Value = DateTime.Parse(this.SelectSchedule.DrvTime);
this.dtp.Focus();
this.originaTime = this.dtp.Value;
return; case "SellStyleName":
this.dgvReaschResult.BeginEdit(true);
this._Rectangle = this.dgvReaschResult.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
this.sellstyle.Size = new Size(this._Rectangle.Width, this._Rectangle.Height);
this.sellstyle.Location = new Point(this._Rectangle.X, this._Rectangle.Y);
this.sellstyle.Visible = true;
this.sellstyle.SelectedText = this.SelectSchedule.SellStyleName;
this.sellstyle.Focus();
this.sellStyleName = this.SelectSchedule.SellStyleName;
return;
}
this.dtp.Visible = false;
this.sellstyle.Visible = false;
} }
  private void dgvReaschResult_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if ((e.RowIndex > -) && (e.ColumnIndex > -))
{
try
{
string dataPropertyName = this.dgvReaschResult.Columns[e.ColumnIndex].DataPropertyName;//获取选中单元格的数据源的名称,即列名
int seats = ;
int num2 = ;
switch (dataPropertyName)
{
case "CanSellSeatCount";//第3种
{
//对获取到的数据进行处理
break;
}
case "DrvTime"://第1种
//对获取到的数据进行处理
break; case "SellStyleName"://第2种
//对获取到的数据进行处理
break; }
}
catch (Exception exception)
{
this.ShowError("执行失败!");
}
} }