Infragistics UltraGrid,我想要一个字符串值的格式化字符串

Infragistics UltraGrid, I want a formated string from a string value

我正在使用 Infragisitics 17.1 UltraGrid。

网格有 2 列。

我想要第二列的格式化字符串,如下所示。

'20170102123456'=>'2017-01-02 12:34:56"

第二列的数据类型是'string'而不是'date'。

这个网格会有大量的数据,所以任何转换都让我担心。

但欢迎任何广告。

DataSoure 如下所示。

    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);
    }

然后我像下面一样初始化网格,

    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";
    }

第一列工作正常,但第二列没有。

如何像下面那样显示第二列?

'20170102123456'=>'2017-01-02 12:34:56"

UltraGrid 无法单独进行此对话。在这种特定情况下,您可以做的是实现您自己的自定义 IEditorDataFilter。为此,请像这样更改您的 InitializeLayuot:

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();
}

然后像这样创建您的自定义 DataFilter:

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;
    }
}