• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2724
  • Last Modified:

Dataformat with Datagrid

Can I set in a datagrid (6.0) the Dataformat for a specific columns via visual basic code ?

for example :

 DataGrid1.Columns(0).dataformat = xxxx

1 Solution
should be able to...it is read/write at design time and run time
Yes you can
Use the DataFormats property of the Data grid. From MSDN:

DataFormats Property

Sets or returns the StdDataFormat objects to which a bound object is attached. Read/write both at design time and run time.



The object placeholder represents anobject expression that evaluates to an object in the Applies To list.


Complex bound objects can be bound to multiple StdDataFormat objects. The DataGrid, for example, has one binding per column. The DataFormats property allows access to multiple StdDataFormat objects bound to a single control.

The DataFormats property can also be set through a property page from the Properties window. If the setting in code differs from the the property page, the property page setting is used for the first record fetched, and from then on the values in code will be used.

Format Objects Tutorial

The following procedure gives you a brief overview of using format objects with bound controls attached to the ADO Data Control. The example uses the Nwind.mdb sample database provided with Visual Basic.

Note that although this example uses the ADO Data Control, format objects can be used with any data source.

Using the ADO Data Control with Format Objects

Create an OLEDB Data Source for the Northwind database, named "Northwind".
If a data source has not been created, follow the steps in Creating the Northwind OLEDB Data Source.

From the Project menu, set references to the Microsoft Data Formatting Object Library and the Microsoft Data Binding Collection.

On the Project menu click Components, and check the ADO Data Control.

Add the ADO Data control and three TextBox controls to a form.

Set property values as shown in the following table.Object Property Setting
ADODC1.ConnectionString =  
(Use Data Link File) northwind.udl
ADODC1.RecordSource = select * from Employees
Text1.DataSource = ADODC1
Text1.DataField = LastName
Text2.DataSource = ADODC1
Text2.DataField = HireDate
Text3.DataSource = ADODC1
Text3.DataField = Extension

Add the following declarations to the form.

' Binds textboxes to the ADO control.
Dim bc As New BindingCollection

' We'll add code to the Format event on this object.
Dim WithEvents f1 as StdDataFormat
Dim WithEvents f2 As StdDataFormat

Declaring f2 with the WithEvents statement exposes the object's events. Step 8 shows code using the Format event to work on data as it is passed back and forth between the database and bound object.

Add the following code to the form's Load event.

' Connect the BindingCollection object to the datasource.
Set bc.DataSource = ADODC1

'set a long date format string
Set f1 = New StdDataFormat
f1.Type = fmtCustom
f1.Format = "long date"

' Use the BindingCollection object to bind the 2nd text box.
bc.Add Text2, "text", "hiredate", f1

'set a currency format string
Set f2 = New StdDataFormat
f2.Type = fmtCustom
f2.Format = "$0.00"

' Use the BindingCollection object to bind the 3rd text box.
Set bc.DataSource = ADODC1
bc.Add Text3, "text", "extension", f2

' Add the following code to the f2 object's Format event.
If DataValue.Value < 3000 Then
   Text3.ForeColor = vbRed
   Text3.ForeColor = vbBlack
End If

Run the project and experiment with the ADO Data control.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

AlessandroFAuthor Commented:
The solution offers from Wsh2 is Ok but complicated, after a weekend-brainstorming I have write this code that change the format of a datagrid columuns:

Set col4 = FormMDI.DataGrid1.Columns(4)
With FormMDI.DataGrid1
     .Columns(0).Caption= "Manuale"
     .Columns(1).Caption = "Revisione"
     .Columns(2).Caption = "Descrizione"
     .Columns(3).Caption = "Data"
     .Columns(4).Caption = "Attivo"
      col4.NumberFormat = CBool(.Columns(4).Value)
end with

AlessandroFAuthor Commented:
But NumberFormat don't work with the boolean type.....
AlessandroFAuthor Commented:
Wsh2 I'am not able to declare:

Dim WithEvents f1 as StdDataFormat

because in my project StdDataFormat is unknow, I read the Msdn relative documet but I not understand.....


       .Columns(0).NumberFormat = "short date"
        .Columns(0).Alignment = dbgCenter
            .Columns(1).NumberFormat = "standard"
            .Columns(1).Alignment = dbgRight
AlessandroFAuthor Commented:
Jannea thanks for response but I have in the columns Boolean value written -1 and 0.
I must write True and False...I have just use NumberFormat but not work with the boolean type.....
Wait a second.. it sounds like in your database you have this field defined as Integer rather than boolean (True/False). If so.. change the database field data type to boolean (ie True/False) and everything should be ok.
Regardless.. please provide some information about your database connection.. <smile>.
AlessandroFAuthor Commented:
The proble is that I can't use the solution offers by Wsh2 because I'am not able to declare:

Dim WithEvents f1 as StdDataFormat

because in my project StdDataFormat is unknow I Open database with:

Set DB = New ADODB.Connection   DB.Open "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & PathDB & NomeDB

I use an adodc control to connect datagrid and databse:

FormMDI.Adodc1.RecordSource = Sql
Set FormMDI.DataGrid1.DataSource = FormMDI.Adodc1.Recordset

you have an idea ?

Alessandrof, Did you do this?

"From the Project menu, set references to the Microsoft Data Formatting Object Library" and the Microsoft Data Binding Collection."

That is where your definitions are.. <smile>.

AlessandroFAuthor Commented:
Ok I write in my code:

Dim WithEvents f1 As StdDataFormatSet f1 = New StdDataFormat
f1.Type = fmtBoolean

f1.FalseValue = "Non attivo"
f1.TrueValue = "Attivo"
Set .Columns(5).DataFormat = f1

I can now set the datagrid.dataformat Thanks.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now