Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi there,

I have a relational database, that I want to display in a DataGridView.
In that db, I have 2 Columns, some kind of a 'flag' (e.g. a processflag, -1 is not started yet, 0 is in progress, and 1 -process completed) - these flag I want do display in my DataGridView as an simple image.

My current solution is quite ugly.

Each Row, captured by a SQLDataReader is put through a Delegate into a method, which checks the row about the flag, and puts the row (incl. the image, selected of a imagelist) into object-List.
Afterwards, this object-List adds a new Row into the database.

This is my current SQLDataReader:

SqlCommand cmd = new SqlCommand(strQuery.ToString(), dbCls.sqlConnection);
                   SqlDataReader dr = cmd.ExecuteReader();
                   while (dr.Read())
                       valuesForDgV = new List<string>();
                       fillDgVDelegate dgvDelegate = new fillDgVDelegate(addData2DgV);
                       dgv_monthView.Invoke(dgvDelegate, valuesForDgV);

private void addData2DgV(List<string> valueArray)
object flgProcess, flgArc = null;

            //select the images for the column in the dgv
            if(valueArray[8] == "0" || valueArray[8] == short.MaxValue.ToString())
                flgProcess = imageList1.Images[0];
                flgProcess = imageList1.Images[1];

            if (valueArray[7] == "0" || valueArray[7] == short.MaxValue.ToString())
                flgArc = imageList1.Images[0];
                flgArc = imageList1.Images[1];

            dgv_monthView.Rows.Add(valueArray[0], betrag, valueArray[2], valueArray[3], valueArray[4], valueArray[5], flgWerbung, flgTermin, flgRechnung);

i don't know if it't an issue of the performance of my computer, but after, i guess 20 rows, the performance is going worse and worse.

So i've tried working with SQLDataAdapter, DataTable and BindingSource.

But I cannot use the Image in my DataGridView. the Column allows only Int-Values

What I need now, is a hint, a tip, may be a codesnippet, how i can solve this problem better.

I'm sorry, for my bad english and I hope you can understand my problem.

Thanks in advance,

I hope these screenshots will better show my problem:

this is what my database looks like:


and this is, how I want to display the highlighted "flags" as an image, seen as the last three columns in my grid


So, I want the '0' Value to be an stop sign in the grid, and the 1-Value is the checked-sign.
Updated 5-May-12 11:17am
NourBerro 5-May-12 16:51pm    
I thing your problem is very simple but actually i couldn't understand it very well,
maybe i can help you if u give me your solution or an example to fix it and give it back to u
Lauryx 5-May-12 17:19pm    
my solution has become quite complex, so an easy sharing at the moment is not possible. But I've addes to screenshots from my database and from the grid, how it should lool like. I hope this helps? If not, I will prepare my solution for sharing.
Zoltán Zörgő 5-May-12 17:20pm    
Have you looked on MSDN?
Lauryx 5-May-12 17:29pm    
I had, my problem is not to add an image Column to the grid, my problem is the performance, with my current solution above.

Is it possible, to load all the data from the database into an DataTable with the SQLDataAdapter and change the columttype from integer to an image-type?
After that bind the datagridview to this datatable with the bindingsource..
Zoltán Zörgő 5-May-12 18:04pm    
Of course you will encounter performance problems, you have a lot of overhead. Dont load too much data into memory yourself!
Use the DataAdapter and let the framework do the fetching optimization. But I suppose you cannot change the type of a column that way. But you can hide the original fields and add your own image columns. After that, you might have several possibilities to change the icons. I have not tried it yet, but here is the simplest I found:

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
// Check that the received cell is in the image column that I want to change.
if (e.ColumnIndex == "3")
e.Value = new ImageResourceHandle("MyGif.gif");
Of course, you have to add your own business logic. See:

1 solution

Ok, i've found a simple solution, may be it's not the perfekt perfect way, but at least quick and dirty.

Befor the data from the database is being collected, I set the visibility-value of my datagrid to false

DataGridView1.Visible = false;

after all collection processes I set the value back to 'true'.

Now my Performance problems are gone..

But thanks a lot for your help, they pushed me to the right way to search!
Share this answer
Shahin Khorshidnia 6-May-12 6:16am    
Thank you.

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