Solved

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

Posted on 2013-06-19
3
471 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exception when using Alea null reference in fsharp.core.dll 3 79
c#, case, if 4 37
ComboBox to String Not Working 5 43
Run software updates from the website 6 50
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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