R – display images in datagrid with Compact Framework

compact-frameworkdatagrid

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

any tips on that?

Best Answer

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.