Solved

Dataformat with Datagrid

Posted on 2000-04-07
12
2,415 Views
Last Modified: 2008-03-03
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

Thanks
0
Comment
Question by:AlessandroF
12 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2693952
should be able to...it is read/write at design time and run time
0
 
LVL 1

Expert Comment

by:riduce
ID: 2694023
Yes you can
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
ID: 2694033
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.

Syntax

object.DataFormats

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

Remarks

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
Else
   Text3.ForeColor = vbBlack
End If

Run the project and experiment with the ADO Data control.
0
 

Author Comment

by:AlessandroF
ID: 2699401
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


0
 

Author Comment

by:AlessandroF
ID: 2699594
But NumberFormat don't work with the boolean type.....
0
 

Author Comment

by:AlessandroF
ID: 2700023
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.....

Thanks




0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Expert Comment

by:jannea
ID: 2700470
       .Columns(0).NumberFormat = "short date"
        .Columns(0).Alignment = dbgCenter
            .Columns(1).NumberFormat = "standard"
            .Columns(1).Alignment = dbgRight
0
 

Author Comment

by:AlessandroF
ID: 2700626
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.....
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2701090
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>.
0
 

Author Comment

by:AlessandroF
ID: 2711806
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
FormMDI.Adodc1.Refresh
Set FormMDI.DataGrid1.DataSource = FormMDI.Adodc1.Recordset

you have an idea ?

0
 
LVL 14

Expert Comment

by:wsh2
ID: 2712690
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>.

0
 

Author Comment

by:AlessandroF
ID: 2715310
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.
Ciao

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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 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…

707 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

15 Experts available now in Live!

Get 1:1 Help Now