Solved

How to fomat data in a DataGrid?

Posted on 2007-04-03
6
714 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

706 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

18 Experts available now in Live!

Get 1:1 Help Now