Results 1 to 4 of 4

Thread: [RESOLVED] Format datagrid cell that contains a date.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    Resolved [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);
    
            }

  2. #2
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    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.
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2005
    Location
    Wisconsin
    Posts
    788

    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.

  4. #4
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972

    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'
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width