Solved

How to fomat data in a DataGrid?

Posted on 2007-04-03
6
723 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

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.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

739 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