vb.netcomponentonec1flexgrid

How to style a particular cell in C1FlexGird?


I need to display a cell in C1FlexGird in currency format so I am trying to create a style with currency format and apply the style after assigning a value to the cell. The cell value does not include any formatting when the gird is loaded. Thanks for any help!

'Create Currency Style
Dim cs As C1.Win.C1FlexGrid.CellStyle
cs.DataType = GetType(String)
cs.Format = "c2"

'Set the value
fg(iRow, 1) = value

'Apply style to cell
 rg = fg.GetCellRange(iRow, 1)
 rg.Style = cs

Solution

  • ComponentOne FlexGrid for WinForms facilitates users to enter currency values in the columns of the grid. This feature is exposed with the Format property of the Column object in C1FlexGrid. With this property, it is possible to change the format of any integer type column to represent currencies. It is seen that the currency format is subject to the current locale setting. Thus, for the scenarios where currencies for the same locale must be displayed, this property is used.

    ' Currency.
    _flex.Cols(2).Format = "c"
    

    However, there are many use cases where different currencies are to be used in a single grid. Using OwnerDrawCell, all you need is to pass the Format string for the cell/column/row, and you escape this “limitation” . Please refer to the following snippet which accomplishes this:

    Private Sub _flex_OwnerDrawCell(sender As System.Object, e As OwnerDrawCellEventArgs) Handles _flex.OwnerDrawCell
     Select Case _flex.Cols(e.Col).Name
        Case "Pound"
            Try
                Dim i As Integer = CInt(Me._flex(e.Row, e.Col))
                e.Text = String.Format("{0:£#,##0}", i)
            Catch
            End Try
        Exit Select
    
        Case "Dollar"
            Try
                Dim i As Integer = CInt(Me._flex(e.Row, e.Col))
                e.Text = String.Format("{0:$#,##0}", i)
            Catch
            End Try
        Exit Select
    
        Case "Euro"
            Try
                Dim i As Integer = CInt(Me._flex(e.Row, e.Col))
                e.Text = String.Format("{0:€#,##0}", i)
            Catch
            End Try
        Exit Select
    
        Case "Yen/Yuan"
            Try
                Dim i As Integer = CInt(Me._flex(e.Row, e.Col))
                e.Text = String.Format("{0:¥#,##0}", i)
            Catch
            End Try
        Exit Select
    
        Case "Won"
            Try
                Dim i As Integer = CInt(Me._flex(e.Row, e.Col))
                e.Text = String.Format("{0:?#,##0}", i)
            Catch
            End Try
        Exit Select
    
        Case Else
        Exit Select
    
     End Select
    End Sub
    

    Find the sample here: http://our.componentone.com/wp-content/uploads/2014/09/FlexGridCurrencyVB.zip