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)).

Downloads

Download - 58 Kb


Comments

  • Image Column overlaps Text Column in Datagrid

    Posted by rinilya on 03/10/2008 05:36am

    Hi 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...............

    • Link for ur question

      Posted by hashim on 06/21/2012 11:10pm

      http://www.codeguru.com/csharp/csharp/cs_controls/datagrid/article.php/c4783/Images-in-a-Data-Grid.htm

      Reply
    Reply
  • Usable in Drag and Drop?

    Posted by RABB17 on 08/14/2006 01:46pm

    Could 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

    Reply
  • Thanks

    Posted by Mint81 on 06/08/2005 01:09am

    Just what I needed. Very clear documentation

    Reply
  • How can I Edit a Datagrid in C# for Pocket PC

    Posted by Legacy on 06/30/2003 12:00am

    Originally posted by: Cesar Ruiz

    I'm using Visual Studio 2003.


    Reply
  • I have an error;

    Posted by Legacy on 05/09/2003 12:00am

    Originally posted by: melon

    when I builder the project, the IDE gives me an error --- "文件"Resource.resx"的资源转换失败。无效的 ResX 输入。"
    I translate it to English
    "fail to convert the resourses in the file of 'Resource.resx'.it is invaliuable to input Rexs.

    Reply
  • Components as containers.

    Posted by Legacy on 12/09/2002 12:00am

    Originally 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?

    Reply
  • ComboBox in DataGrid

    Posted by Legacy on 11/27/2002 12:00am

    Originally posted by: Saroj

    Set the visible property to false of a combobox at design time. then add the following code to the datagrid_currentcellchanged event.
    
    


    comboBox1.Visible = true;
    Rectangle rect;
    rect = dataGrid1.GetCurrentCellBounds();
    comboBox1.Width = rect.Width;
    comboBox1.Height = rect.Height;
    comboBox1.Left = rect.Left + dataGrid1.Left;
    comboBox1.Top = rect.Top + dataGrid1.Top;

    Reply
  • I want to draw Rich Text in the Grid Cell

    Posted by Legacy on 05/09/2002 12:00am

    Originally posted by: qiongwen

    I want to draw Rich Text in the Grid Cell, how to do it?
    Must I implement the Column style myself? Are there any better ways to do that?

    Reply
  • How can I usea ComboBox in a DataGrid ?

    Posted by Legacy on 04/25/2002 12:00am

    Originally 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,
    MIC

    Reply
  • Thanks

    Posted by Legacy on 04/11/2002 12:00am

    Originally posted by: lamar

    Help me with some code I was working on, Thanks

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

  • In the competitive marketplace that surrounds us today, customers shouldn't have to settle for legacy desktop or application delivery simply because they've relied on a certain vendor in the past. This white paper reviews how three customers decided to partner with VMware, and how they benefited from the latest VDI and app trends to improve the end-user experience, increase productivity, reliability and stability to deliver better SLAs - with lower cost and less time needed to manage end users.

Most Popular Programming Stories

More for Developers

RSS Feeds