Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

17 Experts available now in Live!

Get 1:1 Help Now