views:

1084

answers:

5

is it possible to display an image in a datagrid cell? i'm currently working with compact framework 3.5.

any tips on that?

+1  A: 

The only way that I know how to do this would be like the trick for rendering textboxes in the grid by using a trick to paint some images over the grid.

One of the CF team posted something about customising the grid on their site.

Sean Molam
A: 

If you are able to use a third-party solution, have a look at Resco SmartGrid.

tomlog
+1  A: 

It's the same process as doing multiline rows, changing row colors, making the text right-to-left, or highlighting a row: you have to custom draw.

ctacke
+2  A: 

Like the other posters have commented, you're required to roll your own. Luckily, this isn't too difficult.

In my application, I needed a way to draw a 16x16 icon in a particular column. I created a new class that inherits from DataGridColumnStyle, which makes it easy to apply to a DataGrid via a DataGridTableStyle object.

class DataGridIconColumn : DataGridColumnStyle {

public Icon ColumnIcon {
 get;
 set;
}

protected override void Paint( Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight ) {

 // Fill in background color
 g.FillRectangle( backBrush, bounds );

 // Draw the appropriate icon
 if (this.ColumnIcon != null) {
  g.DrawIcon( this.ColumnIcon, bounds.X, bounds.Y );
 }
  }
}

You can see that I defined the public property ColumnIcon so I can specify the icon I need to display outside of this class.

Now, to actually use it on a DataGrid, you'd have a snippet like:

DataGridTableStyle ts = new DataGridTableStyle();

DataGridIconColumn dgic = new DataGridIconColumn();
dgic.ColumnIcon = Properties.Resources.MyIcon;
dgic.MappingName = "<your_column_name>";
dgic.HeaderText = "<your_column_header>";

ts.GridColumnStyles.Add(dgic);

this.myDataGrid.TableStyles.Add( ts );

That's a pretty simple example for applying the DataGridTableStyle -- I actually do a lot of further customization on the rest of my DataGrid columns. But it should get you started on what you want to do.

CBono
A: 

http://www.cf-technologies.net/compactgrid.php. You can use cell's CustomDraw event..

related questions