DateTime Column

3 posts, 0 answers
  1. Andrés
    Andrés avatar
    6 posts
    Member since:
    Nov 2012

    Posted 07 Nov 2012 Link to this post

    Hi, I'm sorry for my english;

    I use a string(AAAAMMDD :20121107) to save a Date (in my DataBase), So I do not know what type use to display my date.

    I tried to use textColumn and in the RowFormatting event  I transform (using my own object) my string like i want (DD/MM/AAAA :7/11/2012), but I need sort the column and I use the CustomSorting event:
    private void radGridView1_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
    {
        MyObjDate row1Freight = new MyObjDate(e.Row1.Cells["colDate"].Value.ToString());
        MyObjDate row2Freight =newMyObjDate(e.Row2.Cells["colDate"].Value.ToString());
        if (row1Freight > row2Freight)
        {
            e.SortResult = 1;
        }
        else if (row1Freight < row2Freight)
        {
            e.SortResult = -1;
        }
        else
        {
            e.SortResult = 0;
        }
    }

    And It does not work, also no sort by other columns.

    The other method of load is transform my string in a DateTime for use DateTime Column, but I don't know where i have to make the transform or if I can tell what will be the column data format to receive would be perfect.

    I tried to convert the string to DateTime in RowFormatting event, but does not show well the data and does not order correctly.

    What would be the correct way to load the date data type using me? thanks
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 08 Nov 2012 Link to this post

    Hello Andres,

    q "I use a string(AAAAMMDD :20121107) to save a Date (in my DataBase), So I do not know what type use to display my date."

    I don't really understand what you mean by this... you should convert the object back to a datetime object after retrieving it from the db and then everything will work fine.

    If this is not the case or you cannot do this by any reason, please create a very small sample and post it here and i will be more than happy to help.

    Best Regards,
    Emanuel Varga
    Winforms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Anton
    Admin
    Anton avatar
    167 posts

    Posted 12 Nov 2012 Link to this post

    Hello Andrés,

    Thank you for writing.

    If I understand correctly, your dates from the database are strings in format "yyyyMMdd" and you want to use them in a DateTime Column with Column Sorting support. If this is so, here is how you can do that:
    private void Form1_Load(object sender, EventArgs e)
    {
         
        DataTable inputData = new DataTable();
        DataColumn columnInt = new DataColumn("Int", typeof(int));
        DataColumn columnString = new DataColumn("Date", typeof(string));
     
        inputData.Columns.Add(columnInt);
        inputData.Columns.Add(columnString);
     
        inputData.Rows.Add(5, "20121107");
        inputData.Rows.Add(6, "20121109");
        inputData.Rows.Add(7, "20121107");
        inputData.Rows.Add(8, "20121108");
     
        this.radGridView1.DataSource = ConvertData(inputData);
        (this.radGridView1.Columns[1] as GridViewDateTimeColumn).FormatString = "{0:dd/MM/yyy}";
     
        this.radGridView1.Columns[1].Width = 100;
    }
    private DataTable ConvertData(DataTable inputData)
    {
        DataTable convertedData = new DataTable();
        DataColumn columnInt = new DataColumn("Int", typeof(int));
        DataColumn columnData = new DataColumn("Date", typeof(DateTime));
     
        convertedData.Columns.Add(columnInt);
        convertedData.Columns.Add(columnData);
     
        CultureInfo provider = CultureInfo.InvariantCulture;
        string format = "yyyyMMdd";
        foreach (DataRow item in inputData.Rows)
        {
            convertedData.Rows.Add(item[0], DateTime.ParseExact(item[1].ToString(), format, provider));
        }
     
        return convertedData;
    }

    If you use a date-time column, there is no need to use the CustomSorting event to support column sorting. If this is not your case, I would kindly ask you to open a new support ticket and provide a sample project that demonstrates your scenario.

    Attached is a sample project that contains the code above. I hope this helps.

    Greetings,
    Anton
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Back to Top