Our xamwebgrid is set up to autogenerate columns as it binds dynamically to multiple datasets. When it generates columns of DateTime type it creates a DateColumn with the DatePicker. Our data is read only so we don't need the datepicker and we need the date/times to show in this format "12/31/2009 13:35:00"
If we parse the dates to string, it messes up our date sort orders as well as the formatting we apply to current records. For instance 01/01/2010 will sort before 01/07/2010.
Is there a way to remove the DatePicker from the DateColumns when the datecolumns are autogenerated?
The XamWebGrid has a collection property off it called ColumnTypeMappings. The objects in this collection define what type of column to use based on the DataType of the underlying data. As of 9.2 a DateTime or DateTime? data field would be mapped to the Date column.
There are two ways you could accomplish what you would like to do.
The first would be to remove the entries in the ColumnTypeMappings table which control what column would be used.
int count = this.grid.ColumnTypeMappings.Count;
for (int i = count - 1; i >= 0; i--)
{
if (this.grid.ColumnTypeMappings[i].DataType == typeof(DateTime))
this.grid.ColumnTypeMappings.RemoveAt(i);
}
else if (this.grid.ColumnTypeMappings[i].DataType == typeof(DateTime?))
You could instead just modify the mappings as well:
this.grid.ColumnTypeMappings[i].ColumnType = typeof(TextColumn);
Either should work. I would do this in the Load of the page.
I tried implementing the same but it didn't work for me. I am binding my grid at runtime.
if I do it on page load then further code would throw error as it disturbs the underlying datatype bindings.
Can someone help ?
even if I use the GRID.ColumnMappingTypes.CLEAR() method, the column is still shows datepicker
Hi,
You don't actually even need to clear, you can just set the ColumnType mapping in xaml:
Note: in 10.2 we changed our namespaces:
10.1
<ig:XamGrid.ColumnTypeMappings>
<ig:ColumnTypeMapping ColumnType="Infragistics.Silverlight.Controls.TextColumn" DataType="System.DateTime"/>
</ig:XamGrid.ColumnTypeMappings>
10.2
<ig:ColumnTypeMapping ColumnType="Infragistics.Controls.Grids.TextColumn" DataType="System.DateTime"/>
If you're doing this in code, make sure that you do this BEFORE you set your ItemSource, otherwise it'll be too late.
Note: again you don't have to clear, you can just set it, last one wins:
this.xamGrid.ColumnTypeMappings.Add(new ColumnTypeMapping() { ColumnType = typeof(TextColumn), DataType = typeof(DateTime) });
Hope this helps.
-SteveZ
Here is a catch.
I am binding my grid at runtime i.e. there are no predefined column(s) definition for the grid. In such a scenario, the datatype for each column would be allocated at runtime AFTER the ItemSource for the grid has been set.
How do I achieve the desired result in this case ?
Its the same as what i just stated.
Setting the ColumnType mappings, will solve your issue, as they determine what kind of column will be generated for a specific DataType.
Hi All,
we are having similar issue with WPF XamGrid. When we define ColumnTypeMappings from C# backcode of the Page it doesn't work(defined in constructor right after InitializeComponent()).
foreach (var columnTypeMapping in PositionsDataGrid.ColumnTypeMappings)
if (columnTypeMapping.DataType == typeof(DateTime))
columnTypeMapping.ColumnType = typeof(TextColumn);
if (columnTypeMapping.DataType == typeof(DateTime?))
...
It works when we add to XAML like bellow:
<ig:ColumnTypeMapping ColumnType="igGrid:TextColumn" DataType="system:DateTime"/>
<ig:ColumnTypeMapping ColumnType="igGrid:TextColumn" DataType="system:Boolean"/>
We are binding XamGrid ItemsSource to ObservableCollection<PresentationItem>, PresentationItem has a lot of properties of a class that are nullable.
How we can define DataType attribute to be "DateTime?"(nullable DateTime) in XAML? How to define nullable DataTypes in XAML?
We are using Infragistics 16.1 version.