Solved

Confusing error with Image properties in a datagridviewimagecolumn when the datagrid also contains text columns?

Posted on 2013-06-19
3
457 Views
Last Modified: 2013-06-20
Hello experts, I've set my columns in a datagrid, and then want to apply additional parameters to the sole Image column.  The following code:

//  Set the datagrid font & size for datagrid "AddSingle"
foreach (DataGridViewColumn c in dGridAddSingle.Columns)
{
     c.DefaultCellStyle.Font = new Font("Calibri", 10);
     c.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
}
dGridAddSingle.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dGridAddSingle.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dGridAddSingle.MultiSelect = false;

foreach (DataGridViewImageColumn c in dGridAddSingle.Columns)
{
     c.ImageLayout = DataGridViewImageCellLayout.Stretch;
}

Open in new window


falls over at:

"foreach (DataGridViewImageColumn c in dGridAddSingle.Columns)"
with an error pointing to the "DataGridViewImageColumn"

with invalid cast exception, unable to cast object of type
'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Windows.Forms.DataGridViewImageColumn'.

What I don't understand (Yes I'm a beginner) is why is it trying to cast the TextBox columns to ImageColumns?  I though the ForEach loop would only have me looping through the imageColumns in the datagrid and secondly, how should I be doing this?

Thanks for any and all input.
0
Comment
Question by:StuBabyAight
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Roopesh Reddy earned 500 total points
Comment Utility
Hi,

DataGridView contains both TextColumns as well as ImageView columns! If you try to loop through all the columns, and cast to DataGridViewImageColumn, then it will throw error obviously!

You may have to cast it to DataGridViewColumn and inisde you may have to check whether the column is Text Column or Image column!

Hope it helps u...
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
Hi,

Do not explicitly convert it to DataGridViewImageColumn.
Try testing if the column is of type DataGridViewImageColumn before doing anything with it and then cast it to DataGridViewImageColumn
foreach (var c in dGridAddSingle.Columns)
{
     if (c is DataGridViewImageColumn)
     {
          ((DataGridViewImageColumn)c).DefaultCellStyle.Font = new Font("Calibri", 10);
          ((DataGridViewImageColumn)c).DefaultCellStyle.WrapMode = DataGridViewTriState.True;
     }
}

Open in new window


Giannis
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
Sorry  roopeshreddy, i had the browser opened for a long time as it seems...
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now