Solved

DataGrid Problem

Posted on 2009-04-03
6
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

738 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