DataGrid图标列的定义

it2026-02-18  12

        如何在DataGrid中某一列既有图标又有数据,而且能够实现什么样的数据显示什么样的图标。对于这样的列,就只能是自己重定义一个DataGridColumn了(在2.0中有一个DataGridImageColumn,但是它好像还只能显示图标,对于数据没有办法显示,说真的2.0的变化有点大,很多东西还不会用)。在这里也定义了一个 DataGridImageColumn,继承DataGridTextBoxColumn。主要功能是能够显示指定的图标,并且也能够显示对应的数据信息。         为了显示的图标不在DataGridColumn中定死,故先定义了一个委托

1 public   delegate   void  FormatImageCellEventHandler( object  sender, DataGridImageCellEventArgs e); 和 DataGridImageCellEventArgs public   class  DataGridImageCellEventArgs:EventArgs      { //         private string _column; //         private int _row; //需要显示的图片信息         private Image _image;         public DataGridImageCellEventArgs(string ColumnName,int RowNum)         {             this.Column = ColumnName;             this.Row = RowNum;         }         public Image Image         {             get             {                 return _image;             }             set             {                 _image =  value;             }         }         public string Column         {             get             {                 return _column;             }             set             {                 _column = value;             }         }         public int Row         {             get             {                 return _row;             }             set             {                 _row = value;             }         }     }

注意这里的图片格式是 16×16

再实现DataGridImageColumn

public   class  DataGridImageBarColumn:System.Windows.Forms.DataGridTextBoxColumn      {         private int _rowNum=-1;         public DataGridImageBarColumn():base()         {         }         /**//// <summary>         /// 设置列图片信息         /// </summary>         public event FormatImageCellEventHandler SetImage;         protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)         {                  Rectangle rect = bounds;             if(this.SetImage!=null)             {                 DataGridImageCellEventArgs ei = new DataGridImageCellEventArgs(this.MappingName,rowNum); //通过这个事件获取显示的图片信息                 this.SetImage(this,ei); //                this.TextBox.Left = bounds.Location.X+(ei.Number+1)*16; //                this.TextBox.Width = bounds.Width - (ei.Number+1)*16;                 rect =new Rectangle(bounds.Location.X+16,                     bounds.Location.Y,bounds.Width-(ei.Number+1)*16,bounds.Height);             }             base.Edit(source,rowNum, rect, readOnly, instantText , cellIsVisible);                          _rowNum = rowNum;         }              protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)         {             DataGridImageCellEventArgs ei = new DataGridImageCellEventArgs(this.MappingName,rowNum);             if(this.SetImage!=null)             {                 this.SetImage(this,ei);             }             DataRowView drv = source.List[rowNum] as DataRowView;             if(drv == null)                 return;             Rectangle rect =bounds;             if(!drv.Row.IsNull(this.MappingName))             {                 int leftX = 16;                 g.FillRectangle(Brushes.White,bounds);                 g.DrawImage(ei.Image,bounds.Location.X+leftX,bounds.Location.Y);                 rect = new Rectangle(bounds.X+16+leftX,bounds.Y+2,bounds.Width-17-leftX,bounds.Height);             }                             base.Paint(g, rect, source, rowNum, backBrush, foreBrush, alignToRight);         }     } 这样在定义DataGridColumnStyle时可以定义这个列信息,犹如定义DataGridTextColumn,但是要比定义DataGridTextClumn多一个 SetImage事件的定义 ,这个事件是获取图片信息的。         在下一篇中准备再次重写这个类,来实现DataGrid中 树的概念。和树一样可以有一种层叠的感觉。

转载于:https://www.cnblogs.com/xiaodele/archive/2005/06/01/166337.html

相关资源:C# DataGridView里添加小图标
最新回复(0)