I'm using Infragisitics 17.1 UltraGrid.
The Grid has 2 columns.
I want the formatted string of the second column, just like below.
'20170102123456' => '2017-01-02 12:34:56"
The data type of the second column is 'string' not 'date'.
This grid will have huge data, so any conversion is worry to me.
But any adive is welcome.
DataSoure just like below.
private void SetTestData()
{
DataTable dtDataSource = new DataTable("table1");
dtDataSource.Columns.Add("OrderDate", typeof(DateTime));
dtDataSource.Columns.Add("RequiredDate", typeof(string));
ultraGrid1.DataSource = dtDataSource;
DataRow rowNew = dtDataSource.NewRow();
rowNew["OrderDate"] = DateTime.Now;
rowNew["RequiredDate"] = "20170101123456";
dtDataSource.Rows.Add(rowNew);
}
And I Initialize Grid Just like below,
private void UltraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
// Fit columns
e.Layout.AutoFitStyle = AutoFitStyle.ExtendLastColumn;
// Set date formats
e.Layout.Bands[0].Columns["OrderDate"].Format = "yyyy-MM-dd HH:mm:ss";
e.Layout.Bands[0].Columns["RequiredDate"].Format = "yyyy-MM-dd HH:mm:ss";
}
The first column works fine, but the second column does not.
How i can display the second column just like below?
'20170102123456' => '2017-01-02 12:34:56"
UltraGrid will not be able to do this conversation alone. What you can do in this specific case is implement your own custom IEditorDataFilter. To do so change your InitializeLayuot like this:
private void UltraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
{
// Fit columns
e.Layout.AutoFitStyle = AutoFitStyle.ExtendLastColumn;
// Set date formats
e.Layout.Bands[0].Columns["OrderDate"].Format = "yyyy-MM-dd HH:mm:ss";
// You do not need this as the column data type is string
//e.Layout.Bands[0].Columns["RequiredDate"].Format = "yyyy-MM-dd HH:mm:ss";
// Set the column's editor DataFilter instead
e.Layout.Bands[0].Columns["RequiredDate"].Editor.DataFilter = new DF();
}
Then create your custom DataFilter like this:
internal class DF : IEditorDataFilter
{
public object Convert(EditorDataFilterConvertArgs conversionArgs)
{
switch(conversionArgs.Direction)
{
case ConversionDirection.DisplayToEditor:
break;
case ConversionDirection.EditorToDisplay:
var valueAsString = conversionArgs.Value.ToString();
var year = int.Parse(valueAsString.Substring(0, 4));
var month = int.Parse(valueAsString.Substring(4, 2));
var day = int.Parse(valueAsString.Substring(6, 2));
var hours = int.Parse(valueAsString.Substring(8, 2));
var minutes = int.Parse(valueAsString.Substring(10, 2));
var result = new DateTime(year, month, day, hours, minutes, 0).ToString("yyyy-MM-dd HH:mm:ss");
conversionArgs.Handled = true;
conversionArgs.IsValid = true;
return result;
case ConversionDirection.OwnerToEditor:
break;
case ConversionDirection.EditorToOwner:
break;
default:
break;
}
return conversionArgs.Value;
}
}