Solved

How to fomat data in a DataGrid?

Posted on 2007-04-03
6
715 Views
Last Modified: 2013-12-25
I'm using the DataGrid for the first time in an Excel VBA application.

I've managed to get my grid to display data from an Access table, via the ADO datasource control, but all my numeric data is left aligned, without commas, and negative numbers showing in black instead of red. How do I apply formatting?

0
Comment
Question by:TimHudspith
  • 2
6 Comments
 
LVL 4

Expert Comment

by:jtaylor8181
ID: 18850805
If you are using VS2005, I think this would work in VS2003 as well you can do this:

'For Alignment
DataGrid1.Columns("column1").DefaultCellStyle.Alignment = Forms.DataGridViewContentAlignment.MiddleRight

'For Color
DataGrid1.Columns("column1").DefaultCellStyle.ForeColor = Color.Red
0
 
LVL 4

Expert Comment

by:jtaylor8181
ID: 18850933
As far as formatting the number itself, what you can do is create a datatable and dataview and then set the datasource for the datatable to be the Access table (the dataview will also have the same datasource as long as you set the dataview to be linked to the datatable).  From there you can set the dataview to be the datasource for the datagrid and then just do this:

Private m_dv as Dataview
'Set up the grid, you don't necessarily have to follow this
Private Sub SetUpGrid()
   With Me.Datagrid1
     .AutoGenerateColumns = True
     .DataSource = Me.m_dv    
     .Columns("column1").DisplayIndex = 0
     . Columns("column1").DefaultCellStyle.Alignment =  Forms.DataGridViewContentAlignment.MiddleRight
    End With

    For i As Integer = 0 To Me.m_dv.Count - 1
            Me.m_dv(i).Item("column1") = Format(Me.m_dv(i).Item("description"), "#,###,###.00")
    Next
End Sub

'You will have to use this event
Private Sub Datagrid1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles Datagrid1.CellFormatting
        If Me.Datagird1.Columns(0).Name = "column1" Then
            If e.Value < "0" Then
                e.CellStyle.ForeColor = Color.Red
            End If
        End If
End Sub
0
 

Author Comment

by:TimHudspith
ID: 18862916
Sorry, I think we might be talking about different objects. I'm using the Microsoft DataGrid Control version 6.0. I don't know anything about a datatable or dataview.

I can't see a '.DefaultCellStyle' property for the column object, but it does have something called '.DataFormat'. The object browser describes this as a formatting object for values in this column, but when I click on the link for the enueration of its constants I get the message: "Cannot jumpt to 'IDataFormatDisp' because it is hidden. Presumably its library is missing, but I can't see anything resembling 'IDataFormatDisp' in the list of References.

Any ideas?
0
 
LVL 17

Accepted Solution

by:
Shanmuga Sundaram earned 500 total points
ID: 21084408
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA: Add rows to listbox based on criteria 7 79
DIR issue 7 51
How to create a duplicate finder Application 9 115
VBA/SQL - Connect to SQL server and pull data 4 89
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

862 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now