其中DataGridViewTextBoxEditingControl表示可以承载于 DataGridViewTextBoxCell 中的文本框控件。
DataGridView.EditingControlShowing 事件在显示用于编辑单元格的控件时发生,可以处理此事件,以便在单元格进入编辑模式时对编辑控件执行自定义初始化。若要自定义控件的显示特征,请设置 DataGridViewEditingControlShowingEventArgs.CellStyle 属性返回的对象的属性,而不要设置 DataGridViewEditingControlShowingEventArgs.Control 属性返回的控件的属性。下面的代码示例阐释如何处理此事件以更改当前单元格的 BackColor 属性。要运行此示例,请将代码粘贴到一个包含名为
dataGridView1
的 DataGridView 的窗体中,并确保 EditingControlShowing 事件与事件处理程序相关联。1private void dataGridView1_EditingControlShowing(object sender,
2 DataGridViewEditingControlShowingEventArgs e)
3{
4 e.CellStyle.BackColor = Color.Aquamarine;
5}
6
7
2 DataGridViewEditingControlShowingEventArgs e)
3{
4 e.CellStyle.BackColor = Color.Aquamarine;
5}
6
7
1 /// <summary>
2 /// 对DataGridView中的某一列大写设置
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
7 {
8 DataGridView dgv = (DataGridView)sender;
9 if (e.Control is DataGridViewTextBoxEditingControl)
10 {
11 DataGridViewTextBoxEditingControl editingControl = (DataGridViewTextBoxEditingControl)e.Control;
12 if (dgv.CurrentCell.OwningColumn.Name == "Column1")
13 {
14 editingControl.CharacterCasing = CharacterCasing.Upper;
15 }
16 else
17 {
18 editingControl.CharacterCasing = CharacterCasing.Normal;
19 }
20 }
21 }
2 /// 对DataGridView中的某一列大写设置
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
7 {
8 DataGridView dgv = (DataGridView)sender;
9 if (e.Control is DataGridViewTextBoxEditingControl)
10 {
11 DataGridViewTextBoxEditingControl editingControl = (DataGridViewTextBoxEditingControl)e.Control;
12 if (dgv.CurrentCell.OwningColumn.Name == "Column1")
13 {
14 editingControl.CharacterCasing = CharacterCasing.Upper;
15 }
16 else
17 {
18 editingControl.CharacterCasing = CharacterCasing.Normal;
19 }
20 }
21 }