This is a migrated thread and some comments may be shown as answers.

Getting sender's column index

4 Answers 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ricardo Romo
Top achievements
Rank 1
Ricardo Romo asked on 09 Nov 2010, 11:53 PM
Hi,

I need to know the name of the sender's column in a GridView. The below code in WinForms Q2 2010 SP2 doesn't work. The code is:

Private Sub Grid_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Grid.ValueChanged
    Dim dce As GridDataCellElement
    dce = CType(sender, GridDataCellElement)
    If dce.ColumnInfo.Name = "Devol" Then
        If Grid.CurrentRow.Cells("Devol").Value Then
            Grid.CurrentRow.Cells("DaysDefDev").ReadOnly = False
        Else
            Grid.CurrentRow.Cells("DaysDefDev").ReadOnly = True
            Grid.CurrentRow.Cells("DaysDefDev").Value = 0
        End If
    End If
End Sub

In the previous code the column named "Devol" is GridviewCheckBoxColumn and "DaysDefDev" is a numeric column. When I check or uncheck "Devol" column an exception occurs with the message like "Can't cast from 'Telerik.WinControls.UI.RadCheckBoxEditor' to 'Telerik.WinControls.UI.GridDataCellElement'."

If I type TryCast insted of CType the object is always nothing and another exception is thrown.

Any help would be great.

Thanks.

4 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 10 Nov 2010, 06:39 AM
Hello Ricardo,

In this case the sender is the editor itself, but you can use the Grid.CurrentCell.ColumnInfo.Name, and just removing the first 2 lines, like this:
Private Sub Grid_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Grid.ValueChanged
    If Grid.CurrentCell.ColumnInfo.Name = "Devol" Then
        If Grid.CurrentRow.Cells("Devol").Value Then
            Grid.CurrentRow.Cells("DaysDefDev").ReadOnly = False
        Else
            Grid.CurrentRow.Cells("DaysDefDev").ReadOnly = True
            Grid.CurrentRow.Cells("DaysDefDev").Value = 0
        End If
    End If
End Sub

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Ricardo Romo
Top achievements
Rank 1
answered on 10 Nov 2010, 07:10 PM
Thank you Emanuel. It works good.
0
Ivan Petrov
Telerik team
answered on 12 Nov 2010, 04:20 PM
Hello Ricardo,

Thank you for your question.

I looked at Emanuel's example and noticed that it can be improved a little. In his example after this line is executed:
Grid.CurrentRow.Cells("DaysDefDev").Value = 0
the event is fired again and it goes into an infinite loop until the stack overflows.

The fix in this case is really simple:
Private Sub Grid_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
Handles Grid.ValueChanged
  If Grid.CurrentCell.ColumnInfo.Name = "Devol" Then
       If CBool(Grid.CurrentRow.Cells("Devol").Value) Then
            Grid.CurrentRow.Cells("DaysDefDev").ReadOnly = False
       Else
            Grid.CurrentRow.Cells("DaysDefDev").ReadOnly = True
            If CInt(Fix(Grid.CurrentRow.Cells("DaysDefDev").Value)) <> 0 Then
                 Grid.CurrentRow.Cells("DaysDefDev").Value = 0
            End If
       End If
  End If
End Sub

I hope this resolves your issue. If you have further questions, do not hesitate to ask us.

Best wishes,
Ivan Petrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Emanuel Varga
Top achievements
Rank 1
answered on 12 Nov 2010, 04:42 PM
Hello Ivan,

Should it fire value changed if you assign the same value?
It would be very strange it it were to do that...

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
Tags
GridView
Asked by
Ricardo Romo
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Ricardo Romo
Top achievements
Rank 1
Ivan Petrov
Telerik team
Share this question
or