Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
I format DatagridView from Entity query.
Now I have code:

C#
List<Cards.Model.Cards> list = new List<Cards.Model.Cards>();
            DateTime timeNow = DateTime.Now; 

            foreach (var obj in BLL.Cards.GetListCards())
            {

                Cards.Model.Cards qwe = new Cards.Model.Cards();
                DateTime timeEnd = BLL.Cards.UnixTimeStampToDateTime(obj.EndDay);

                SetStatusIcon (timeEnd < timeNow ? 1 : 0);

                //qwe.Status = timeEnd < timeNow ? 1 : 0;
                qwe.IdUser = obj.IdUser;  //this is column name
                qwe.Mobile = obj.Mobile;
                qwe.Username = obj.Username;
                qwe.StartDayD = BLL.Cards.UnixTimeStampToDateTime(obj.StartDay);
                qwe.EndDayD = timeEnd;
                list.Add(qwe);
            }
                
                ListCards.DataSource = list;


And function, that change image path and add to Grid image:

 private void SetStatusIcon(int status)
        {

            DataGridViewImageColumn img = new DataGridViewImageColumn();
            string path = status == 1 ? "Active.png" : "NoActive.png";
            string imagepath = "D:\\""+ path;
            Image image = Image.FromFile(imagepath);
            img.Image = image;
            ListCards.Columns.Add(img); //want here to add field with icon for every row of datagridview
            img.HeaderText = "Image";
            img.Name = "img";

        }



My goal is: Set one column Image and for every row of datagridview add one field image with any status (Active.png, NoActive.png)

I actually spent a lot of time so i requested you for help.
Thank you!



I tried also make such after i get filled grid:

C#
foreach (DataGridViewRow row in ListCards.Rows)
                {
                    DataGridViewImageCell cell = row.Cells[1] as DataGridViewImageCell;
                    cell.Value = (System.Drawing.Image)Properties.Resources.Active;  //Here return error: Object reference not set to an instance of an object.
                }


Another way i did not find
Posted
Updated 16-Apr-13 5:08am
v3
Comments
ZurdoDev 16-Apr-13 11:08am    
I don't quite follow what you are saying but I believe you want to use the CellFormatting event. http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx
[no name] 16-Apr-13 11:12am    
No. i use Windows Form C#. I want add to Grid one column Image. And every row this Grid must contains Image (Or Active.png or NoActive.png). This must be form by loop Foreach, where i try make List for adding to ListCards.DataSource = list;
[no name] 16-Apr-13 13:26pm    
I thought that may be simply insert button with background image instead image?!

1 solution

Hi,

Example:

private void createGraphicsColumn()
{
    Icon treeIcon = new Icon(this.GetType(), "tree.ico");
    DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
    iconColumn.Image = treeIcon.ToBitmap();
    iconColumn.Name = "Tree";
    iconColumn.HeaderText = "Nice tree";
    dataGridView1.Columns.Insert(2, iconColumn);
}


Check this^ link for more details.

Cheers,
JAFC
 
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