我不知道如何在WPF中转换datagrid中的列以显示2个小数位。 有人可以帮我吗?
这是我用于填充数据网格的代码。
Public Sub ListaZaposlenikaZaObracun()
databasePath = lblPath.Content
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" &
" Data Source=" & databasePath & ";Persist Security Info=False;")
cn.Open()
cm = New OleDb.OleDbCommand("SELECT * From PripremaObracuna", cn)
adp = New OleDb.OleDbDataAdapter(cm)
adp.Fill(ds, "PripremaObracuna")
DgvPriprema.ItemsSource = ds.Tables("PripremaObracuna").AsDataView()
End Sub
上面的代码有效,现在我需要将6列转换为2个小数位的代码。
XML代码是:
<Window x:Class="Obracun"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PlateApp"
mc:Ignorable="d"
Title="Obracun" Height="666.848" Width="1250.529" Loaded="Window_Loaded" Closing="Window_Closing">
<Grid>
<TextBox x:Name="TxtMjesec" HorizontalAlignment="Left" Height="25" Margin="347,70,0,0" TextWrapping="Wrap" Text="Mjesec" VerticalAlignment="Top" Width="90"/>
<TextBox x:Name="TxtFirstDay" HorizontalAlignment="Left" Height="25" Margin="40,114,0,0" TextWrapping="Wrap" Text="Period od" VerticalAlignment="Top" Width="118"/>
<TextBox x:Name="TxtLastDay" HorizontalAlignment="Left" Height="25" Margin="188,114,0,0" TextWrapping="Wrap" Text="Period do" VerticalAlignment="Top" Width="113"/>
<ComboBox x:Name="CmbMjesec" HorizontalAlignment="Left" Height="22" Margin="40,73,0,0" VerticalAlignment="Top" Width="118"/>
<ComboBox x:Name="CmbGodina" HorizontalAlignment="Left" Height="22" Margin="183,73,0,0" VerticalAlignment="Top" Width="118" RenderTransformOrigin="-0.042,0"/>
<Label Content="Mjesec" HorizontalAlignment="Left" Height="27" Margin="40,41,0,0" VerticalAlignment="Top" Width="112"/>
<Label Content="Godina" HorizontalAlignment="Left" Height="27" Margin="183,41,0,0" VerticalAlignment="Top" Width="118"/>
<DataGrid x:Name="DgvPriprema" HorizontalAlignment="Left" Height="337" Margin="10,172,0,0" VerticalAlignment="Top" Width="1223" MinWidth="100" MinHeight="28" ColumnHeaderHeight="50" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
</DataGrid>
<Button x:Name="BtnPopuna" Content="Button" HorizontalAlignment="Left" Height="38" Margin="532,68,0,0" VerticalAlignment="Top" Width="147"/>
<Label x:Name="lblPath" Content="Label" HorizontalAlignment="Left" Height="24" Margin="10,602,0,0" VerticalAlignment="Top" Width="111"/>
<Label x:Name="lblAktiv" Content="1" HorizontalAlignment="Left" Height="29" Margin="449,127,0,0" VerticalAlignment="Top" Width="112"/>
<Button x:Name="BtnObracunaj" Content="Button" HorizontalAlignment="Left" Margin="763,65,0,0" VerticalAlignment="Top" Width="136" Height="44"/>
<TextBox x:Name="txtIznosOdbitka" HorizontalAlignment="Left" Height="27" Margin="587,129,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="127"/>
</Grid>
答案 0 :(得分:0)
我找到了解决方法! 绑定并使用StringFormat属性后,我添加了所需的格式。
格式示例: StringFormat = {} {0:N}
要显示用逗号分隔的数字(1,00),我添加了ConverterCulture = hr-HR
示例:
<DataGridTextColumn Binding="{Binding Path=OsnovicaPOreza,StringFormat={}{0:N}, ConverterCulture=hr-HR}" Header="Osnovica poreza" >
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
感谢所有人!