Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
I forgot my password
Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 13th, 2006, 09:51 AM
Authorized User
 
Join Date: Nov 2005
Location: , , .
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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 13th, 2006, 02:32 PM
Authorized User
 
Join Date: Nov 2005
Location: , , .
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);
   }


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a column yogeshyl Excel VBA 1 December 10th, 2007 06: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 08:29 AM



All times are GMT -4. The time now is 07:57 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
© 2011 John Wiley & Sons, Inc.