שתף באמצעות


Change a datagridview cell to upper case

Question

Thursday, August 28, 2008 7:01 PM

Hello All,

Does anyone know how to change the format of a cell in a datagridview to upper case?  The code that I use to change the format is:

DataGrid2.Columns("Misc Amount").DefaultCellStyle.Format = "What Goes Here"

 Does anyone know what I use for What Goes Here?  I can format for numeric, currency, date and time just fine (n,c etc.).  But I do not know how to convert a cell to upper after someone has type data in that cell.

Regards,

Rob

All replies (8)

Thursday, August 28, 2008 8:00 PM ✅Answered

because the default cell format is a string intelisence gives you .ToUpper(). but I don't think that will set the value in the cell to upper case. All the format strings I have seen format numbers you may have to handle the upper case in an event like CellEndEdit or CellLeave

    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit  
        Dim NewValue As String 
 
        'Apply to certin columns  
        Select Case e.ColumnIndex  
            Case 2, 3, 6  
                NewValue = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value  
 
                DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = NewValue.ToUpper  
        End Select 
 
        'or  
 
        'Apply to all columns that are of String datatype  
        If DataGridView1.Columns(e.ColumnIndex).ValueType Is GetType(String) Then 
            NewValue = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value  
 
            DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = NewValue.ToUpper  
        End If 
    End Sub 

Thursday, August 28, 2008 8:29 PM ✅Answered

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  
                strUpperValue = DataGrid2.CurrentCell.Value  
                DataGrid2.CurrentCell.Value = strUpperValue.ToUpper  
        End Select  
 
 
    End Sub 

Sorry,  this was the one that worked.  If I used the CellLeave I get an error about DBNull can't be converted to string.  So I used the CellEndEdit and it worked great.


Thursday, August 28, 2008 7:22 PM | 1 vote

 DataGrid2.Columns("Misc Amount").DefaultCellStyle.Format.ToUpper()


Thursday, August 28, 2008 8:14 PM

I believe you can look into the FormatProvider property of the DefaultCellStyle.  I've never played with it myself but it allows you to provide custom formatting.


Thursday, August 28, 2008 8:20 PM

Thanks for the help.  I was able to get it to work like this:

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

Thanks again.

Rob


Thursday, August 28, 2008 11:31 PM | 1 vote

Even with CellEdit I would check for DbNull Something like

If DataGrid2.CurrentCell.Value Is DbNull.Value Then 
    Exit Sub 
End If 

Thursday, September 4, 2008 11:47 PM

Thanks,

I added that and kept the error from occuring. 

Rob


Friday, September 5, 2008 1:02 AM

Rob,

You might try to put it in the DataGridView_CellValidating event of the grid... then if you do e.Cancel=true it wont let the person leave the cell edit mode if they have something in there that wont fly like DBNull.  The grid has a built in error handler you can use as well that works great: myGrid.Rows(e.RowIndex).errortext="Yo! Can't put that here!" pops a nice red exclamation mark for you without any work :)

enjoy!

Michael