locked
Datagrid Column Formatting in Code Behind RRS feed

  • Question

  • I have a WPF Datagrid whose values are populated via ItemsSource method in VB code behind. Some of the columns contain date values and I'm looking for a way to modify the formatting of these columns, in code behind, to represent a small date format.

    I know that this can easily be done in XAML using StringFormat but my columns are not defined in XAML, but rather in code behind. Right now I use code to either show or hide columns as such: gridMyGrid.Columns(0).Visibility = Windows.Visibility.Hidden

    Is there similar code to change the formatting?

    Thank you, Herb

     


    Herb
    Saturday, January 29, 2011 7:10 PM

Answers

  • Hi PreservationProgrammer,

    So, it seems you want to generate the columns automatically from the bound data, and yes, your code is a good solution to change the Binding details while the DataGrid is generating the columns. 

    If you do not want to generate the columns automatically, that you could try my solution before loading the data to DataGrid. Since we should declare the columns defination for the DataGrid before applying the item template. DataGrid does not have the columns elements in the visual tree at run-time. Only have the corresponding DataGridRow.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 1, 2011 5:29 AM

All replies

  • Hi PreservationProgrammer,

    Yes, we can transform any code in XAML to the behind code, for BindingBase.StringFormat property, we could declare a Binding instance in behind code, and set the Binding property of the corresponding column with the StringFormat value. Please try below code:

    DataGridTextColumn textcolumn = new DataGridTextColumn() { Header = "Name" };      
    Binding binding = new Binding("Name") { StringFormat = "as {0}" };
    textcolumn.Binding = binding;
    datagrid.Columns.Add(textcolumn);
    

    VB.Net:

    Dim textcolumn As New DataGridTextColumn() With { .Header = "Name" }
    Dim binding As New Binding("Name") With { .StringFormat = "as {0}" }
    textcolumn.Binding = binding
    datagrid.Columns.Add(textcolumn)
    

    Sincerely,

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 31, 2011 2:33 AM
  • Hello Bob and thank you for the excellent response. Your code example is in the context of creating the datagrid in code and assigning the colum format at that time.

    Unfortunately, my situation is such that the datagrid is defined by the ItemsSource method so I need a way to format the string for existng columns. Over the weekend I found that using AutoGeneratedColumns works for what I am doing.

    Private Sub grdMyDataGrid_AutoGeneratedColumns(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdMyDataGrid.AutoGeneratedColumns
    For Each col As DataGridColumn In grdMyDataGrid.Columns
       Select Case col.Header.ToString
          Case "DateAdded"
             If col.GetType.ToString = "System.Windows.Controls.DataGridTextColumn" Then
                Dim dg As DataGridTextColumn = col
                dg.Binding.StringFormat = "MM/dd/yyyy"
             End If
          End Select
       Next
    End Sub

    If there is a better way please let me know.

    Thank you, Herb

     


    Herb
    • Proposed as answer by Jie Bao Tuesday, February 1, 2011 5:29 AM
    Monday, January 31, 2011 2:13 PM
  • Hi PreservationProgrammer,

    So, it seems you want to generate the columns automatically from the bound data, and yes, your code is a good solution to change the Binding details while the DataGrid is generating the columns. 

    If you do not want to generate the columns automatically, that you could try my solution before loading the data to DataGrid. Since we should declare the columns defination for the DataGrid before applying the item template. DataGrid does not have the columns elements in the visual tree at run-time. Only have the corresponding DataGridRow.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 1, 2011 5:29 AM