总共有三个事件
DgvFeeInspection_CellClick事件
DgvFeeInspection_EditingControlShowing事件
editingControl_TextChanged自定义事件
static bool bo = false; private void DgvFeeInspection_CellClick(object sender, DataGridViewCellEventArg s e) { if (DgvFeeInspection.Rows.Count >= 1) { try { if (this.DgvFeeInspection.CurrentCell.ColumnIndex != 4) bo = false; else bo = true; } catch { } } }
private void DgvFeeInspection_EditingControlShowing(object sender, DataGridViewEditingContr olShowingEventArgs e) { if (DgvFeeInspection.Rows.Count >= 1) { if (e.Control.GetType().Equals(typeof(DataGridViewTextBoxEditi ngControl)))//cell为类TextBox时 { e.CellStyle.BackColor = Color.FromName("window"); DataGridViewTextBoxEditi ngControl editingControl = e.Control as DataGridViewTextBoxEditi ngControl; editingControl.TextChanged += new EventHandler(editingControl_TextChanged); } } } private void editingControl_TextChanged(object sender, EventArgs e) { if (DgvFeeInspection.Rows.Count >= 1) { if (bo == true) { string str = DgvFeeInspection.CurrentCell.EditedFormattedValue.ToString(); SpellAndWbCode spell = new SpellAndWbCode(); this.DgvFeeInspection.Rows[DgvFeeInspection.CurrentCell.RowIndex].Cells["Column1"].Value = spell.GetSpellCode(str); this.DgvFeeInspection.Rows[DgvFeeInspection.CurrentCell.RowIndex].Cells["Column1"].ReadOnly = true; } } }