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 .
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.
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";
// 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");