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

New To Telerik, and the GridView Control

7 Answers 95 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 01 Feb 2016, 04:43 PM

I am trying to change the background colour in a GridView as the data is loaded.

I think that I should be using 

 

        private void manualGridView_CellLoaded(object sender, Telerik.Windows.Controls.GridView.CellEventArgs e)
        {

        }

 

but I uncertain how to access the background colour from here and where I might test which column I am in and the cells value.  Any help is much appreciated.

7 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 02 Feb 2016, 09:01 AM
Hello Carl,

Thank you for contacting us.

Would it be possible to provide some more details on your setup so I can advise with the most convenient approach for you?

If you would like to conditionally style a cell based on its value you can check the following article from our documentation page - CellStyleSelector. You can also find a demo that implements the CellStyleSelector in our SDK Samples Browser which you can download from here - http://demos.telerik.com/xaml-sdkbrowser/. Generally, I suggest checking the following article as well - RadGridView - Styling Cells

Apart from that, you can check the cell`s column through the CellLoaded event by the following approach:
private void clubsGrid_CellLoaded(object sender, CellEventArgs e)
       {
           if (e.Cell.Column.UniqueName == "Name")
           {
               ...
           }
       }

I am looking forward to your reply, Carl.

Regards,
Stefan Nenchev
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Carl
Top achievements
Rank 1
answered on 04 Feb 2016, 02:54 PM

Hi Stefan, I eventually solved the issue:  The code behind is a concept prototype so the two parts seen here will eventually reside in the ViewModel, but for the purpose of changing colours this works just fine for me.  But should you have a better way of doing this I would be interested in using that. 

 

 private void manualGridView_CellLoaded(object sender, Telerik.Windows.Controls.GridView.CellEventArgs e)
        {          
             
            if (e.Cell.ToString().Contains("GridViewHeaderCell"))
            {
                vmDevelopers.PresentationFilters.AddField(e.Cell.Column.UniqueName.ToString()); 
                

            }


            if (e.Cell.ToString().Contains("GridViewCell") && e.Cell.Column.UniqueName == "YearsUsed")
            {

                if (Convert.ToInt32(((TextBlock)e.Cell.Content).Text ) > 8)
                { 
                    e.Cell.Background = Brushes.Red;
                    
                }
  
                 
            }           
        }

0
Stefan Nenchev
Telerik team
answered on 05 Feb 2016, 11:52 AM
Hello Carl,

Generally, we do not advise on working with the visual elements such as the cells and the rows in that manner as the RadGridView uses UI virtualization in order to boost its performance. This also means that when scrolling - the visual elements are reused for the upcoming items of the underlying data source. With this in mind, using the visual elements for some internal operations might result in inconsistent results at your end.

In order to achieve such conditional styling of the I suggest you review the previously mentioned CellStyleSelector property of the RadGridView`s columns. I have added a sample project that implements a scenario that is pretty similar to yours. Please go through it and consider such approach at your end.

Regards,
Stefan Nenchev
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Carl
Top achievements
Rank 1
answered on 31 Mar 2016, 07:50 AM

Hi Stefan

Apologies for taking such a long time returning to this.  I was out of touch onsite for over a month.  I have been able to use what you suggested but I have a further an possibly related issue.  To make the dynamic formatting I used the GridView_RowLoaded event. For the post completion I used the GridView_Loaded event.  

The code looks like this 

        private void safeDataGridView_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
        {
            foreach (GridViewCellBase cell in e.Row.Cells)
            {
                if (cell.ToString().Contains("GridViewCell") && cell.Column.UniqueName == "Background")
                {
                    if (((TextBlock)cell.Content).Text.ToString() != string.Empty && ((TextBlock)cell.Content).Text.ToString().Length > 0)
                    {
                        Brush backgound = ((TextBlock)cell.Content).Text.ToString().ToBrush();
                        e.Row.Background = backgound;
                    }
                }

                if (cell.ToString().Contains("GridViewCell") && cell.Column.UniqueName == "Foreground")
                {
                    if (((TextBlock)cell.Content).Text.ToString() != string.Empty && ((TextBlock)cell.Content).Text.ToString().Length > 0)
                    {
                        Brush foreground = ((TextBlock)cell.Content).Text.ToString().ToBrush();
                        e.Row.Foreground = foreground;
                    }
                }
            }
        }

 

        private void safeDataGridView_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.safeDataGridView.Columns.Count > 0)
            {
                Int32 iCount = this.safeDataGridView.Columns.Count - 1;
                this.safeDataGridView.Columns[iCount].IsVisible = false;
                this.safeDataGridView.Columns[iCount - 1].IsVisible = false;
            }
        }

 

The columns Background and Foreground are used to determine the rows colour format.  I then remove those two columns at the end of formatting so that the user does not see them.  This final step works fine when I initially load the control but when I close the properties form that specifies the rules when a format is applied I update the data in the DataContext.  The whole thing works fine up until the step.  The loaded event does not fire.  Is there a means to trigger this or is there something like a reloaded event that I could use?

 

0
Stefan Nenchev
Telerik team
answered on 01 Apr 2016, 11:38 AM
Hello Carl,

As previously advised, working with the visual elements directly(GridViewCell, GridViewRow) is highly not recommended and might result in inconsistent behavior as in your case. Please review the updated project that uses a RowStyleSelector instead.

Regards,
Stefan Nenchev
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Carl
Top achievements
Rank 1
answered on 04 Apr 2016, 12:56 PM

Hi Stefan

Perhaps I can explain why I can't follow your advice.  The method you are suggesting is based upon the premiss that the dataset is known and the behaviour is reacting to to the data found in that known dataset.  There may even be several known datasets with known columns or configurations where columns are removed from the super set of known columns.  

The control I am working on is based upon a situation where the end user can put any valid SQL query against the grid and based upon that queries resulting dataset write formatting which suites their requirements.  It cannot be anticipated using redefined formatting what rules the end users want to deliver to screen.

 

So in this particular case I need to apply the rules into an extended dataset and use code behind to deliver the format to screen.  Can I count on you to help me with this?

 

Kind regards

 

Carl

0
Stefan Nenchev
Telerik team
answered on 07 Apr 2016, 07:48 AM
Hello Carl,

Please refer to the ticket you have raised regarding the issue where you have provided the actual sample project. As advised there, you need to use a StyleSelector approach that will work with the Background and Foreground properties instead of using the RowLoaded and CellLoaded events.

Regards,
Stefan Nenchev
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
GridView
Asked by
Carl
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Carl
Top achievements
Rank 1
Share this question
or