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

WPF radgridview coloring of cells if datatable is the itemsouorce

3 Answers 41 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Iron
Peter asked on 24 Apr 2021, 06:09 AM

Dear Team,

Im kinda new in WPF, i switched from winform ~a year ago. I have to move one of my old projects from winform to WPF (VB.NET), and as i see the basic gridview is not too good, so im about to buy the radgridview from Telerik.

Im trying, and i cant solve one thing:

I have a 'datatable', which is passed to the radgridview as itemsource. The columns are partnumbers the rows are dates, and both are dynamic - i mean the number of both them are dynamic - so i cannot pre-define them, in the xaml, but i need to change the backgorund color for some cell, and the triggers can be several kind:

 - If cell was edited

 - If cell value < 0  

 - triggers which doenst depend on the data in the grid, comes from other datadatable, but i know which column and row has to be changed.  

 

Since virtualization is ON for rows and columns as well, if i just set the background color of the cell, and scroll away, the color disappears.

My approach (probably wrong):

--------------------------------------------------------------------

      Dim myDataGridRow As Telerik.Windows.Controls.GridView.GridViewRow = CType(dgw_FG.ItemContainerGenerator.ContainerFromItem(dgw_FG.Items(1)), Telerik.Windows.Controls.GridView.GridViewRow)
         Dim G As Telerik.Windows.Controls.GridView.GridViewCell = myDataGridRow.Cells(4)
        G.Background = New SolidColorBrush(Colors.Blue)

--------------------------------------------------------------------

Is there a way to recolor a cell background/foreground knowing only the X/Y coordinate of the filed on vb-code-side?

I suffered 3 days with this question, reading a lot of threads here... but i stuck. An example code would make me happy ;)

Thank you for your answer !!!

 

Peter B (Hungary)

 

3 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 28 Apr 2021, 04:40 PM

Hi Peter,

Thank you very much for the provided image.

Indeed, it is not advisable to work directly with the visual elements, such as the GridViewCell, and set their properties due to the UI virtualization of the control. 

I believe, however, that you can achieve the desired behavior by creating an appropriate StyleSelector and setting it as the CellStyleSelector property of the RadGridView control.

For the purpose, however, you would need to hold information about the state of a particular property of a particular item - you can either hold this information in each item or have an external structure which would be responsible for this.

You can then translate the X/Y coordinate to a particular item and corresponding property of this item and determine what style should be provided for this cell value.

As this is really dependent on the exact setup at your end, if you require further assistance with the exact implementation, please demonstrate your scenario in a small sample project so that I can try to give you a concrete example. If you do not find this possible, please provide some code snippets which demonstrate how the data is bound to the control and how the background of the cell will be determined so that I can try to come up with an adequate solution.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Peter
Top achievements
Rank 1
Iron
commented on 29 Apr 2021, 12:35 PM

Dear Dilyan!
Many thanks! I tried, but seems im still not experienced enough for these. I placed an example on my server:
www.szanesz.net/telerik.zip

(its not https, so browser may complain about it, but dont worry its harmless)

if you could add these i would be very happy :)
thanks
Peter
1
Dinko
Telerik team
answered on 03 May 2021, 03:36 PM

Hi Peter,

Thank you for the provided image.

You can consider using CellStyleSelector when the cell value goes below 0. The SelectTemplate method will be called when you edit a cell from the row. Now the tricky part will be to mark when a cell was edited. I am not sure that you can modify the DataTable but what comes up to my mind is to add a boolean property that could be set to true when a cell is edited. You could set this property in the PreparingCellForEdit event handler. Could this be an option for you?

Regards,
Dinko
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Peter
Top achievements
Rank 1
Iron
commented on 04 May 2021, 05:48 AM

Hello,

Yes, i see that CellStyleSelector would help, but i still cant imagine how could i use it if the datasource is a dynamix datatable. I cant predefine the columns in xaml, because columsn are partnumbers and they are always different... sorry, i have not enough experience with xaml yet, but i try to do my best...
I placed an example on my server:
www.szanesz.net/telerik.zip

Can be the dewsired colorings achieved via vb code without poking the xaml?

Peter
Dinko
Telerik team
commented on 06 May 2021, 12:55 PM

Hi Peter,

 I am not able to download your project due to some limitations/security policies we are following.

After searching for a better way to mark the edited cell, I think I found one which you can try. You can subscribe to the CellEditEnded event of the RadGridView. Inside the event handler, you can check if the user changed the initial value of the cell and, if yes, change the background color.

Private Sub clubsGrid_CellEditEnded(ByVal sender As Object, ByVal e As GridViewCellEditEndedEventArgs)
    If e.NewData <> e.OldData AndAlso e.EditAction = Telerik.Windows.Controls.GridView.GridViewEditAction.Commit Then
        e.Cell.Background = Brushes.Red
    End If
End Sub

You can further test the approach and modify it if required.

Regards,
Dinko
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer, and each of you can get a $50 Amazon gift voucher.

Peter
Top achievements
Rank 1
Iron
commented on 13 May 2021, 03:58 AM

Dear Dinko.

Its not good, because virtualization is ON. (it has to be because i have more hundred columns)
After edit it changes to red, but if i scroll alway and scroll back the background color is white again.... :(

Peter
0
Dinko
Telerik team
answered on 17 May 2021, 09:27 AM

Hi Peter,

You are right that virtualization will break the approach. This is because the cells are reused. I have searched for other solutions and what I can suggest is to add an additional column for each property that will hold if a cell was edited. You can remove these columns in the AutoGeneratingColumn event handler. Then you can create a custom StyleSelector for the CellStyleSelector property of the columns. Inside the custom class, the SelectStyle property will be called when the cell finishes its edit mode. To better demonstrate what I have in mind, I attached the sample project which I used to test your scenario.

I hope that this project is helpful.

Regards,
Dinko
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Peter
Top achievements
Rank 1
Iron
commented on 05 Jun 2021, 04:22 PM

Dear Dinko!
Thanks for the example code.. im a VB guy, but i can translate with the telerik converter... i think this will help!
Tags
General Discussions
Asked by
Peter
Top achievements
Rank 1
Iron
Answers by
Dilyan Traykov
Telerik team
Dinko
Telerik team
Share this question
or