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

ImageIndex issue

6 Answers 180 Views
GridView
This is a migrated thread and some comments may be shown as answers.
George Saveliev
Top achievements
Rank 1
George Saveliev asked on 08 Nov 2010, 01:36 PM

Hello!


I have an issue with assigning the ImageIndex of GridCellElement.


Let’s take a simple code sample. An image list with a single image is assigned to the grid view. All settings are by default.


01.public partial class Form1 : Form
02.{
03.    public Form1()
04.    {
05.        InitializeComponent();
06. 
07.        for (int i = 0; i < 8; i++)
08.            radGridView1.Columns.Add(new GridViewTextBoxColumn());
09. 
10.        for (int i = 0; i < 8; i++)
11.            radGridView1.Rows.Add(radGridView1.Rows.NewRow());
12.    }
13. 
14.    private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
15.    {
16.        if ((e.CellElement.RowIndex + e.CellElement.ColumnIndex) % 2 == 0)
17.        {
18.            e.CellElement.ImageIndex = 0;
19.        }
20.    }
21.}

When I build this sample with “RadControls for WinForms Q3 2009 SP1” it shows the “chessboard”. But when I build it with the latest “RadControls for WinForms Q2 2010 SP2” it shows an empty grid.


To have it running as expected I have to do the following workaround (see line 5):


1.private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
2.{
3.    if ((e.CellElement.RowIndex + e.CellElement.ColumnIndex) % 2 == 0)
4.    {
5.        e.CellElement.ImageIndex = -1;
6.        e.CellElement.ImageIndex = 0;
7.    }
8.}

So, perhaps this is a bug.


Thank you.

6 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 08 Nov 2010, 01:57 PM
Hi,

In order to set the image for a cell, inside your if statement you should set the image as follows:

e.CellElement.Image = Me.ImageList.Images(0)

Let me know if that helps, or if you need more assistance.
Richard
0
George Saveliev
Top achievements
Rank 1
answered on 08 Nov 2010, 02:06 PM

Hello!

This works, thank you!

But how should the RadGridView.ImageList and GridCellElement.ImageIndex properties be used?

Thank you.

0
Richard Slade
Top achievements
Rank 2
answered on 08 Nov 2010, 02:25 PM
Hi George,

Glad that worked for you. Please remember to mark as answer so others can find the answer too.

I have tried in a grid setting the ImageIndex property, and I was able to replicate your issue. I also had to set the image Index to -1 before setting it to 0 in order for the image to display.

I'd say that this is a bug and needs to be reported via the report a bug link in the Your Account section.

Hope that helps
richard
0
Alexander
Telerik team
answered on 11 Nov 2010, 02:48 PM
Hello George,

Thank you for reporting this issue. We will try to address it in a next release. I have updated your Telerik points.

The CellFormatting event of RadGridView is raised not only for data cells, but for the cells of the new row and filter row. You can improve the implementation in the event handler by making sure that the image is added only to data cell element:
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    int rowIndex = e.CellElement.RowIndex;
    if (rowIndex > -1 && (rowIndex + e.CellElement.ColumnIndex) % 2 == 0)
    {
        e.CellElement.Image = this.imageList1.Images[0];
    }
}

If you need these cells only for displaying images, you can use a GridViewImageColumn instead of the GridViewTextBoxColumn. You can set the desired cell image as the Value of the image cell and remove the CellFormatting event handler.

Best regards,
Alexander
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
George Saveliev
Top achievements
Rank 1
answered on 12 Nov 2010, 04:25 PM

Thank you! 

As for the GridViewImageColumn, I tried it with the ImageIndex, but got the same issue I described in the original message.

Using the Image instead of ImageIndex is a good workaround.

0
Alexander
Telerik team
answered on 17 Nov 2010, 05:02 PM
Hello George,

When you use GridViewImageColumn, you do not need to use the CellFormatting event of RadGridView. You can set the image as Value of the cell. Please review your example implemented with GridViewImageColumns:
for (int i = 0; i < 8; i++)
{
    radGridView1.Columns.Add(new GridViewImageColumn());
}
 
for (int i = 0; i < 8; i++)
{
    radGridView1.Rows.Add(radGridView1.Rows.NewRow());
    for (int j = 0; j < radGridView1.Columns.Count; j++)
    {
        if ((i + j) % 2 == 0)
        {
            radGridView1.Rows[i].Cells[j].Value = this.imageList1.Images[0];
        }
    }
}

The performance of this solution will be better, because it does execute the code from the CellFormatting event handler when a cell element is repainted.

Best regards,
Alexander
the Telerik team
See What's New in RadControls for WinForms in Q3 2010 on Wednesday, November 17, 11am Eastern Time: Register here>>
Tags
GridView
Asked by
George Saveliev
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
George Saveliev
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or