Home › Forums › WinForms controls › Xceed Grid for WinForms › valuechanged during
-
AuthorPosts
-
#14426 |
‘conncting datasource with grid
grdTest.DataSource = testDS.Tables(0)‘adding handler for value changed
grdTest.BeginInit()
Dim cell As DataCell
For Each cell In .DataRowTemplate.Cells
AddHandler cell.ValueChanged, AddressOf formatRows
Next‘changing the row colow depends on the value of the given cell
Private Sub formatRows(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Dim row As CellRow = grdTest.CurrentRow
If row.Cells(0).Value = “ColorMe” Then
row.ForeColor = Color.Red
Else
row.ForeColor = Color.Black
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End SubThis above code throws ‘object refreence not set to an instance of an object’ exception
How can I achieve this with out this exception?
Imported from legacy forums. Posted by PETE (had 3508 views)
This won’t work.
You need to attach the handler to all the cells that are actually in your grid, so loop through each row in your table and then through each cell and add handlers
Change:
Dim row As CellRow = grdTest.CurrentRow
To:
Dim changingCell As Xceed.Grid.DataCell = DirectCast(sender As Xceed.Grid.DataCell)
Dim row As Xceed.Grid.DataRow = Nothing
If Not changingCell Is Nothing Then
row = changingCell.ParentDataRow
EndIf Not row Is Nothing Then
‘ Rest of your code goes here.
End If
Instead of adding handlers though inherit from DataCell and DataRow and use them. It’s more memory efficient, plus you have greater control and easier use. There are documents in the Xceed documentation that tell you how to do this.
Imported from legacy forums. Posted by Chris (had 306 views)
Thank you Mr.Unasking.
its comes out with systax error as DirectCast has two parameter.
what is the another parameter for DirectCast?I remember you already suggested the same thing to one of my previous question about using handlers. But I could not figure it out yet.
Looking forward for your help again.s
Imported from legacy forums. Posted by PETE (had 425 views)
Yeah sure sorry, I’ve been working with C# so much that trying to remember what the VB.NET code is gets me all confused.
Yeah DirectCast should be called like this DirectCast(object, type) so
Dim changingCell As Xceed.Grid.DataCell = DirectCast(sender, Xceed.Grid.DataCell)DirectCast is better than Cast in Vb.NET because if it can’t convert it it returns Nothing instead of throwing an exception and interestingly it has a slight (but only minisucle) performance enhancement (again). I’ve read it somewhere but I can’t remember where.
Yeah as I said it’s probably easier to inherit from the classes. Adding handlers is not wrong but reading the MSDN documentation inheriting doesn’t use delgates and only virtual method calls so is slightly better on performance, plus you don’t have to worry about adding more than one handler to a cell.
Hope this helps
Imported from legacy forums. Posted by Chris (had 358 views)
Thank you again for your response, which was very helpful.
Imported from legacy forums. Posted by PETE (had 4663 views)
-
AuthorPosts
- You must be logged in to reply to this topic.