我有一个包含5列的DataTable dtLeaves
,其中2列是DateTime StartDate, EndDate
列,它们可能具有minDate值01/01/1900
,而通过ListView LeavesListView
显示的DataTable是有没有ListView将日期01/01/1900
显示为空字段,而不是实际值?
从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;
}
}
答案 0 :(得分:1)
您可以使用转换器:
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>