Images in a Data Grid
Environment: .NET Beta 2
This article tells you how to get images in a data grid
If you try to display bitmaps in a data grid, you see only a text message for the bitmap. For displaying bitmaps, you must create an own table style for your data grid. Furthermore you need a 'DataGridImageColumn' class for displaying bitmaps in the datagrid. The following code shows how to create your own table style. A DataSet with a table 'Test' is defined previously:
DataGridTableStyle DGStyle; DataGridColumnStyle GridTextColumn, GridBmpColumn; DGStyle = new DataGridTableStyle(); // select table DGStyle.MappingName = "Test"; // get the PropertyDescriptorCollection for the data // source and data member. PropertyDescriptorCollection pcol = this.BindingContext[DBDataSet, "Test"].GetItemProperties(); // create column style for a text column GridTextColumn = new DataGridTextBoxColumn(pcol["Description"]); GridTextColumn.MappingName = "Description"; GridTextColumn.HeaderText = "Icon-Name"; GridTextColumn.Width = 200; DGStyle.GridColumnStyles.Add(GridTextColumn); // create column style for an image column GridBmpColumn = new DataGridImageColumn(pcol["Images"]); GridBmpColumn.MappingName = "Images"; GridBmpColumn.HeaderText = "Bitmap"; GridBmpColumn.Width = 100; DGStyle.GridColumnStyles.Add(GridBmpColumn); // add table style to the data grid dataGrid1.TableStyles.Add(DGStyle); // connect grid with database dataGrid1.DataSource = DBDataSet; dataGrid1.DataMember = DBDataSet.Tables["Test"].TableName;
The 'PropertyDescriptorCollection' do the data binding to the columns of the data grid. After creating an own column style for each column, you add the styles to your data grid. Now, you must connect the data grid with your data base and implement your own 'DataGridImageColumn' class.
The 'DataGridImageColumn' class is inherited from the PictureBox class. For a valid DataGridColumn class you must override the methods: Abort, Commit, Edit, GetMinimumHeight, GetPreferredHeight, GetPreferredSize and the Paint routines (see the code for the 'DataGridImageColumn' class in the example). The Paint routine do the hole work. With the GetColumnAtRow method you get the field data of the actual data grid cell. After extracting the bitmap from the OLE container, you can draw the image at the given position.
Remark: In ADO.NET (or maybe generally in OLE fields) the fields are limited to a maximum of 8000 Bytes, so Bitmap size must be smaller than 7928 Bytes (8000Bytes - 72Bytes (Container)).

Comments
Image Column overlaps Text Column in Datagrid
Posted by rinilya on 03/10/2008 05:36amHi Iam using VB.NET 2003 with Access Databse to retrieve images and display it in the grid.My Problem Image column overlaps text column. Could any one help me.....It is very Urgent...............
-
ReplyLink for ur question
Posted by hashim on 06/21/2012 11:10pmhttp://www.codeguru.com/csharp/csharp/cs_controls/datagrid/article.php/c4783/Images-in-a-Data-Grid.htm
ReplyUsable in Drag and Drop?
Posted by RABB17 on 08/14/2006 01:46pmCould this be implemented for a drag and drop where the images come from a file instead of from a database? If so, what changes would need to be made? Thanks, RABB17
ReplyThanks
Posted by Mint81 on 06/08/2005 01:09amJust what I needed. Very clear documentation
ReplyHow can I Edit a Datagrid in C# for Pocket PC
Posted by Legacy on 06/30/2003 12:00amOriginally posted by: Cesar Ruiz
I'm using Visual Studio 2003.
ReplyI have an error;
Posted by Legacy on 05/09/2003 12:00amOriginally posted by: melon
when I builder the project, the IDE gives me an error --- "文件"Resource.resx"的资源转换失败。无效的 ResX 输入。"
ReplyI translate it to English
"fail to convert the resourses in the file of 'Resource.resx'.it is invaliuable to input Rexs.
Components as containers.
Posted by Legacy on 12/09/2002 12:00amOriginally posted by: sushil
How can I include one windows component inside another. For instance, how can I include a Treeview control inside a combo-box, and place that combo-box inside a cell in a data grid?
ReplyComboBox in DataGrid
Posted by Legacy on 11/27/2002 12:00amOriginally posted by: Saroj
ReplyI want to draw Rich Text in the Grid Cell
Posted by Legacy on 05/09/2002 12:00amOriginally posted by: qiongwen
I want to draw Rich Text in the Grid Cell, how to do it?
ReplyMust I implement the Column style myself? Are there any better ways to do that?
How can I usea ComboBox in a DataGrid ?
Posted by Legacy on 04/25/2002 12:00amOriginally posted by: LuckyMIC
Hi...
How can I use a ComboBox as a DataGridColumn ? I want to inherit the DataGridColumn to define my own Column, but how can I add my own Item ?
Thanks in advance,
ReplyMIC
Thanks
Posted by Legacy on 04/11/2002 12:00amOriginally posted by: lamar
Help me with some code I was working on, Thanks
ReplyLoading, Please Wait ...