• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

令狐冲和酒壶

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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

 }
}

posted on 2006-08-02 17:12  我不是冷狐冲,我就是一酒壶  阅读(582)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3