none
Converting Text to UPPER case in a datagridview control

    Question

  • Hi,  I've tried everything but don't seem to be able to get this to work.

    The seniario is this.  A user inputs some text in a cell of a datagridview control and I want the text to be converted into upper case before it is stored in the database.  This data is a KEY field to a table.

    I've tried the format method of the defaultcellstyle and the onvalidate row, and cell events but can't seem to be able to figure this out.

    Has anyone ever needed to do this before?  And, would you mind tell me how to do it?

    Thanks,

    Dave

    Thursday, September 21, 2006 7:33 PM

Answers

  • To make sure the data entered into the textbox column is Upper Case you can always set the datagridview's textbox's character casing in the EditingControlShowing event

     



        Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            If TypeOf e.Control Is TextBox Then
                DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
            End If
        End Sub

     

    Thursday, September 21, 2006 8:54 PM

All replies

  • you need to convert that row/column of data into a string then do a ToUpper() on it and store it back to the cell. You could implement the cellEndEdit event and use this to change the text on the given row and column
    Thursday, September 21, 2006 7:37 PM
  • Thank Sir, 

    That was it.  It worked like a charm

    Just one question.. is there a why to force it to uppercase as the data is keyed in?

    Thank you

    Thursday, September 21, 2006 7:44 PM
  • glad I could help my friend.

    I'm not sure how you would do what you are asking, I know you can implement a textchanged event but this is easily done if it was a single UI control, like a textbox, but when it comes to a datagridview, things are different.

    Thursday, September 21, 2006 7:56 PM
  • Hi again,

    May I please ask you one more question.

    Here is the code:

    Private Sub dgMenuRoles_CellEndEdit(ByVal sender As Object, _

    ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _

    Handles dgMenuRoles.CellEndEdit

    If Not (Me.dgMenuRoles.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Is Nothing Then

    Me.dgMenuRoles.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = Me.dgMenuRoles.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString.ToUpper

    End If

    End Sub

    The problem is that this code is executed even when the normal edit routines in VB are clearing out the data.  For instance, I have a single field on a single datagridview.  The field is a key item.  When i type in any value such as "A" as a field, it works.  When I type in "A" again on another line,  VB is correctly complain that this is a dup value and clears the row.  But this code still executes and complains about a null value.

    Would you know how to keep this code from running or not running if there was an error, or, running before the other validation event occurs?

    Thank you.

    Thursday, September 21, 2006 8:01 PM
  • I will try my best to help.

    Well this is true of course since the field is a key (unique) field so duplicates are not allowed, as correctly said.

    I remember I had this a while back whilst I was experimenting. Does a MessageBox appear automatically when you enter a duplicate value in the datagridview cell? It should say something like to override the error message/display a custom message or something, to implement the error event? Take a note of that error event and implement it, then in this event simply set focus back to the row it was on.

    I am not sure as it has been a while but will recheck.

    Thursday, September 21, 2006 8:10 PM
  • Hi,

    Yes it says something about implement your own DATAERROR event to not display this message (something like that).

    I did try to implement my own event but i found then I needed to check for everything like dup index values, etc.  I was kind of hoping not to have to write code to that level of detail.. Mostly cause I'm new to VB.  I usually use ACCESS or Oracle Forms.

    I'll work on it a bit more.  I've spent the day on this one thing.  New to VB.

    Thank you for your help.  If you see another way please let me know.

    Thanks,

    Thursday, September 21, 2006 8:14 PM
  • Hi,

    This works:

    Private Sub dgMenuRoles_CellEndEdit(ByVal sender As Object, _

    ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _

    Handles dgMenuRoles.CellEndEdit

    'MsgBox(Me.dgMenuRoles.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)

    If Not (Me.dgMenuRoles.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Is Nothing Then

    Me.dgMenuRoles.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = Me.dgMenuRoles.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString.ToUpper

    End If

    End Sub

    ----------------------------------------------------------

    I put the test around the assignment.  I don't know why it didn't work in the last post but it does work now.

    Thank you for helping me deal with this.

    Dave

     

     

    Thursday, September 21, 2006 8:18 PM
  • To make sure the data entered into the textbox column is Upper Case you can always set the datagridview's textbox's character casing in the EditingControlShowing event

     



        Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            If TypeOf e.Control Is TextBox Then
                DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
            End If
        End Sub

     

    Thursday, September 21, 2006 8:54 PM
  • Thanks Ken :-) I do have a question if you don't mind.

    Does this only apply for a textbox? What I mean is, the actual cell you see where you type in the data - is that a textbox control or some other control? If its not a textbox control (the cell) then how can you achieve the same thing?

    Thursday, September 21, 2006 9:29 PM
  • Hi,

    Your solution worked... Thank you!

    May I please ask you a question about masks text items?  I need to edit what users input and have a proper mask applied to it.  I tried to use the Masked Text Box but it forces me to enter the entire number in to fill the mask.. 

    For example, the mask is "###,###,##0.00".  The user has to enter all the digits can not just enter "9.35" in the beginning part of the cell.  This isn't really that friendly...  Can you please tell me how you would set this up?

     

    Thank you,

     

    Dave

    Tuesday, September 26, 2006 8:16 PM
  • Thank you very much for posting this.  I found it very useful in a project I am working on currently.  I asked a few other developers around here, and no one knew how to do it.  You are the Man.

    Monday, October 08, 2007 7:24 PM
  • Thanks for that.

     

    In C# I came up with:

     

    Code Snippet

    private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

    {

    if(e.Control is TextBox)

    ((TextBox)e.Control).CharacterCasing = CharacterCasing.Upper;

    }

     

     

     

    Now, what if I wanted only some of the textbox columns in the grid to be forced to uppercase?

     

    I thought I could derive a new Control from DGVTextBoxColumn, add a member called ForceUppercase and check for this in the handler.

     

    But, is there a simpler way?

    Thursday, March 27, 2008 6:41 PM
  • I came up with two solutions for the problem of wanting SOME textbox columns uppercase:

     

    1. This one is a hack:  Make use of the tag of the column's default CellStyle to set whether the text should be uppercase.

     

    First set the tag to indicate uppercase: something like "myUpperCaseColumn.DefaultCellStyle.Tag = "TOUPPER"" in the form load (or you can probably set this in the designer)

     

    Then, when the editing control shows, set the underlying text case to upper if the tag is set.  Keep in mind the tag can be any object, so you could indicate other formatting if you want.

     

    Code Snippet

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

    {

    if((e.Control is TextBox) && (e.CellStyle.Tag != null) && (e.CellStyle.Tag.ToString() == "TOUPPER"))

    ((TextBox)e.Control).CharacterCasing = CharacterCasing.Upper;

    else

    ((TextBox)e.Control).CharacterCasing = CharacterCasing.Normal;

    }

     

     

     

    btw, the else clause is important, all columns seem to share the same TextBox for editing, so once you set it for one column, you have to unset it for other columns that don't need uppercase.

     

    2. This is the solution I chose:  I made use of a sample project I got from Microsoft to create a MaskedTextBoxColumn.  Setting the mask to ">" makes the chars uppercase.

     

    Here is the link: http://msdn2.microsoft.com/en-us/library/ms180996.aspx

     

    NOTE: I found two problems with this implementation: Both the MaskedTextBoxCell and MaskedTextBoxColumn are missing their clone overrides, so they do not work in the designer.  Here are the fixes:

     

    Code Snippet

            public override object Clone()
            {
                MaskedTextBoxCell mtbc = (MaskedTextBoxCell)base.Clone();
                mtbc.mask = this.mask;
                mtbc.promptChar = this.promptChar;
                mtbc.includePrompt = this.includePrompt;
                mtbc.IncludeLiterals = this.includeLiterals;
                mtbc.validatingType = this.validatingType;

                return mtbc;
            }

     

     

    Code Snippet

            public override object Clone()
            {
                MaskedTextBoxColumn mtbc = (MaskedTextBoxColumn)base.Clone();
                mtbc.mask = this.mask;
                mtbc.promptChar = this.promptChar;
                mtbc.includePrompt = this.includePrompt;
                mtbc.IncludeLiterals = this.includeLiterals;
                mtbc.validatingType = this.validatingType;

                return mtbc;
            }

     

     

     

    Friday, March 28, 2008 7:21 PM
  • Hello Mike Parker,

    The solution you offered for uppercase view in datagrid view was excellent............I tried the method 1 and it is working fine. Thank you very much

    Wednesday, April 23, 2008 11:24 AM
  • hello

    KEN Tucker this code is great thanks 

     

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            If TypeOf e.Control Is TextBox Then
                DirectCast(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
            End If
        End Sub
    
    

     


    • Proposed as answer by Black Angle Sunday, June 05, 2011 4:39 PM
    Sunday, June 05, 2011 4:39 PM
  • Another solution is to use the fact that every cell is in a colum.  Here's what I used to force upper casing only on the column I wanted.

       Private Sub CategoryDataGridView_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles CategoryDataGridView.EditingControlShowing
            If CategoryDataGridView.CurrentCell.OwningColumn.HeaderText = "Category" Then
                CType(e.Control, TextBox).CharacterCasing = CharacterCasing.Upper
            End If
        End Sub
    

    Mike.

    Friday, October 14, 2011 8:21 PM
  • Let's assume you have two user-enterable columns in your datagrid, one is a textbox which you want only upper-case, and the other allows normal case entry. Let's also assume that upon entry into the modifiable cells, (the CellEnter Event), you issue a BeginEdit()

    To have the typed-in data appear in UpperCase, use the EditingControlShowing (which would be triggered upon the BeginEdit() command) event on the datagrid, as follows:

     

            private void MyDataGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

            {
                // Set case to Upper for this column
                if (MyDataGrid.CurrentCell.ColumnIndex == 0)
                    ((TextBox)e.Control).CharacterCasing = CharacterCasing.Upper;
                // Set case to Upper/Lower for this column
                if (MyDataGrid.CurrentCell.ColumnIndex == 1)
                    ((TextBox)e.Control).CharacterCasing = CharacterCasing.Normal;
            }    

     

    Note: Although .Normal is the default, you still need to set it to .Normal on the other column, since when you set it for the first column, it stays upper for the entire datagrid until you change it

     

     

    Tuesday, January 03, 2012 7:52 PM