Column.Format in bound mode only works with correct data type

Posted by: tschoep on 11 November 2019, 11:16 pm EST

    • Post Options:
    • Link

    Posted 11 November 2019, 11:16 pm EST - Updated 3 October 2022, 3:31 pm EST

    I have to write a software, where the user can define formats for user input fields. In the database this fields are stored as strings because I only know the datatype of the fields at runtime. Therfore I’m trying to set the datatype and the format of a column in the grid at runtime like this:

    c1FlexGrid1.Cols["Date1"].DataType = typeof(DateTime);
    c1FlexGrid1.Cols["Date1"].Format = "D";
    

    The grid is in bound mode. But the format only works if the bound field is of type DateTime and doesn’t work if the bound field is of type string:

    public DateTime Date1 { get; set; }
    public string Date2 { get; set; }
    

    How can I tell the grid to format the bound string field also in the correct date format?

    Attached you can find a screenshot of the two fields (DateTime and string) in the grid and a small test project written in C#.

    Thanks for you help

    Markus

    ColumnFormatTest.zip

  • Posted 12 November 2019, 7:40 pm EST

    Hi Markus,

    applying the format is not possible - if the cell contains string data, this will not be converted to DateTime values, and thus the format is not applied.

    But there is a workaround: use the “OwnerDrawCell” event and parse the string date as DateTime, then format this value. See attached reworked sample.

    Note the you also have to set the “DrawMode” to “OwnerDraw”.

    ColumnFormatTest_OwnerDraw.zip

    Best regards

    Wolfgang

  • Posted 12 November 2019, 9:05 pm EST

    Hello,

    @Wolfgang, thank you very much for the sample and the explanation.

    @Markus, if you need any other help, please let us know.

    Regards,

    Prabhat Sharma.

  • Posted 12 November 2019, 11:38 pm EST

    Hi there,

    @Wolfgang, thanks for your quick reply and your help. And thanks a lot taking time to extend the sample for me! :slight_smile:

    @Prabhat, I already thought about using “OwnerDrawCell”, but I was shure there should be a better way, like telling bound mode that some fields should be converted into another type. I couldn’t understand why I can separately set the “DataType” of a col independent of the value of the bound field and this will have effect, while “Format” is ignored.

    Thanks a lot and best regards

    Markus

  • Posted 13 November 2019, 4:22 pm EST

    Hello Markus,

    When the grid is in bound mode setting the DataSource property of the column makes no sense because it shows the data as per the field in the DataSource and whenever any changes occurs, DataSource refreshes itself that’s why it is treating the second column values as String only.

    One thing you can do is if you are getting data from any database to DataTable and assigning that DataTable to the FlexGrid’s DataSource.

    And you don’t want to change the field DataType in the database, then you can use the code snippet given below by Cloning the DataTable :

    DataTable dtCloned = dt.Clone();
    dtCloned.Columns[1].DataType = typeof(DateTime);
    foreach (DataRow row in dt.Rows)
     {
      dtCloned.ImportRow(row);
     }
    c1FlexGrid1.DataSource = dtCloned;
    

    Please go through the attached sample demonstrating the same.

    Regards,

    Prabhat Sharma.

    ColumnFormatTest1.zip

  • Posted 13 November 2019, 9:30 pm EST

    Hi there,

    @Prabhat: Thanks for your help. I will try this out.

    So I’ll be able to workaround this problem in anyway.

    Thanks a lot to both of you

    Markus

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels