Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - DataGridView - Image and Text on the same row

I am looking for something like this on DataGridView:

Image  | Text | 
Text   | Text |
Image  | Text

Basically, I just want Image cells and Text cells on the same row. I was able to get it to work with different types, for example: Checkbox, Text, and etc... But I am not able to get it to work with images.

I get this error: Invalid Cast from 'System.String' to 'System.Drawing.Image'

Does anybody know the solution or have suggestion on how I should do this ? Thanks

like image 761
FerX32 Avatar asked Jul 14 '12 04:07

FerX32


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

It is relatively easy to have a column of type DataGridViewImageColumn display text in certain cells.

All you need to do is replace a desired cell with a DataGridViewTextBoxCell.

So for example, if I add the following image column to my grid:

DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
imageColumn .Name = "ImageColumn";
imageColumn .HeaderText = "An Image!";
Image i = Image.FromFile(@"C:\Pictures\TestPicture.jpg");
imageColumn.Image = i;
dataGridView1.Columns.Add(imageColumn);

You can replace a given cell with text like so (here in a button handler but you could also do it somewhere like within a databinding complete handler).

private void button1_Click(object sender, EventArgs e)
{
    dataGridView1.Rows[3].Cells["ImageColumn"] = new DataGridViewTextBoxCell();
    dataGridView1.Rows[3].Cells["ImageColumn"].Value = "Some text!";  
}

This solution leaves a little bit of work for you if you want different images (you need to bind to a property of type image) and if you want different text. Since the Value property of an image column is of type Image you cannot use the same binding.

You could make your own overridden image column that handled this, but it would be a bit of work, that might not pay for itself vs. simply setting the value for the cells where you want text directly.

like image 175
David Hall Avatar answered Oct 07 '22 04:10

David Hall