Solved

DataGrid Problem

Posted on 2009-04-03
6
223 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
Technology Partners: 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!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
write xml in vb.net 2 41
Visual Studio hangs on running project 6 57
vb.net background worker crossthread 11 28
Create a button class for use in all vb.net apps 5 13
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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