DataGridView date issue

hi all...

I have a DatagridView populated by a database. One of the colums holds datetime values, but it seems that the DataGrid stores those values as txt.

So, when i click the DGV header to order by the date column, it doens't consider the month (being dd-mm-yyyy), so it orders considering only the day value 'dd'.

How do I format the dgv column to read the dates properly so that i can order the values by date

thx in advance




Answer this question

DataGridView date issue

  • xclairex

    dsani's option didn't work, i it should be something else

    here's how i populate the datagrid

    cnn.Open();

    strSql = "SELECT * from clients";

    SqlCommand command = new SqlCommand(strSql, cnn);

    SqlDataReader DataReader;

    DataReader = command.ExecuteReader();

    if (DataReader.HasRows)

    {

    while (DataReader.Read())

    {

    schedulesDataGrid.Rows.Add(DataReader["name"].ToString(),DataReader["datetin"].ToString());

    }

    }

    DataReader.Close();

    cnn.Close();



  • Ntc

    oh! fool me! :)

    Thanks allot ahmedilyas



  • kennm

    Hi Pedro,

    Assuming that the datagridview is bound and assuming that the datetime value is stored in the database as datetime.

    Go to the datagrdiview properties, click on edit columns. Select the date column from the list. In the righthand panel for bound column properties, you will find default cell style. Click on this - you will get the cellstyle builder form. In that look for the format and click on it. Set the format custome type to (dd mm yyyy) or whatever format you need.

    trust this would solve your issue, if i hv understood it correctly.

    thanks.


  • Harry Pfleger

    you need to strongly type your dataset/grid column - this could be the reason why as everything uses the base.object as the datatype but you want the datetime datatype.

    one way of doing this would maybe be to GetDateTime() using the DataReader, the other would be to fill the dataset using a SqlDataAdapter then binding the dataset to the datagrid

    currently you are converting it ToString() which will not work with what you are trying to do...



  • conrarn

    Thread moved to the appropriate forum.

    I'm unsure why it does that as if it has been populated using a database (with the aid of say a dataset and the binding has been set) then it should automatically create the datatypes.

    Any chance of giving us the code on how you are populating the data from the database into the dgv



  • Samer Selo

    no worries, glad I could help

  • DataGridView date issue