Solved

How to fomat data in a DataGrid?

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

809 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