Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 13th, 2006, 09:51 AM
Authorized User
 
Join Date: Nov 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Column Sorting/Formatting in MS Datagrid!

I have quite a nasty issue. As trivial as it may look it is so much more frustrating now. In VB6 or earlier versions, or using 3rd party tools would be ‘walk thru the park’. Never the less now it’s not fun anymore.

I’m getting data from MS-SQL Server and everything is fine as long as date field is loaded as a datetime().

Never the less display of it is rather ambiguous from user standpoint (i.e. 01/02/2006 could be taken as Feb-01 or Jan-02).

Now if I convert() output datetime() field to string (varchar(), char()) I get exactly what users want to see (in any format they want) but then sorting of the grid becomes meaningless (it is sorted as a string). This is an important feature for the users, exactly on that column.

This is windows based application, delivered in C# v1.1 (VB.NET responses would be perfectly fine too).

I’m already using MS datagrid’s TableStyles property to set fields individual GridColumnStyles[ ].Width().

I tried DataGridTextBoxColumn(),PropertyDescriptor() and other relevant aspects but somehow it didn’t have any bearings on the result. I guess I’m missing some important bit there.

All sample codes I found do address mainly ASP.NET approach or using code generated data then adding column to the grid.

My issue is to format a column as a date in order to sort properly. I see two possible approaches:

- Getting data in as datetime() then format column to meaningful display
- Getting data in as string (varchar,char) then format column to Date format for sorting reason.

Any response would be greatly appreciated. Alternative approach is great too in case if I missed something.


Thanks


 
Old July 13th, 2006, 02:32 PM
Authorized User
 
Join Date: Nov 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Done! Solved!

For everybody else’s info look at the MSDN sample below if order to get around this issue.

 private void MyAddCustomDataTableStyle()
   {
      // Get the currency manager for 'myDataSet'.
      CurrencyManager myCurrencyManger =
               (CurrencyManager)this.BindingContext[myDataSet];

      DataGridTableStyle myTableStyle = new DataGridTableStyle();
      myTableStyle.MappingName = "Customers";

      PropertyDescriptor proprtyDescriptorName =
               myCurrencyManger.GetItemProperties()["CustName"];

      DataGridColumnStyle myCustomerNameStyle =
               new DataGridTextBoxColumn(proprtyDescriptorName);

      myCustomerNameStyle.MappingName = "custName";
      myCustomerNameStyle.HeaderText = "Customer Name";
      myTableStyle.GridColumnStyles.Add(myCustomerNameSt yle);

      // Add style for 'Date' column.
      PropertyDescriptor myDateDescriptor =
               myCurrencyManger.GetItemProperties()["Date"];
      // 'G' is for MM/dd/yyyy HH:mm:ss date format.
      DataGridColumnStyle myDateStyle =
               new DataGridTextBoxColumn(myDateDescriptor,"G");

      myDateStyle.MappingName = "Date";
      myDateStyle.HeaderText = "Date";
      myDateStyle.Width = 150;
      myTableStyle.GridColumnStyles.Add(myDateStyle);

      // Add DataGridTableStyle instances to GridTableStylesCollection.
      myDataGrid.TableStyles.Add(myTableStyle);
   }







Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a column yogeshyl Excel VBA 1 December 10th, 2007 07:09 PM
Formatting datagrid column dates badgolfer VS.NET 2002/2003 12 August 9th, 2006 08:24 AM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM
Data Formatting in DataGrid Template Column djmarquette ADO.NET 3 April 14th, 2004 07:25 AM
sorting a column alphabetically Adam H-W SQL Server 2000 8 November 11th, 2003 09:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.