none
Upper Case Letters in DataGrid

    Question

  • I am a newbie in C# . I have a datagrid control and i want my user to enter only upper case letters. I dont know how to do it. Please help me out.

    Thanks in advance

     

    Wednesday, December 13, 2006 12:51 PM

Answers

  • When you post a question about the data grid please be sure to mention whether it is web or WinForms as the control exists in both but is not the same.  Additionally be aware that in v2 an entirely new set of grid controls were introduced but they are still often referred to as a data grid.

    If you have a DataGrid with a DataGridTextBoxColumn then you will need to get access to the underlying DataGridTextBox after it is created.  You can then set the CharacterCasing property to force it to upper case.  You will need to wait until the control is created either initially or after data binding occurs so you'll need to handle the appropriate event in the grid.  This is for WinForms only.

    The web version does not expose a text box at all.  Even if it did the TextBox class does not support casing.  Therefore you'll have to create a template column that shows a custom text box when in edit mode.  You'll need to use scripting on the client to uppercase letters as the user types.  You'll then need to hook the text change notification on the client to run your script.

    For v2 with the GridView for web apps you are basically in the same boat.  The BoundColumn does support a formatting string but I'm not aware of a format specifier that will upper case a string.  You could write one though.  If you go this route be sure to set the ApplyFormatInEditMode(?) so the formatting applies in both read-only and edit mode.  You could also create your own column deriving from DataControlField.

    The DataGridView for WinForms is like the GridView.  You have to set the formatting option for the column but you'll have to create the format string first.  You can also create your own text box column deriving from DataGridViewColumn.

    Michael Taylor - 12/13/06

     

    Wednesday, December 13, 2006 2:15 PM
    Moderator

All replies

  • When you post a question about the data grid please be sure to mention whether it is web or WinForms as the control exists in both but is not the same.  Additionally be aware that in v2 an entirely new set of grid controls were introduced but they are still often referred to as a data grid.

    If you have a DataGrid with a DataGridTextBoxColumn then you will need to get access to the underlying DataGridTextBox after it is created.  You can then set the CharacterCasing property to force it to upper case.  You will need to wait until the control is created either initially or after data binding occurs so you'll need to handle the appropriate event in the grid.  This is for WinForms only.

    The web version does not expose a text box at all.  Even if it did the TextBox class does not support casing.  Therefore you'll have to create a template column that shows a custom text box when in edit mode.  You'll need to use scripting on the client to uppercase letters as the user types.  You'll then need to hook the text change notification on the client to run your script.

    For v2 with the GridView for web apps you are basically in the same boat.  The BoundColumn does support a formatting string but I'm not aware of a format specifier that will upper case a string.  You could write one though.  If you go this route be sure to set the ApplyFormatInEditMode(?) so the formatting applies in both read-only and edit mode.  You could also create your own column deriving from DataControlField.

    The DataGridView for WinForms is like the GridView.  You have to set the formatting option for the column but you'll have to create the format string first.  You can also create your own text box column deriving from DataGridViewColumn.

    Michael Taylor - 12/13/06

     

    Wednesday, December 13, 2006 2:15 PM
    Moderator
  •  Hello,

    Do you know the format for upper case in a datagridview in VB 2005? 

    If I use this code:

    DataGrid2.Columns("Storage Location").DefaultCellStyle.Format = "What goes here?"


    What do I replace What goes here with?

    Thanks,

    Rob
    Thursday, August 28, 2008 6:48 PM
  • Hey Rob;

    To complete your line of code you want:

    DataGrid2.Columns("Storage Location").DefaultCellStyle.Format.ToUpper()

    One dot short of the answer.

    David M. Nichols contract software engineer
    Thursday, August 28, 2008 7:56 PM
  • David,

    I had already tried that and it did not work.  I got it to work by using the CellEndEdit event of the datagridview like this:

    Private Sub DataGrid2_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGrid2.CellEndEdit  
            Dim strUpperValue As String  
            'Apply to certin columns        
            Select Case e.ColumnIndex  
                Case 8  
                    If IsNumeric(DataGrid2.CurrentCell.Value) Then  
     
                    Else  
                        strUpperValue = DataGrid2.CurrentCell.Value  
                        DataGrid2.CurrentCell.Value = strUpperValue.ToUpper  
                    End If  
            End Select  
        End Sub 

    I have to add the IsNumeric statement for my needs because the user can type a 1 or ea in the field and we always want the ea to be upper case.

    Thanks for the response.

    Rob
    Thursday, August 28, 2008 9:21 PM