Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Setting DataGrid Cell Style From Code Behind

Setting DataGrid Cell Style From Code Behind

Scheduled Pinned Locked Moved WPF
csswpfwcfquestioncareer
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have a UserControl with a data grid that I'm creating in code in a View Model. It looks like this [[^]](https://1drv.ms/u/s!AjBmoYAYz\_v2hBe-082OOoYPAw3r?e=q67bBY). I'm trying to set the Cell Style to show 2 decimal from the VM but you can see it's not working. The first column is a string, and the remainder are Decimal. Here's my Cell Style

    <Style x:Key="dataGridCurrencyCellStyle"
    BasedOn="{StaticResource dataGridCellStyle}"
    TargetType="{x:Type DataGridCell}">

    <Setter Property="Width" Value="75"/>
    <Setter Property="TextBlock.TextAlignment" Value="Right"/>
    <Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:0.##}}"/>
    <Setter Property="IsEnabled" Value="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}"/>
    

    </Style>

    Here's how I'm creating the table in the VM and setting the Style. The Style is set in the foreach loop towards the bottom.

    private void LoadTable()
    {
    if (Budgets.Count > 0)
    {
    // 1- Create the data table

        BudgetTable = null;
        BudgetTable = new DataTable();
    
        // Add the first column
        DataColumn column = new DataColumn();
        column.ColumnName = "Type";
        column.DataType = System.Type.GetType("System.String");
        column.ReadOnly = false;
        BudgetTable.Columns.Add(column);
    
    
    
        // Get a distinct list of Plan/Elevations
        List<string> planElevations = new List<string>();
    
        switch (BudgetType)
        {
            case BudgetType.Job:
                var sheets = JobSequenceSheets.Where(x => x.Status == "New").ToList();
                planElevations = sheets.Select(x => $"{x.Plan}{x.Elevation}").ToList();
                break;
    
            case BudgetType.Project:
                planElevations = ProjectPlanTypeSummaries.Select(x => $"{x.Plan}{x.Elevation}").ToList();
                break;
    
            default:
                throw new ArgumentException("Unhandled CASE statement");
                break;
        }
    
        // Add a column for every distinct Plan/Elevation
        foreach (var planElevation in planElevations)
        {
            column = new DataColumn();
    
            column.ColumnName = planElevation;
            column.Caption = planEle
    
    realJSOPR 1 Reply Last reply
    0
    • K Kevin Marois

      I have a UserControl with a data grid that I'm creating in code in a View Model. It looks like this [[^]](https://1drv.ms/u/s!AjBmoYAYz\_v2hBe-082OOoYPAw3r?e=q67bBY). I'm trying to set the Cell Style to show 2 decimal from the VM but you can see it's not working. The first column is a string, and the remainder are Decimal. Here's my Cell Style

      <Style x:Key="dataGridCurrencyCellStyle"
      BasedOn="{StaticResource dataGridCellStyle}"
      TargetType="{x:Type DataGridCell}">

      <Setter Property="Width" Value="75"/>
      <Setter Property="TextBlock.TextAlignment" Value="Right"/>
      <Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:0.##}}"/>
      <Setter Property="IsEnabled" Value="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}"/>
      

      </Style>

      Here's how I'm creating the table in the VM and setting the Style. The Style is set in the foreach loop towards the bottom.

      private void LoadTable()
      {
      if (Budgets.Count > 0)
      {
      // 1- Create the data table

          BudgetTable = null;
          BudgetTable = new DataTable();
      
          // Add the first column
          DataColumn column = new DataColumn();
          column.ColumnName = "Type";
          column.DataType = System.Type.GetType("System.String");
          column.ReadOnly = false;
          BudgetTable.Columns.Add(column);
      
      
      
          // Get a distinct list of Plan/Elevations
          List<string> planElevations = new List<string>();
      
          switch (BudgetType)
          {
              case BudgetType.Job:
                  var sheets = JobSequenceSheets.Where(x => x.Status == "New").ToList();
                  planElevations = sheets.Select(x => $"{x.Plan}{x.Elevation}").ToList();
                  break;
      
              case BudgetType.Project:
                  planElevations = ProjectPlanTypeSummaries.Select(x => $"{x.Plan}{x.Elevation}").ToList();
                  break;
      
              default:
                  throw new ArgumentException("Unhandled CASE statement");
                  break;
          }
      
          // Add a column for every distinct Plan/Elevation
          foreach (var planElevation in planElevations)
          {
              column = new DataColumn();
      
              column.ColumnName = planElevation;
              column.Caption = planEle
      
      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      If the way you're doing it works without side-effects, that's the right way to do it. :)

      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
      -----
      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
      -----
      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups