C# – Icons in DataGridView

cdatagridviewdatatablewinforms

I have a form with a datagridview control on it.

The datagridview is bound to a DataView which in turn is populated from a DataTable. The app works fine in it's current state, but now I want to modify it so as to introduce a new column in to the datagrridview. The new column is to be an icon.

I am assuming that somehow I need to create a new column for the icon in the DataTable so it can flow through to the datagridview. Any help on how I can achieve this.

Cut down version of the code is

private DataTable _tableDT = new DataTable("dt");
private DataView _viewDT = new DataView();

_viewDT.Table = _tableDT;
_tableDT.Columns.Add("Name", typeof(string));
_tableDT.Columns.Add("Desc", typeof(string));

// populate data table
_tableDT.Rows.Add(.....)

dataGridView1.DataSource = _viewDT;

Also, as an additional question. Can a column have both an icon and text in it. (i.e. ideally the new column would have an icon to the left hand side followed by some text ).

Best Answer

Have a little read of this. It's in VB but it's easy enough to interpret.

http://arsalantamiz.blogspot.com/2008/04/how-to-display-row-icon-in-datagridview.html

Alternatively, if you're reading the image in from a database, you can just get the DataGridView to do all the hard work, simply by adding a new DataGridViewImageColumn column. By overriding the CellPainting event you can specify the image based upon the row data.

http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/4f3c8d15-f1f5-4d62-9f26-8a04f683a210

Hope this helps, Tom :)

Related Topic