Convert DataGrid cell content

I have a DataGrid whose ItemsSource is set to a DataTable. The DataTable has a column of type DateTime, and I would like to display informational text (ie. "N/A") if the date in a particular cell is a certain value.

My first thought was to somehow bind the cell content to itself, and use a converter, but I can't seem to get it working correctly, and it seems as though there should be a better way.

Additionally, both the DataGrid and DataTable are dynamically generated, so this has to be done in the code behind.

Here's the code I tried initially:

// Create a new DataGridCellStyle
Style myStyle = new Style();
myStyle.TargetType = typeof(DataGridCell);

// Create the binding
Binding myBinding = new Binding();
myBinding.RelativeSource = RelativeSource.Self;
myBinding.Converter = new DateTimeToStringConverter();

// Add the Content setter
Setter mySetter = new Setter();
mySetter.Property = ContentProperty;  
mySetter.Value = myBinding;
myStyle.Setters.Add(setter);

// Set the Style and ItemsSource
myDataGrid.CellStyle = myStyle ;
myDataGrid.ItemsSource = myDataTable.DefaultView;

DateTimeToStringConverter does implement IValueConverter, but I'm guessing the problem lies somewhere with the binding, since DateTimeToStringConverter is never actually called when the DataGrid is displayed.

Answers


At first, you Add the variable with the name setter to the Setters collection, but you are define the variable with the name mySetter. It may be a reason, why your Converter is not actually called.

Also the solution for your problem will be a bit more complicated. Actually the Convert get a value of type RowDataView which contains a data for whole row. There is not an information in the converter about a Column or a Cell that is actually binded.

Better will be skip AutoGenerateColumns and generate them programmatically. Here is example:

myDataGrid.ItemsSource = myDataTable.DefaultView;
myDataGrid.AutoGenerateColumns = false;

foreach (DataColumn column in myDataTable.Columns)
{
    Binding binding = new Binding(column.ColumnName)
        {
            Converter = new DateTimeToStringConverter()
        };
    DataGridColumn gridColumn = new DataGridTextColumn 
        { 
            SortMemberPath = column.ColumnName, 
            Header = column.ColumnName, 
            Binding = binding 
        };
    myDataGrid.Columns.Add(gridColumn);
}

Of course, for a performance will be better use the converter only in a DateTime columns.


Need Your Help

I have a list with over a million objects in it, trying to find the fastest way to search through it

c# dictionary hash hashset ilist

I have a list that stores well over a million objects within it. I need to look through the list and update the objects found through the below query as efficiently as possible.

how to add image with text in qlistwidget pyqt4 python?

python pyqt4 qlistwidget

How to add image/icon with text in a qlistwidget in pyqt4 python? I want to add an icon with text just like a chat system. thanks

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.