none
Format Columns of a DataGrid

    Question

  • I have a DataGrid that's being populated with a List<> at the runtime. I'm trying to figure out how to format some of the columns of the DataGrid. I have a number column that's currently displaying like: 51.3232424. I'd rather it display as currency like $51.32.
    Thursday, February 10, 2011 1:14 PM

Answers

  • Didn't see your class.  Update.

    <DataGrid AutoGenerateColumns="False" 
         Height="88" 
         HorizontalAlignment="Left" 
         Margin="12,338,0,0" 
         Name="dataGrid1" 
         VerticalAlignment="Top"  
         Width="637" 
         ItemsSource="{Binding}">
     <DataGrid.Columns>
      <DataGridTextColumn Header="Date Time" 
         Binding="{Binding Path=DateTime}" />
      <DataGridTextColumn Header="Value" 
         Binding="{Binding Path=Value, StringFormat={}{0:C}}" />
     
     </DataGrid.Columns>
    </DataGrid>
    
    

    • Marked as answer by Gustavo Binnie Thursday, February 10, 2011 3:43 PM
    Thursday, February 10, 2011 2:31 PM

All replies

  • I think you can utilize the StringFormat property in your Column Binding.

    SOmething like:

    "{Binding Path=Salary, StringFormat={}{0:C}}"

     

    Thursday, February 10, 2011 1:44 PM
  • KP_SES,

    My XAML:

    <DataGrid AutoGenerateColumns="True" Height="88" HorizontalAlignment="Left" Margin="12,338,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="637" ItemsSource="{Binding}"/>

    And my .cs file:

    dataGrid1.ItemsSource = List;

     

    How can I format only one column?

    Thursday, February 10, 2011 1:55 PM
  • You would need to create all your columns manually and turn off AutoGenerateColumns to format with binding.  Not sure how you could accomplish this task using AutoGenerateColumns.


    Thursday, February 10, 2011 2:00 PM
  • Kp_Ses,

    I have a List (List<DataValue> List1) of struct with two properties (one DateTime and one double):

     

      public struct DataValue
      {
        private DateTime _data;
        public DateTime Data
        {
          get { return _data; }
          set { _data = value; }
        }
    
        private double _value;
        public double Value
        {
          get { return _value; }
          set { _value = value; }
        }
      }
    
    How can I bind this List with the DataGrid and format the columns with the AutoGenerateColumns turned off?

     

    Thursday, February 10, 2011 2:10 PM
  • This is an example of how to create one column.  You would need to create a column for every Property in your class you want to display.

     <DataGrid AutoGenerateColumns="False" 
             Height="88" 
             HorizontalAlignment="Left" 
             Margin="12,338,0,0" 
             Name="dataGrid1" 
             VerticalAlignment="Top"   
             Width="637" 
             ItemsSource="{Binding}">
      <DataGrid.Columns>
       
        <DataGridTextColumn Header="Salary" 
             Binding="{Binding Path=Salary, StringFormat={}{0:C}}" />
     
      </DataGrid.Columns>
    </DataGrid>
    
    

    Thursday, February 10, 2011 2:19 PM
  • Didn't see your class.  Update.

    <DataGrid AutoGenerateColumns="False" 
         Height="88" 
         HorizontalAlignment="Left" 
         Margin="12,338,0,0" 
         Name="dataGrid1" 
         VerticalAlignment="Top"  
         Width="637" 
         ItemsSource="{Binding}">
     <DataGrid.Columns>
      <DataGridTextColumn Header="Date Time" 
         Binding="{Binding Path=DateTime}" />
      <DataGridTextColumn Header="Value" 
         Binding="{Binding Path=Value, StringFormat={}{0:C}}" />
     
     </DataGrid.Columns>
    </DataGrid>
    
    

    • Marked as answer by Gustavo Binnie Thursday, February 10, 2011 3:43 PM
    Thursday, February 10, 2011 2:31 PM
  • Thanks KP_SES. That works!
    Thursday, February 10, 2011 3:43 PM