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. Code Behind Data Grid Style Issue

Code Behind Data Grid Style Issue

Scheduled Pinned Locked Moved WPF
helpcsswpfwcfjson
5 Posts 3 Posters 5 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 am generating a datagrid on the fly at runtime. [See this pic ](https://1drv.ms/u/s!AlkRTpT49yCMmgESCd3dl0wkqwa7?e=wWUFNP). I am generating a Data Table, and the go through each column of the table and create DataGridColumns. The first column, the Type, is read only. The rest are editable and will accept money values. I am trying to apply the cell style in code behind:

    foreach (string item in columnNames)
    {
    // Create binding
    Binding binding = new Binding(item);
    string styleName = "";

    // Get binding mode and style name
    if (item == "Type")
    {
        binding.Mode = BindingMode.OneTime;
        styleName = "budgetGridCellStyle";
    }
    else
    {
        binding.Mode = BindingMode.TwoWay;
        binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        styleName = "dataGridCurrencyCellStyle";
    }
    
    // Get the style
    var style = Application.Current.FindResource(styleName) as Style;
    
    // Bind the column
    var col = new DataGridTextColumn()
    {
        Header = item,
        Binding = binding,
        Visibility = Visibility.Visible,
        CellStyle = style
    };
    
    DataGridColumns.Add(col);
    

    }

    Here is the DataGridCellStyle

    <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>

    Yet, AFTER I save changes, the cells end up with 4 decimal places. Anyone have any ideas on how to fix this?

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    Richard DeemingR L 2 Replies Last reply
    0
    • K Kevin Marois

      I am generating a datagrid on the fly at runtime. [See this pic ](https://1drv.ms/u/s!AlkRTpT49yCMmgESCd3dl0wkqwa7?e=wWUFNP). I am generating a Data Table, and the go through each column of the table and create DataGridColumns. The first column, the Type, is read only. The rest are editable and will accept money values. I am trying to apply the cell style in code behind:

      foreach (string item in columnNames)
      {
      // Create binding
      Binding binding = new Binding(item);
      string styleName = "";

      // Get binding mode and style name
      if (item == "Type")
      {
          binding.Mode = BindingMode.OneTime;
          styleName = "budgetGridCellStyle";
      }
      else
      {
          binding.Mode = BindingMode.TwoWay;
          binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
          styleName = "dataGridCurrencyCellStyle";
      }
      
      // Get the style
      var style = Application.Current.FindResource(styleName) as Style;
      
      // Bind the column
      var col = new DataGridTextColumn()
      {
          Header = item,
          Binding = binding,
          Visibility = Visibility.Visible,
          CellStyle = style
      };
      
      DataGridColumns.Add(col);
      

      }

      Here is the DataGridCellStyle

      <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>

      Yet, AFTER I save changes, the cells end up with 4 decimal places. Anyone have any ideas on how to fix this?

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Kevin Marois wrote:

      The first column, the Type, is read only. The rest are read only

      Unless you meant to say that all columns are read-only, I suspect the rest are editable. :)


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      K 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Kevin Marois wrote:

        The first column, the Type, is read only. The rest are read only

        Unless you meant to say that all columns are read-only, I suspect the rest are editable. :)


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Yup, you're right. I corrected it.

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        1 Reply Last reply
        0
        • K Kevin Marois

          I am generating a datagrid on the fly at runtime. [See this pic ](https://1drv.ms/u/s!AlkRTpT49yCMmgESCd3dl0wkqwa7?e=wWUFNP). I am generating a Data Table, and the go through each column of the table and create DataGridColumns. The first column, the Type, is read only. The rest are editable and will accept money values. I am trying to apply the cell style in code behind:

          foreach (string item in columnNames)
          {
          // Create binding
          Binding binding = new Binding(item);
          string styleName = "";

          // Get binding mode and style name
          if (item == "Type")
          {
              binding.Mode = BindingMode.OneTime;
              styleName = "budgetGridCellStyle";
          }
          else
          {
              binding.Mode = BindingMode.TwoWay;
              binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
              styleName = "dataGridCurrencyCellStyle";
          }
          
          // Get the style
          var style = Application.Current.FindResource(styleName) as Style;
          
          // Bind the column
          var col = new DataGridTextColumn()
          {
              Header = item,
              Binding = binding,
              Visibility = Visibility.Visible,
              CellStyle = style
          };
          
          DataGridColumns.Add(col);
          

          }

          Here is the DataGridCellStyle

          <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>

          Yet, AFTER I save changes, the cells end up with 4 decimal places. Anyone have any ideas on how to fix this?

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          [BindingBase.StringFormat Property (System.Windows.Data) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.data.bindingbase.stringformat?view=windowsdesktop-6.0)

          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

          K 1 Reply Last reply
          0
          • L Lost User

            [BindingBase.StringFormat Property (System.Windows.Data) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.data.bindingbase.stringformat?view=windowsdesktop-6.0)

            "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            Thanks for the reply, but I don't see how that solves the issue

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            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