Solved

DataGrid Problem

Posted on 2009-04-03
6
220 Views
Last Modified: 2012-05-06
Hi,

I am using a OracleDataAdapter to populate a DataTable.

The DataTable is then linked to DataGridView as a datasource.

The DataGridView is automaticallly populated with data and i have no control over the format of certain columns...

Example: there are date columns where the date is returned from the query in the form YYYYMMDD.

I would like to display date values in the gird as DD/MM/YYYY or MM/DD/YYYY depending on the users local date settting format....

*I have no control over the date format is returned back from the database...

Thnks Ian




0
Comment
Question by:ISC
  • 3
  • 2
6 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 250 total points
ID: 24062248
You can set the DataGridViewCellStyle format for that column. You can either do it in the designer (see attached) or at run-time.
0
 
LVL 4

Assisted Solution

by:Avelan
Avelan earned 250 total points
ID: 24062251
Hey,

here's a bit of code that may be helpful, of course you'll need to change it a bit to make it work for you.
It should make dates display as you've set in the system settings.

-Avelan
    Public Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
 
    Public Declare Function GetLocaleInfo Lib "kernel32" _
       Alias "GetLocaleInfoA" _
      (ByVal Locale As Long, _
       ByVal LCType As Long, _
       ByVal lpLCData As String, _
       ByVal cchData As Long) As Long
 
    Public Const LOCALE_SSHORTDATE As Long = &H1F
 
    Private Sub Table_1DataGridView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles Table_1DataGridView.CellFormatting
        Dim str As String = ""
        If IsDate(e.Value) Then
            Call GetLocaleInfo(GetSystemDefaultLCID, LOCALE_SSHORTDATE, str, Len(str))
            e.CellStyle.Format = str
        End If
 
    End Sub

Open in new window

0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 24062256
Oops, didn't attach the first time.
DataGridViewColumns.jpg
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Author Comment

by:ISC
ID: 24095978
Hi,

The following line does not return any format...

Call GetLocaleInfo(GetSystemDefaultLCID, LOCALE_SSHORTDATE, str, Len(str))

Ian
0
 
LVL 1

Author Comment

by:ISC
ID: 24096259
The code below just sets the contents of each cell to the format string which is not what I expected or want...

 Private Sub dgv_accrual_periods_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgv_accrual_periods.CellFormatting

        If (e.ColumnIndex = 1) Or (e.ColumnIndex = 2) Or (e.ColumnIndex = 3) Then

            Dim l_objDateFormat As System.Globalization.DateTimeFormatInfo
            l_objDateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat
            e.CellStyle.Format = l_objDateFormat.ShortDatePattern.ToString

        End If

    End Sub
0
 
LVL 1

Author Comment

by:ISC
ID: 24096684
I also tried...

  Private Sub dgv_accrual_periods_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgv_accrual_periods.CellFormatting

        If (e.ColumnIndex = 1) Or (e.ColumnIndex = 2) Or (e.ColumnIndex = 3) Then

            Dim l_objDateFormat As System.Globalization.DateTimeFormatInfo
            l_objDateFormat = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat
            e.Value = String.Format(l_objDateFormat.ShortDatePattern.ToString, e.Value.ToString)

        End If

    End Sub
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question