Programmatically replace content of GridView cell with loading

This is my first time working on a program in WPF. I have a ListView in GridView displaying data from a bound dataset (which is being grabbed from the database).

In my database, "date of birth" is optional. So any entry without a dob mattered DateTime.MinValue

. In each of these dates, the date shows the minimum value in cell 01/01/0001. I'm trying to find a way to either format a cell so that it DateTime.MinValue

doesn't show, or replace each MinValue

with ""

.

My thought was to either use the "Loaded" event on the text block that the date is in and replace each instance of "01/01/0001", or loop through the dataset before submitting to the GridView and delete / replace it there ... I haven't even been able to figure out how to do this.

My xaml code for GridView:

<Grid>
    <ListView x:Name="resultsListView" GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler" Margin="0,54,0,28" ItemsSource="{Binding Path=Table}">
    <ListView.View>
    <GridView>
        <GridViewColumn DisplayMemberBinding="{Binding Path=LastName}"
            Header="Last Name" 
            Width="150"/>
        <GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}" 
            Header="First Name"
            Width="100"/>
        <GridViewColumn DisplayMemberBinding="{Binding Path=MiddleName}" 
            Header="Middle Name"
            Width="100"/>
        <GridViewColumn Header="Date of Birth" Width="100">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock TextAlignment="Justify" Text="{Binding Path=DateOfBirth, StringFormat='{}{0:MM/dd/yyyy}'}" Loaded="TextBlock_Loaded" />
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
    </ListView.View>
    </ListView>
</Grid>

      

Code for DataSet:

private void FillListView(DataSet ds)
{
    if (resultsListView.Items.Count != 0)
    {
        resultsListView.Items.Clear();
    }
    resultsListView.DataContext = ds.Tables[0].DefaultView;
}

      

Any advice on how to show spaces for DateTime.MinValue

in my GridView would be greatly appreciated!

+2


source to share


1 answer


I would do IValueConverter

that deals with this and include it in your binding expression.

In your resources:

<local:DateTimeConverter x:Key="DateTimeConverter" />

      

Then update the binding:



<TextBlock Text="{Binding Path=DateOfBirth, 
                          Converter={StaticResource DateTimeConverter},
                          ConverterParameter='MM/dd/yyyy'}" />

      

Then define the class:

public class DateTimeConverter : IValueConverter

      

These are two methods. You only need to implement Convert

(unless you plan on using two way binding). In this method, you can take the format string via paramater (as I passed in the binding expression above) and also check for DateTime.MinValue

and return an empty string.

+2


source







All Articles