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