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

Object reference not set to an instance of an object when trying to assign picture to a gridview column

3 Answers 250 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hossein
Top achievements
Rank 1
Hossein asked on 03 Aug 2012, 02:28 PM
Hello all .
I'm trying to assign a picture to a gridview column . i use the following statements , but i get a" Object reference not set to an instance of an object. "error .
How can i fix that ?
In my forms constructor i have this :
((GridTableElement)this.radGridView_ClassInfo_ListHonarjoyan.GridElement).RowHeight = 80;

And in the form_load() i have this :
foreach (GridViewRowInfo row in radGridView1.Rows)
            {
                row.Cells["picture"].CellElement.ImageLayout = ImageLayout.Zoom;
                row.Cells["picture"].CellElement.ImageAlignment = ContentAlignment.MiddleCenter;
                if (row.Cells["image"].Value == null )
                {
                    row.Cells["picture"].CellElement.Image = Image.FromFile("img/1.jpg");
                }else
                row.Cells["picture"].CellElement.Image = Image.FromFile(row.Cells["image"].Value.ToString());
                //pictureBox1.Image = Image.FromFile(row.Cells["Picture"].Value.ToString());
            }

in my table , the image column contains an address to the image on my drive , and if there is , to show the picture , i created a imagecolumn from radGridViews Properties (collections) and named it image . thats what im trying to do

3 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 08 Aug 2012, 11:12 AM
Hello Hossein,

Thank you for writing.

In 2010 we have performed a major refactoring of RadGridView and since then CellElements and RowElements should not be accessed directly. Here is a blog regarding this matter: http://blogs.telerik.com/blogs/posts/10-07-27/explained-radgridview-for-winforms-virtualization-and-its-implications-the-case-with-cellelement-and-rowelement.aspx.

Please consider using the CellFormatting event for this purpose: http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html. Alternatively, you can use GridViewImageColumn:  http://www.telerik.com/help/winforms/gridview-columns-gridviewimagecolumn.html.

I hope this helps. 

Regards,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Hossein
Top achievements
Rank 1
answered on 09 Aug 2012, 01:20 AM
Hello Stefan ,
Thank you very much. but i got my answer that day after alot of researchings and through trial and error .
but the links you provided are very good as well and helpful . i used cell view formatting event ( have no idea how different it is from cell formatting anyway ! would like to know the difference by the way)
0
Stefan
Telerik team
answered on 13 Aug 2012, 09:48 AM
Hi Hossein,

The difference between the CellFormatting and the ViewCellFormatting events is that the first one is fired only for data cells while the other one is fired for all cells in the grid including filter cells, header cells, group cells, etc.

I hope that you find this information useful.
 
All the best,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Hossein
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Hossein
Top achievements
Rank 1
Share this question
or