|
-
Jan 5th, 2012, 05:10 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Format datagrid cell that contains a date.
I would like to take a cell in a datagrid that will display a date and format it as such.
Currently is : 2011/12/31
Would like it to be: 12/31/11
I have this code which is formatting my headers correctly(thanks to help I received in this forum). Is there something I can do with this code to format the actual cells?
Code:
private void SetUpHoldTableStyles()
{
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = binding2.DataMember.ToString();
DataGridColumnStyle recordid = new DataGridTextBoxColumn();
recordid.MappingName = "RecordID"; //column 0
recordid.HeaderText = "#";
recordid.Width = -1;
ts.GridColumnStyles.Add(recordid);
DataGridColumnStyle customername = new DataGridTextBoxColumn();
customername.MappingName = "CustomerName";
customername.HeaderText = "Name";
customername.Width = 155;
ts.GridColumnStyles.Add(customername);
DataGridColumnStyle checkdate = new DataGridTextBoxColumn();
checkdate.MappingName = "CheckDate";
checkdate.HeaderText = "Date";
checkdate.Width = 70;
ts.GridColumnStyles.Add(checkdate);
DataGridColumnStyle paymenttype = new DataGridTextBoxColumn();
paymenttype.MappingName = "PaymentType";
paymenttype.HeaderText = "#";
paymenttype.Width = -1;
ts.GridColumnStyles.Add(paymenttype);
dgHolds.TableStyles.Clear();
dgHolds.TableStyles.Add(ts);
}
-
Jan 6th, 2012, 08:34 AM
#2
Fanatic Member
Re: Format datagrid cell that contains a date.
if you're using databinding, you can format the sql query, at least that's how i use to do it.
Check out about sql compact convert/cast here. Check also DataTypes and Standard and Extended Date Formats
You're looking for: SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY]
Last edited by TDQWERTY; Jan 6th, 2012 at 08:39 AM.
-
Jan 6th, 2012, 10:09 AM
#3
Thread Starter
Fanatic Member
Re: Format datagrid cell that contains a date.
Microsoft Access is the database that is being used. I found this to work for me.
SELECT Format(CheckDate,'mm/dd/yy') as TranDate FROM SomeTable;
Then I changed the section referencing the data this way
Code:
DataGridColumnStyle checkdate = new DataGridTextBoxColumn();
// checkdate.MappingName = "CheckDate";
checkdate.MappingName = "TranDate";
checkdate.HeaderText = "Date";
checkdate.Width = 70;
ts.GridColumnStyles.Add(checkdate);
Thanks again for the links.
-
Jan 6th, 2012, 10:53 AM
#4
Fanatic Member
Re: [RESOLVED] Format datagrid cell that contains a date.
you're welcome but you might want to check if 'mm/dd/yy' won't return 'minute/day/yeay' instead of month/day/year
If i'm not wrong SQL needs 'MM/dd/yy'
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|