Solved

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

Posted on 2013-06-19
3
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

623 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