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 = 

// create column style for a text column
GridTextColumn = new DataGridTextBoxColumn(pcol["Description"]);
GridTextColumn.MappingName = "Description";
GridTextColumn.HeaderText = "Icon-Name";
GridTextColumn.Width = 200;

// create column style for an image column
GridBmpColumn = new DataGridImageColumn(pcol["Images"]);
GridBmpColumn.MappingName = "Images";
GridBmpColumn.HeaderText = "Bitmap";
GridBmpColumn.Width = 100;

// add table style to the data grid

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


Download - 58 Kb


  • 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

  • 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

  • Thanks

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

    Just what I needed. Very clear documentation

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

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

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

  • 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;

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

  • How can I usea ComboBox in a DataGrid ?

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

    Originally posted by: LuckyMIC


    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,

  • Thanks

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

    Originally posted by: lamar

    Help me with some code I was working on, Thanks

  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • As the mobile enterprise marketplace expands and customer needs grow more diverse, Samsung recognizes that solution partners and developers play an essential role by continually innovating to meet their customers' needs. Samsung works to provide these developers and partners with the latest tools and resources needed to create these solutions. Read this program guide to learn how the Samsung Enterprise Alliance Program provides partners and developers with Samsung enterprise software development kits (SDKs) …

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date