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

Adding onError attribute to GridImageColumn bombs when Grouping

2 Answers 89 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael Malone
Top achievements
Rank 1
Michael Malone asked on 06 Nov 2012, 09:44 PM
In order to replace all broken image links in my RagGrid, I'm using the following code in my ItemDataBound() event to add the "onError" attribute to my GridImageColumn. The code works great until a Grouping is performed on the RadGrid, which throws an exception of "Specified argument was out of the range of valid values.". Is there any workaround for this? I tried checking if Controls.Count > 0 but then the attribute is never added. 


void uxDirectoryGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        //set the onError attribute to handle images which do not exist
        var imageColumnIndex = e.Item.OwnerTableView.Columns.FindByUniqueName("AccountName").OrderIndex;
        var imageCell = e.Item.Cells[imageColumnIndex];
        var image = imageCell.Controls[0] as Image; //<--- BOMBS OUT HERE: Specified argument was out of the range of valid values
 

 
        if (image != null)
        {
            image.Attributes["onError"] = "this.src='EmployeePhotos/404.jpg'";
        }
    }
}

2 Answers, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 09 Nov 2012, 11:45 AM
Hello Michael,

If the control is rendered  and in the same time the e.Item.Cells[imageColumnIndex] returns cell with 0 controls inside, then the control is not in that column.
Check if the imageColumnIndex is correct.
If the Image is never rendered in some cases, then it is normal to check if (imageCell.Controls.Count > 0) and not to add the attribute. You can't add attribute to a control that does not exist.

Greetings,
Vasil
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Debora
Top achievements
Rank 1
answered on 16 Jan 2013, 01:39 PM
The problem with your code is in the "OrderIndex" attribute. It does not represent the Cell Index, but the view order of the columns (in the case you want to reorder them).
Try to use something like that code instead

GridTableCell cell = new GridTableCell();
 
for (int i = 0; i < e.Item.Cells.Count; i++)
{
    if ((e.Item.Cells[i] as GridTableCell).Column.UniqueName == "AccountName")
        cell = e.Item.Cells[i] as GridTableCell;
}

Tags
Grid
Asked by
Michael Malone
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Debora
Top achievements
Rank 1
Share this question
or