Solved

How to fomat data in a DataGrid?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

735 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