This is a migrated thread and some comments may be shown as answers.

Sorting on a DateTime Column

3 Answers 557 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeremy Murtishaw
Top achievements
Rank 1
Jeremy Murtishaw asked on 05 Aug 2009, 09:31 PM
Hello,

I'm having trouble with sorting on my gridview column. I've specified the DataType as datetime:

grdPackages.Columns[(int)ServerPackage.Column.Timestamp].DataType = typeof(DateTime);

However, when clicked on, it still does a Text-based sort. Is there anything else I need to do? If I were to change the column to a DateTime column, would this help? How would I go about doing this? It's a databound gridview populated from a list of custom data types.

Also, I'm also getting 'NullReferenceException' errors in my gridview formatting function on the line above, when it's called for a second time. So, the first time the grid is populated, the formatting function is called, and the column is set to typeof(DateTime). After this point, the user clicks elsewhere, causing the gridview's datasource to be set to null. Then, if they return to the same spot, the data is loaded and the formatting function is called again. This time, it throws the nullreferenceexception on that line. I don't understand why this would happen. I've verified that it happens even when the data source is the same...

Thanks!
Jeremy


3 Answers, 1 is accepted

Sort by
0
Aaron
Top achievements
Rank 2
answered on 05 Aug 2009, 10:31 PM
I assume you are using GridViewTextBoxColumns to display the date information? Also, I am assuming your Date data is probably being returned as a string from your datasource? This probably what is causing the text based sort. You should probably be using GridViewDateTimeColumn to display your DateTime data instead.

I think there are a few options you could look at in this situation. If you can somehow modify your data source to return a DateTime object instead of a string you could display your data more easily inside of a GridViewDateTimeColumn. You could also create some sort of unbound column in the gridview in which you programmatically display the date string converted to a DateTime.

You could also perform a custom sort on the column displaying the Date information. Here is some code that does just that.

        private void radGridView1_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
        {
            if (e.Column.FieldName == "Date")
            {
                DateTime d1 = DateTime.Parse(e.CellValue1.ToString());
                DateTime d2 = DateTime.Parse(e.CellValue2.ToString());
                if (e.GridViewTemplate.Sort == "[Date] ASC")
                    e.SortResult = d1.CompareTo(d2);
                else
                    e.SortResult = d2.CompareTo(d1);
            }
        }

- Aaron
0
Jeremy Murtishaw
Top achievements
Rank 1
answered on 06 Aug 2009, 12:43 AM
Hi Aaron,

I am not able to choose the type of these columns, as far as I know.

The way the gridview is populated is as follows:

grdObjects.DataSource=null
List <ServerObject> objObjectList = new List<ServerObject>(); 
sSuccess = Server.GetObjectsByType(sType, ""); 
    foreach (string str in Server.Objects) 
    { 
        string[] asData = str.Split('^'); 
ServerObject o = new ServerObject(GetObjectTypeName(asData[0], true), asData[1], Utilities.RestoreEscapedChars(asData[2]), asData[3], asData[4], asData[5], asData[6]); 
                            objObjectList.Add(o);
    } 
grdObjects.DataSource=objObjectList; 
//May format grid here 

I am not building it manually from columns and data. 

Colin
0
Jack
Telerik team
answered on 06 Aug 2009, 12:34 PM
Hello Jeremy Murtishaw,

You should disable AutoGenerateColumns property and add all desired columns manually, for example:

grvObjects.MasterGridViewTemplate.AutoGenerateColumns = false
grvObjects.Columns.Add(new GridViewDecimalColumn("ID")); 
grvObjects.Columns.Add(new GridViewDateTimeColumn("Date")); 
grvObjects.DataSource = objObjectList; 

I hope this helps. If you need further assistance, we will be glad to help.

All the best,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Jeremy Murtishaw
Top achievements
Rank 1
Answers by
Aaron
Top achievements
Rank 2
Jeremy Murtishaw
Top achievements
Rank 1
Jack
Telerik team
Share this question
or