Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
 DATAGRID CELL ICON C#
Need help about that
Need column, if column 2 is null, picture in cell 1 empty
If column 2 is not null need to show picture in cell1
 

 
Thank you


What I have tried:

 DATAGRID CELL ICON C#
 
Some recomandition?
 
Thank you
Posted
Updated 18-Jan-20 15:43pm

Hi Goran
What happens to your google? :)

I assume you are using WinForm, here you will find an example (see second answer): setting an Image column in a datagrid view based on a value in the database c#[^]

Note: The example shows how to bring in the image into a cell. I would recommend not to load them each time from a file, better to instantiate them in an appropriate location (e.g. your form).
 
Share this answer
 
v2
Hello Goran,

Here is some code to get to you started. Make sure the column you are displaying images in is of type DataGridViewImageColumn.

C#
// adds a few test rows to the DataGridView
dataGridView1.Rows.Add(null, "some text");
dataGridView1.Rows.Add(null, "");
dataGridView1.Rows.Add(null, "some text");
dataGridView1.Rows.Add(null, "");

// This snippet assumes there are (2) picture boxes on your form with the desired images set 

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    string val = row.Cells["Column2"].Value.ToString(); // gets the value of the current cell in Column2

    // checks whether the value is an empty string
    if (val == "")
        row.Cells["Column1"].Value = pictureBox1.Image; // empty value image
    else
        row.Cells["Column1"].Value = pictureBox2.Image; // non-empty value image
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900