Problem with datagridview.CellFormatting event

V

Vili

Hi all

Posted this to microsoft.public.dotnet.framework.windowsforms.controls,
but with no answers so I try here.

I have created a datagridview and a datasource at designmode and then
added two colums.
Second column is my own custom column with custom cell where I want to
insert object.
Problem is that on the CellFormatting event the e.ColumnIndex never
points to myCol -column. How can I get the cellformatting happen to
myCol?

I noticed that somehow when the CellFormatting event happens
DGV.Columns.Count is 1
and it should be 2. Lice my cell doesn't even exist

Here is some code

DGV.AutoGenerateColumns = False
Dim newColIndex As Integer = DGV.Columns.Add("ID", "ID")
DGV.Columns(newColIndex).DataPropertyName = "ID" 'Bound column
Dim col As New ControlColumn
col.Name = "myCol"
col.HeaderText = "myCol"

DGV.Columns.Add(col) 'Unbound custom column

'Here is my CellFormatting event with the problem

Private Sub DGV_CellFormatting(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellFormattingEventArgs)
Handles DGV.CellFormatting
If e.ColumnIndex = DGV.Columns("Oma").Index Then
e.FormattingApplied = True
dim oMyObject as new mycustomclass
'Setting up the object here...
e.Value = oMyobject
End If
End Sub
 
V

Vili

I have the same problem, did you find the solution.

EggHeadCafe.com - .NET Developer Portal of Choicehttp://www.eggheadcafe.com

It seems that the only way was to add the columns in code

Dim myCol as new myColumnClass.myColumn

With myGrid
.Columns.Add("Name", "Name")
.Columns.Add(myCol)

...dataproperties
...other properties
End with

and then.. This seems to work

Private Sub myGrid_CellFormatting(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles
myGrid.CellFormatting
If e.ColumnIndex = 1 Then
'DoStuff
End If
End Sub

Tuomo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top