C # How to provide uppercase letters in a specified DataGridView column?

I would like to be able to set CharacterCasing of the specified column to uppercase.

I cannot find a solution anywhere that will convert characters to uppercase as they are entered.

Thanks so much for any help

+3
source share
3 answers

To edit the contents of any cell in a column, you must use the EditingControlShowing event in the Datagridview. Using this event, you can trigger a keypress event in a specific cell. In the keypress event, you can apply a rule that automatically converts lowercase letters to uppercase.

Here are the steps for doing this:

EditingControlShowing , , . , - 2- .

    private void TestDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {
         if(TestDataGridView.CurrentCell.ColumnIndex.Equals(1))
        {
          e.Control.KeyPress += Control_KeyPress; // Depending on your requirement you can register any key event for this.
        }
     }

private static void Control_KeyPress(object sender, KeyPressEventArgs e)
    {
      // Write your logic to convert the letter to uppercase
    }

CharacterCasing , , KeyPress , if . KeyPress.

:

if(e.Control is TextBox)
        {
          ((TextBox) (e.Control)).CharacterCasing = CharacterCasing.Upper;
        }
+7

, ( TextBox), CharacterCasing.

+2

EditingControlShowing Datagridview

private void dgvGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
      if (dgvGrid.CurrentCell.ColumnIndex == 0 || dgvGrid.CurrentCell.ColumnIndex == 2)
          {
           if (e.Control is TextBox)
              {
                ((TextBox)(e.Control)).CharacterCasing = CharacterCasing.Upper;
              }
          }
    }

0
source

Source: https://habr.com/ru/post/1753132/


All Articles