Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For Ex:
I have to load icons in 1st two columns in a gridview. That should change the icons when its true, and when it is false show different icon.

Can anyone give me idea? how can i achieve this task!

Much needed Answer.

Thank you...!

What I have tried:

Actually, I am newbie to c#. so I am looking for someone to help me out from this...
thanks
Posted
Updated 30-Mar-16 23:56pm
v2
Comments
FARONO 31-Mar-16 3:22am    
What is the data for the gridview? You could switch the icon based on a true/false boolean in your dataset?
Santosh Kokatnur 31-Mar-16 3:57am    
@FARONO... Just is manually added.... ya as you said, boolean in dataset. Can you please provide me the code?

using DataGridViewImageColumn [^] you can create it.

Add the relevant images to the resources Folder How to: Add or Remove Resources[^]

C#
DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
dataGridView2.Columns.Add(imageCol);


for (int i = 0; i < dataTable.Rows.Count; i++)
{
    var flag = dataTable.Rows[i]["BooleanColumn"].ToString().ToLower() == "true";

    if (flag)
        dataGridView2.Rows[i].Cells[0].Value = WindowsFormsApplication1.Properties.Resources.image1;
    else
        dataGridView2.Rows[i].Cells[0].Value = WindowsFormsApplication1.Properties.Resources.image2;
}
 
Share this answer
 
v2
Comments
Santosh Kokatnur 31-Mar-16 5:15am    
Can you please provide me Full code?
Karthik_Mahalingam 31-Mar-16 5:17am    
what do you mean by full code.. I have created a sample app like this

private void Form1_Load_1(object sender, EventArgs e)
{

DataTable dataTable = new DataTable();
dataTable.Columns.Add("flag");
dataTable.Columns.Add("Name");
dataTable.Rows.Add(true, "aa");
dataTable.Rows.Add(false, "bb");
dataTable.Rows.Add(true, "cc");
dataGridView2.DataSource = dataTable;


DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
dataGridView2.Columns.Add(imageCol);


for (int i = 0; i < dataTable.Rows.Count; i++)
{
var flag = dataTable.Rows[i]["BooleanColumn"].ToString().ToLower() == "true";

if (flag)
dataGridView2.Rows[i].Cells[0].Value = WindowsFormsApplication1.Properties.Resources.image1;
else
dataGridView2.Rows[i].Cells[0].Value = WindowsFormsApplication1.Properties.Resources.image2;
}




}
Karthik_Mahalingam 16-May-16 1:42am    
its difficult, i dont have time to break my head on it.
post it on cp, someone will help you..
Karthik_Mahalingam 16-May-16 1:51am    
when will you learn ?
i would suggest you to break your module into N number of pieces and do bit by bit, this is the only way to achieve any big task, i follow this way..
take a pen and paper, sit and analyse your problem and do some rough calculations on your module, objects, and related dependencies and try to build your stuff,,
nothing wil work by asking for help on whole project or delaying..
slow and steady wins the race..
start doing all the best..
I know your problem and i can build a solution too, but this is not my job. i am not your servent. I am helping others in CP, i like this very much. but there is a limit for everything and you have crossed that.. so i put an end to it.
start working... start coding..
Santosh Kokatnur 16-May-16 1:56am    
ok bro... thanks! i will do it.
C#
DataGridViewImageColumn ic= new DataGridViewImageColumn();
 ic.HeaderText = "Img";
 ic.Image = null;
 ic.Name = "cImg";
 ic.Width = 100;
 DGV.Columns.Add(ic);


 foreach (DataGridViewRow row in DGV.Rows)
 {
   DataGridViewImageCell cell = row.Cells[1] as DataGridViewImageCell;
   cell.Value = (System.Drawing.Image)Properties.Resources.Icon_delete;
 }
 
Share this answer
 
Comments
Santosh Kokatnur 31-Mar-16 5:10am    
using boolean data set, how we can do this?

I mean, value is true, then image will be 1.png, if value is false, image will be 2.png
Sagar Haridas Shinde 31-Mar-16 8:13am    
Give Condition in for loop and according to this assign different two images for particular cell.

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