WPF DataGrid 时间列的显示问题
xaml中绑定:<DataGridTextColumnHeader="日期"Binding="{BindingPath=Date}"/>xaml.cs中:(s是SQLHe...
xaml中绑定:
<DataGridTextColumn Header="日期" Binding="{Binding Path=Date}"/>
xaml.cs中:(s是SQLHelper的一个实例)
s.Open();
string sql = "SELECT Date FROM DateTable";
DataTable dt = s.GetDataTable(sql);
dataGrid1.ItemsSource = dt.DefaultView;
显示出来的时间为:2/17/2011 12:00:00 AM,而数据库中的Date是2011-02-17,为什么会这样显示?
解决:xaml:
<--命名空间引用-->
<Window
xmlns:c="clr-namespace:...."
...
>
</Window>
<Window.Resources >
<c:DateConvert x:Key="DateConvert"/>
</Window.Resources>
……
<DataGridTextColumn Header="日期" Binding="{Binding Path=Date,Converter={StaticResource DateConvert}}">
</DataGridTextColumn>
DateConvert.cs
public class DateConvert : IValueConverter
{
public string Date(DateTime date)
{
return string.Format("{0:D}", date.ToShortDateString());
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Date((DateTime)value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
&nb 展开
<DataGridTextColumn Header="日期" Binding="{Binding Path=Date}"/>
xaml.cs中:(s是SQLHelper的一个实例)
s.Open();
string sql = "SELECT Date FROM DateTable";
DataTable dt = s.GetDataTable(sql);
dataGrid1.ItemsSource = dt.DefaultView;
显示出来的时间为:2/17/2011 12:00:00 AM,而数据库中的Date是2011-02-17,为什么会这样显示?
解决:xaml:
<--命名空间引用-->
<Window
xmlns:c="clr-namespace:...."
...
>
</Window>
<Window.Resources >
<c:DateConvert x:Key="DateConvert"/>
</Window.Resources>
……
<DataGridTextColumn Header="日期" Binding="{Binding Path=Date,Converter={StaticResource DateConvert}}">
</DataGridTextColumn>
DateConvert.cs
public class DateConvert : IValueConverter
{
public string Date(DateTime date)
{
return string.Format("{0:D}", date.ToShortDateString());
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Date((DateTime)value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
&nb 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询