I have a wpf datagrid,
Here’s my datagrid
<dg:DataGrid x:Name="dataGrid" AutoGenerateColumns="false"
ColumnHeaderStyle="{StaticResource columnHeaderStyle}"
AlternationCount="2" RowBackground="Beige"
RowStyle="{StaticResource rowStyle}"
AlternatingRowBackground="LightBlue"
HeadersVisibility="All"
HorizontalGridLinesBrush="#DDDDDD"
VerticalGridLinesBrush="#DDDDDD" Grid.ColumnSpan="2" Margin="0,0,0,26" IsReadOnly="True" ColumnHeaderHeight="30">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Header=" Task Id" Binding="{Binding Path=TaskId}" Width="60" />
<dg:DataGridTextColumn Header="Order Description" Binding="{Binding Path=OrderDescription}" Width="120"/>
<dg:DataGridTextColumn Header="Final Client Name" Binding="{Binding Path=ClientName}" Width="110"/>
<dg:DataGridTextColumn Header="Order Date" Binding="{Binding Path=OrderDate}" Width="80"/>
<dg:DataGridTextColumn Header="Task Description" Binding="{Binding Path=TaskDescription}" Width="130"/>
<dg:DataGridTextColumn Header="Group Name Short" Binding="{Binding Path=GroupNameShort}" Width="116"/>
<dg:DataGridTemplateColumn MinWidth="100" Header=" Actions">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="DetailButton_Click">Close</Button>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
In aspx to change text we use onrowdatabound for example, here in wpf how can I change the field Order Description and Order Date.
I have two functions to change the text, that are convertDatetimeToDate(string datetime) e htmltotext(string text).
You can use a value converter on the binding. Create a class that implements IValueConverter, and set an instance of that class as the Converter property of the binding.
In your XAML, create an instance of the converter. You may need to add a namespace reference:
And use it in the binding:
In 3.5 SP1 or later, you can also use the StringFormat property on the binding to do simple formatting. This should format the datetime as a plain date by doing the equivalent of
string.Format("{0:d}", OrderDate):