Converting Text to UPPER case in a datagridview control

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



Answer this question

Converting Text to UPPER case in a datagridview control

  • alan279

    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



  • T. RUIZ

    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.



  • dragoncells

    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


  • ytandeta

    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.



  • Sugan_Dave

    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.


  • R Raghu

    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,


  • prashant mulay

    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



  • B M E

    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

  • Chris Richner

    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


  • JohnD74

    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.



  • csi_hugh

    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


  • Converting Text to UPPER case in a datagridview control