Solved

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

Posted on 2013-06-19
3
462 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
ID: 39262425
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
ID: 39262489
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
ID: 39262496
Sorry  roopeshreddy, i had the browser opened for a long time as it seems...
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

775 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