如何在C1FlexGrid中设置特定单元格的样式?

时间:2014-09-18 14:58:25

标签: vb.net componentone c1flexgrid

我需要以货币格式在C1FlexGird中显示一个单元格,因此我尝试使用货币格式创建样式,并在为单元格指定值后应用样式。加载网格时,单元格值不包含任何格式。谢谢你的帮助!

'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

1 个答案:

答案 0 :(得分:1)

ComponentOne FlexGrid for WinForms便于用户在网格列中输入货币值。使用C1FlexGrid中Column对象的Format属性公开此功能。使用此属性,可以更改任何整数类型列的格式以表示货币。可以看出货币格式受当前区域设置的约束。因此,对于必须显示相同区域设置的货币的情况,将使用此属性。

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

但是,有许多用例在单个网格中使用不同的货币。使用OwnerDrawCell,您只需要传递单元格/列/行的格式字符串,然后转义此“限制”。请参阅以下代码片段来完成此操作:

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

在此处找到示例:http://our.componentone.com/wp-content/uploads/2014/09/FlexGridCurrencyVB.zip