This is a migrated thread and some comments may be shown as answers.

Set Data Format on a GridViewDataColumn programmatically.

3 Answers 495 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John Cleophas
Top achievements
Rank 1
John Cleophas asked on 21 Jul 2010, 04:01 PM
Hi

I am trying to set the data format of a column using the DataFormatString property of a GridViewDataColumn object. No matter what format string i pass the format just does not change. Below is a code snippet

 Public Sub InitializeGrid(ByVal Data As ObjectOptional ByVal ColumnList As List(Of GridColumn) = Nothing)
        _ColumnList = ColumnList
        _DataSource = Data
        If Not ColumnList Is Nothing Then
            rdgMain.AutoGenerateColumns = False

            'Setup the columns

            For Each col As GridColumn In _ColumnList

                Dim column As New GridViewDataColumn()
                column.DataMemberBinding = New Binding(col.ColumnName)
                column.Header = col.ColumnHeader
                column.UniqueName = col.ColumnName
                column.DataType = col.Datatype
                column.TextAlignment = col.TextAlignment
                'Force a format to test with
                column.DataFormatString = "{0:C}" 'col.FormatString
                If Not ColumnExists(col.ColumnName) Then
                    rdgMain.Columns.Add(column)
                End If
            Next

        End If

        rdgMain.ItemsSource = Data
        VisualStateManager.GoToState(Me"IdleState"True)
    End Sub

3 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 21 Jul 2010, 04:17 PM
Hello John Cleophas,

That is very strange. What is the actual data type?

I have prepared a sample project which works correctly. It formats the string both via XAML and in code behind.

Can you modify my sample project so that it reproduces the behavior and then send it back to us. You will need to open a separate support ticket in order to attach the modified sample project.

We are looking forward to hearing from you.

Kind regards,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
John Cleophas
Top achievements
Rank 1
answered on 22 Jul 2010, 08:55 AM
Hi Ross

I have logged support ticket and uploaded your app that i modified as well as one that i wrote to replicate the issue. I have identified the issue. Below is sample code that reproduces the issue: You will notice the 'Salary' property of the staffMember class is defined as string. This is intentional since my numeric data can be returned as string. When i apply a format to this property nothing happens.

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports System.Windows.Data

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()

        Dim staffList As New List(Of StaffMember)

        Dim john As New StaffMember
        Dim jim As New StaffMember
        Dim jill As New StaffMember

        With john
            .Salary = 30500
            .FirstName = "John"
            .LastName = "Smith"
        End With

        With jim
            .Salary = 62400
            .FirstName = "Jim"
            .LastName = "Bob"
        End With

        With jill
            .Salary = 21800
            .FirstName = "Jill"
            .LastName = "Williams"
        End With

        staffList.Add(john)
        staffList.Add(jim)
        staffList.Add(jill)

        grid.AutoGenerateColumns = False

        Dim gridColFirstName As New GridViewDataColumn
        Dim gridColLastName As New GridViewDataColumn
        Dim gridColSalary As New GridViewDataColumn

        With gridColFirstName
            .DataMemberBinding = New Binding("FirstName")

        End With

        With gridColLastName
            .DataMemberBinding = New Binding("LastName")
        End With

        With gridColSalary
            .DataMemberBinding = New Binding("Salary")
            '.DataType = GetType(Decimal)
            .DataFormatString = "{0:C}"
        End With

        grid.Columns.Add(gridColFirstName)
        grid.Columns.Add(gridColLastName)
        grid.Columns.Add(gridColSalary)

        grid.ItemsSource = staffList
    End Sub

End Class

Public Class StaffMember

    Private _FirstName As String
    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property


    Private _LastName As String
    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

    Private _Salary As String
    Public Property Salary() As String
        Get
            Return _Salary
        End Get
        Set(ByVal value As String)
            _Salary = value
        End Set
    End Property

End Class


0
Rossen Hristov
Telerik team
answered on 22 Jul 2010, 03:11 PM
Hi John Cleophas,

This is absolutely expected. The .NET Framework cannot do this. Numeric string formats are meant for numeric types. If you want to use numeric string formats you will have to convert your strings to numbers.

.NET is a strongly-typed platform.

Imagine that you write this lines of code:

string s = "I am a string";
string result = string.Format("{0:C2}", s);

What do you expect to see in the result variable?

Your only solution is to convert your strings to numbers in case you want to use the standard .NET string formatting pattern.

Best wishes,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
John Cleophas
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
John Cleophas
Top achievements
Rank 1
Share this question
or