Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DataGrid Problem

Posted on 2009-04-03
6
Medium Priority
?
229 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 750 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 750 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 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