[英]Display empty field in ListView when Datetime in Datatable is 01/01/1900
我有一個帶有5列的DataTable dtLeaves
,其中2個是DateTime StartDate, EndDate
列,可能具有minDate值01/01/1900
並且通過ListView LeavesListView
顯示了DataTable,ListView是否可以顯示日期01/01/1900
為空字段,而不是實際值? DataTable是從SQL Server填充的。
XAML
<ListView x:Name="LeavesListView" Margin="10,10,13,10" Background="White" Opacity="0.8" Grid.Row="2" FontFamily="Times New Roman" FontSize="14" FlowDirection="RightToLeft" SelectionChanged="LeavesListView_SelectionChanged" MouseDoubleClick="LeavesListView_MouseDoubleClick">
<ListView.View>
<GridView>
<GridViewColumn Header="تاريخ البدء" Width="120" TextBlock.TextAlignment="Center" DisplayMemberBinding="{Binding StartDate, StringFormat=yyyy/MM/dd}" FrameworkElement.FlowDirection="RightToLeft"/>
<GridViewColumn Header="تاريخ الأنتهاء" Width="120" TextBlock.TextAlignment="Center" DisplayMemberBinding="{Binding EndDate, StringFormat=yyyy/MM/dd}" FrameworkElement.FlowDirection="RightToLeft" />
<GridViewColumn Header="نوع الاجازة" Width="100" FrameworkElement.FlowDirection="RightToLeft">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LeaveType}" Foreground="Red" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
將數據加載到ListView中的代碼
public MainWindow()
{
InitializeComponent();
Variables.DtLeaves = DataLoad.LoadData("EmpLeaves", "");
}
private void LoadLeaveData()
{
try
{
if (EmployeesListView.SelectedIndex < 0) return;
Variables.DtLeaves.DefaultView.RowFilter = string.Format("EmpID = '{0}'",
Variables.Dt.Rows[EmployeesListView.SelectedIndex][0]);
Variables.DtLeaves.DefaultView.Sort = " StartDate ASC";
LeavesListView.ItemsSource = Variables.DtLeaves.DefaultView;
LeaveData.DataContext = LeavesListView.SelectedItem;
LeavesListView.SelectedIndex = 0;
LeavesListView.Items.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Error({0}): {1} ", ex.Message, ex.HResult), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
和DataLoad.Cs中的代碼
public static DataTable LoadData(string tableName, string filterString)
{
using (var con = new SqlConnection(Variables.ConString))
{
var table = new DataTable("");
con.StatisticsEnabled = true;
if (filterString != "")
filterString = string.Format(" WHERE {0}", filterString);
Variables.CmdString = string.Format("SELECT * FROM {0} {1}", tableName, filterString);
var cmd = new SqlCommand(Variables.CmdString, con);
var sda = new SqlDataAdapter(cmd);
sda.Fill(table);
Variables.CurrentStatistics = con.RetrieveStatistics();
return table;
}
}
您可以使用轉換器:
public class DateConverter : IValueConverter
{
private static readonly DateTime s_defaultDate = new DateTime(1900, 01, 01);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime date = (DateTime)value;
return date == s_defaultDate ? string.Empty : date.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<ListView x:Name="LeavesListView" Margin="10,10,13,10" Background="White" Opacity="0.8" Grid.Row="2" FontFamily="Times New Roman" FontSize="14" FlowDirection="RightToLeft" SelectionChanged="LeavesListView_SelectionChanged" MouseDoubleClick="LeavesListView_MouseDoubleClick">
<ListView.Resources>
<local:DateConverter x:Key="DateConverter" />
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="تاريخ البدء" Width="120" TextBlock.TextAlignment="Center" DisplayMemberBinding="{Binding StartDate, Converter={StaticResource DateConverter}}" FrameworkElement.FlowDirection="RightToLeft"/>
<GridViewColumn Header="تاريخ الأنتهاء" Width="120" TextBlock.TextAlignment="Center" DisplayMemberBinding="{Binding EndDate, Converter={StaticResource DateConverter}}" FrameworkElement.FlowDirection="RightToLeft" />
<GridViewColumn Header="نوع الاجازة" Width="100" FrameworkElement.FlowDirection="RightToLeft">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LeaveType}" Foreground="Red" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.