C# C/S下DataGrid根据数据库里的值(int型)显示ImageList里的图片

    由于做项目需要写了一个显示图片的自定的DataGridColumn样式,抛砖引玉了

using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace DataGridPictureBoxColumn
{

 public class DataGridPictureBoxColumn : DataGridColumnStyle
 {
  private ImageList _showimagelist = null;

  public ImageList ShowImageList
  {
   set
   {
    _showimagelist = value;
   }
  }


  public DataGridPictureBoxColumn() : base()
  {
  }

  protected override void Paint(Graphics g,
   Rectangle bounds,
   CurrencyManager source,
   int rowNum)
  {
   Paint(g, bounds, source, rowNum, false);
  }
  protected override void Paint(
   Graphics g,
   Rectangle bounds,
   CurrencyManager source,
   int rowNum,
   bool alignToRight)
  {
   Paint(
    g,bounds,
    source,
    rowNum,
    Brushes.Red,
    Brushes.Blue,
    alignToRight);
  }
  protected override void Paint(
   Graphics g,
   Rectangle bounds,
   CurrencyManager source,
   int rowNum,
   Brush backBrush,
   Brush foreBrush,
   bool alignToRight)
  {
   int Index = (int)GetColumnValueAtRow(source, rowNum);
   g.FillRectangle(new SolidBrush(Color.White),bounds);
   g.DrawImage(this._showimagelist.Images[Index],bounds);
  }

 

 
  protected override void Abort(int rowNum)
  {
   Invalidate();
  }

  protected override bool Commit
   (CurrencyManager dataSource, int rowNum)
  {
   Invalidate();

   
   return true;
  }

  protected override void Edit(
   CurrencyManager source,
   int rowNum,
   Rectangle bounds,
   bool readOnly,
   string instantText,
   bool cellIsVisible)
  {
   return;
  }

  protected override Size GetPreferredSize(
   Graphics g,
   object value)
  {
   return new Size(20,20);
  }

  protected override int GetMinimumHeight()
  {
   return 20;
  }

  protected override int GetPreferredHeight(Graphics g,
   object value)
  {
   return 20;
  }

 }
}

你可能感兴趣的:(datagrid)