Reputation: 31
Basically, what I want to do is the WinForm Datagridview equivalent of dgvPreview.Columns(4).DefaultCellStyle.Format = "#,##0.00"
But instead of Datagridview, it's with Datagrid in WPF. Best I can do is assign a datatable to a Datagrid and change its alignment property.
DataGridPreview.ItemsSource = dtPreview.DefaultView
Private Sub BtnTest_Click(sender As Object, e As RoutedEventArgs) Handles BtnTest.Click
Dim txt As New DataGridTextColumn()
Dim s As New Style
s.Setters.Add(New Setter(TextBox.TextAlignmentProperty, TextAlignment.Right))
txt.CellStyle = s
DataGridPreview.Columns(4).CellStyle = s
'dgvPreview.Columns(4).DefaultCellStyle.Format = "#,##0.00"
End Sub
Please point me in the right direction. I'm trying to migrate from Winforms to WPF. And as much as possible I want to do this programmatically. I have also tried using the AutoGeneratingColumn but I can't figure it out.
If e.Column.Header.ToString = "Amount" Then
Dim dg As DataGridTextColumn = e.Column
dg.Binding.StringFormat = "#,000.00"
End If
Upvotes: 0
Views: 1221
Reputation: 524
If you are using AutoGeneratingColumn
, the best time to update the StringFormat
is on AutogeneratingColumn
event. Column's binding serves as a blueprint for the individual cells' binding, so for some updates it is important to do them before the cells are created. In C# it will be something like this:
public MainWindow()
{
InitializeComponent();
grid.ItemsSource = Enumerable.Range(0, 10).Select(s => new { Id = s });
}
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
(e.Column as DataGridTextColumn).Binding.StringFormat = "0.000";
}
Upvotes: 1