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

[Solved] RadGridView e.Row.Cells.Count in RowLoaded event too small

2 Answers 193 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ralph
Top achievements
Rank 1
Ralph asked on 05 Aug 2011, 10:04 PM
I am attempting to format the background color in the RowLoaded event. For some data sets it doesn't work. As far as I can tell the e.Row.Cells.Count value is not always the final value. My code is below.

The error message appears here
---------------------------

---------------------------
Row Loaded
Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index
Stack Trace
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)

   at System.ThrowHelper.ThrowArgumentOutOfRangeException()

   at System.Collections.Generic.List`1.get_Item(Int32 index)

   at RRI.PMDG.UI.Controls.MainPage.GridViewRowLoaded(RowLoadedEventArgs e, Object sender)
---------------------------
OK  
---------------------------

private void GridViewRowLoaded(RowLoadedEventArgs e, object sender)
{

 try
 {
  if (e.Row is GridViewRow)
  {
   if ((sender as RadGridView) != null) //Make sure that casting doesn't fail
   {
    int cellCount = e.Row.Cells.Count;
    if (cellCount >= 5) // a legacy I don't understand.
    {
     //_riskFactor = "Factor";
     var CellFgBinding = new System.Windows.Data.Binding("Factor");
     CellFgBinding.Converter = new AssementColorConverter(cardInfoColActive.AssesmentColorItems);
     e.Row.Cells[(sender as RadGridView).Columns[_riskFactor].DisplayIndex].SetBinding(GridViewCellBase.BackgroundProperty, CellFgBinding);

     var safetyBgBinding = new System.Windows.Data.Binding("SafetyRiskRankColor");
     safetyBgBinding.Converter = new SafetyColorConverter();
     int displayIndex = (sender as RadGridView).Columns["SafetyRiskRankDescription"].DisplayIndex;
     if (cellCount > displayIndex)
     {
      GridViewCellBase gridViewCell = e.Row.Cells[displayIndex];
      gridViewCell.SetBinding(BackgroundProperty, safetyBgBinding);
     }
    }
   }
  }
 }
 catch (Exception ex)
 {
  #region TRASH
  MessageBox.Show("Row Loaded\n" + ex.Message+
   "\nStack Trace\n" + ex.StackTrace);
  #endregion
 }
 }
 

 

2 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 08 Aug 2011, 12:55 PM
Hi Ralph,

Occasionally Silverlight's Loaded event is fired before the control has its template applied, so that it does not provide a reliable option for interaction with the visual tree. You may read more on this topic in this article. Would you please shed some more light on your requirements so that we could think of an alternative solution?

All the best,
Ivan Ivanov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
0
Ralph
Top achievements
Rank 1
answered on 15 Aug 2011, 09:43 PM
//Here is the hack a previous developer resorted to...
if (cellCount >= 5) // a legacy I don't understand.
{
    //_riskFactor = "Factor";
    var CellFgBinding = new System.Windows.Data.Binding("Factor");
    CellFgBinding.Converter = new AssementColorConverter(cardInfoColActive.AssesmentColorItems);
    e.Row.Cells[(sender as RadGridView).Columns[_riskFactor].DisplayIndex].SetBinding(GridViewCellBase.BackgroundProperty, CellFgBinding);
  
    var safetyBgBinding = new System.Windows.Data.Binding("SafetyRiskRankColor");
    safetyBgBinding.Converter = new SafetyColorConverter();
    int displayIndex = (sender as RadGridView).Columns["SafetyRiskRankDescription"].DisplayIndex;
    if (cellCount > displayIndex)
    {
        GridViewCellBase gridViewCell = e.Row.Cells[displayIndex];
        gridViewCell.SetBinding(BackgroundProperty, safetyBgBinding);
    }
}

Within the previously posted code is the about if statement, where the Count of columns is verified to be greater than 5. I don't see why this is ever necessary. Neverthess the line "e.Row.Cells[(sender as RadGridView).Columns[_riskFactor].DisplayIndex].SetBinding(GridViewCellBase.BackgroundProperty, CellFgBinding); " will throw an exception if this check is not there. The fact is now I have added enough columns that this check gets in the way. I want to use my color converter to change the background of a cell. This used to work.

Edit:
I have recently learned that the magical number 5 in the if statement is the column index in question. I have also discovered that e.Row.Cells.Count changes multiple times and depends on which row is being loaded. Is the grid essentially a jagged array?
Tags
GridView
Asked by
Ralph
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Ralph
Top achievements
Rank 1
Share this question
or