Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi,
I am having a datagirdiew with one date column. in which when i sort the date column to ascending order, the date format is becoming like below:
27/01/2015
27/01/2016
27/01/2016
27/01/2017
27/01/2018
27/01/2018
27/01/2018
27/02/2018
27/02/2018
27/03/2016
27/04/2015
27/04/2016

see last 3 dates, here it was sorting with date and month but not year.
i want it as
27/01/2015
27/04/2015
27/01/2016
27/01/2016
27/03/2016
27/04/2016
27/01/2017
27/01/2018
27/01/2018
27/01/2018
27/02/2018
27/02/2018


What I have tried:

if (Rb_Date.Checked == true)
            {
                dgv_summary.Columns["Date"].ValueType = typeof(DateTime);
                this.dgv_summary.Sort(this.dgv_summary.Columns["Date"], ListSortDirection.Ascending);
            }


Can any one suggest a code to sort the datagridview column date with DATE/MONTH/YEAR..

Thanks in Advance.
Posted
Updated 29-Jan-18 19:28pm

try like this

private void Form1_Load(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("Date" );
           dt.Columns.Add("DateObjectHide",typeof(DateTime));
           dt.Rows.Add("27/01/2015");
           dt.Rows.Add("27/01/2016");
           dt.Rows.Add("27/01/2016");
           dt.Rows.Add("27/01/2017");
           dt.Rows.Add("27/01/2018");
           dt.Rows.Add("27/01/2018");
           dt.Rows.Add("27/01/2018");
           dt.Rows.Add("27/02/2018");
           dt.Rows.Add("27/02/2018");
           dt.Rows.Add("27/03/2016");
           dt.Rows.Add("27/04/2015");
           dt.Rows.Add("27/04/2016");
           dataGridView1.DataSource = dt;
           dt.AsEnumerable().ToList().ForEach(k => k["DateObjectHide"] = DateTime.ParseExact(k["Date"].ToString(), "dd/MM/yyyy", CultureInfo.CurrentCulture));
           dataGridView1.Columns["DateObjectHide"].Visible = false;

       }



       bool flag = false;
       private void buttonSort_Click(object sender, EventArgs e)
       {
           if(flag)
               this.dataGridView1.Sort(this.dataGridView1.Columns["DateObjectHide"], ListSortDirection.Ascending);
           else
               this.dataGridView1.Sort(this.dataGridView1.Columns["DateObjectHide"], ListSortDirection.Descending);
           flag = !flag;

       }
 
Share this answer
 
Comments
chaitanya556 30-Jan-18 21:42pm    
Hi, It Didn't worked. actually im binding the data from the database.
Karthik_Mahalingam 30-Jan-18 21:50pm    
however from database you will be getting datatable rite?
so you can apply the above logic.
chaitanya556 30-Jan-18 21:55pm    
actually im binding like this:
Hide   Copy Code
string sqlex="select * from Table";dap=new SqlDataAdapter(sqlex,connectionString);table=new DataTable();table.Columns.Add(new DataColumn(" ",typeof(bool)));dap.fill(table);datagridview1.DataSource=table;
Karthik_Mahalingam 30-Jan-18 22:03pm    
you should add the column after binding the data to table.
after "fill" method.
Karthik_Mahalingam 30-Jan-18 22:14pm    
string sqlex="select * from Table";
dap=new SqlDataAdapter(sqlex,connectionString);
table=new DataTable();
table.Columns.Add(new DataColumn(" ",typeof(bool)));
dap.fill(table);
 table.Columns.Add("DateObjectHide",typeof(DateTime));
datagridview1.DataSource=table;
  table.AsEnumerable().ToList().ForEach(k => k["DateObjectHide"] = DateTime.ParseExact(k["Date"].ToString(), "dd/MM/yyyy", CultureInfo.CurrentCulture));
           dataGridView1.Columns["DateObjectHide"].Visible = false;
dgv_summary.Columns["Date"].DefaultCellStyle.Format = "dd/MM/yyyy";

Try this code block before your sorting line of code.
 
Share this answer
 
Comments
[no name] 31-Jan-18 2:37am    
Let me know the reason to down vote?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900