2011-03-21 45 views
2

我有一些列的gridview,我需要按日期列排序gridview。但是我没有正确地分类。这是我使用的代码:如何对DataView中的DateTime列进行排序?

dt.DefaultView.Sort = "Meldingsdatum asc"; 
gvOutlookMeldingen.DataSource = dt; 
gvOutlookMeldingen.DataBind(); 

有人可以帮助我,请。

回答

3
+1

不是一个真正的答案,但它的链接。发布答案,以便我可以接受 – SamekaTV 2011-03-21 08:48:06

+0

作为openshac的建议,答案是:“检查DataColumn的DataType属性并确保它设置为DateTime。” – DOK 2013-05-23 14:32:09

1

是你DateTime类型的列,如果没有的话,它可能必须这样做。在填充表格之前,在开始时正确完成此操作。或者,您可以创建类型为DateTime的第二列,然后使用该列,但这有点凌乱:-)

+0

我必须首先将该列的数据类型设置为DateTime。然后我可以对它进行分类 – SamekaTV 2011-03-21 09:39:44

2

DataView将日期或任何内容存储为字符串类型。因此,当你按字符串排序时。要将其排序为DateTime,您需要在添加任何数据之前将其转换为DateTime,如下所示:

dt.Columns [“Date”] .DataType = Type.GetType(“System.DateTime”);

0

我的代码片段希望显示的需要来设置列的typeof(DateTime的),才能正确地排序它

 EntityCollection result = serviceProxy.RetrieveMultiple(querybyattribute); 

     DataTable dt = new DataTable(); 
     dt.Columns.Add("Title"); 
     dt.Columns.Add("Created On", typeof(DateTime)); 

     foreach (Entity entity in result.Entities) 
     { 
      DataRow dr = dt.NewRow(); 

      dr["Title"] = entity.Attributes["title"].ToString(); 
      dr["Created On"] = entity.Attributes["createdon"]; 

      dt.Rows.Add(dr); 
     } 

     DataView dv = dt.DefaultView; 
     dv.Sort = "Created On desc"; 
     DataTable sortedDT = dv.ToTable(); 

     dataGridView1.DataSource = sortedDT; 
相关问题