浅谈DataGridTableStyle心得

在开发数据DataGrid显示时候通常会遇到自定义DataGrid的列显示,
比如经常会提到的,
如何单击一行显示某种颜色
如何点击到某个单元格不显示编辑框
如何自定义一列的显示,比如本来有个标记位flag为0,本行背景显示为白色等等,
此类问题归根到底还是要靠 DataGridTableStyle 和 System.Windows.Forms.DataGridColumnStyle 这两个类来完成.

DataGridColumnStyle  是定义一列显示的样式
DataGridTableStyle  是定义整个datagrid的实现样式.
None.gif dts.GridColumnStyles.Clear();//将column的style清除
None.gif            
None.gif            
for  ( int  i = 0 ;i < Boxs.Count;i ++ )
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                dts.GridColumnStyles.Add((DataGridTextBoxColumn)Boxs[i]);//把所有的columnstyle附加上去
ExpandedBlockEnd.gif            }

None.gif
None.gif             
None.gif            Employee_lst.TableStyles.Add(dts);//最后把datatablestyle附加到datagrid上去


还要注意每一个columnstryl都要对应到列,否则会出错.
DatatableStyle也要对应到表.
xxx.MappingName=  yyy;



我们来自己创建一个DataGridColumnStyle 的子类来:
这个子类能够解决:
如何单击一行显示某种颜色
如何点击到某个单元格不显示编辑框

其实这个子类很简单,主要注意看paint是如何完成的.

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Drawing;
None.gif
using  System.Data;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  Stock
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// ButtonColumnStyle2 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ButtonColumnStyle2 : System.Windows.Forms.DataGridColumnStyle
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif        
public  delegate void OnClickEventHandler(object sender,int rowIndex);
InBlock.gif        
public event OnClickEventHandler OnClick;
InBlock.gif
InBlock.gif        
public ButtonColumnStyle2()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 该调用是 Windows.Forms 窗体设计器所必需的。
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何初始化
InBlock.gif
            Invalidate();
InBlock.gif            
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
组件设计器生成的代码#region 组件设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary> 
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器 
InBlock.gif        
/// 修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            components 
= new System.ComponentModel.Container();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
继承datagridcolumnstyle#region 继承datagridcolumnstyle
InBlock.gif        
protected  override void Abort(int rowNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif        
protected  override bool Commit(CurrencyManager dataSource, int rowNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        
//        protected  override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly)
InBlock.gif        
//        {
InBlock.gif        
//            base.Edit (source, rowNum, bounds, readOnly);
InBlock.gif        
//        }
InBlock.gif        
//
InBlock.gif        
//        
InBlock.gif        
//
InBlock.gif        
//        protected  override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText)
InBlock.gif        
//        {
InBlock.gif        
//            
InBlock.gif        
//            //base.Edit (source, rowNum, bounds, readOnly, instantText);
InBlock.gif        
//        }
InBlock.gif        
//
InBlock.gif
        protected  override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
paint#region paint 
InBlock.gif        
protected  override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected  override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected  override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
InBlock.gif            Brush dbrush 
= new SolidBrush(System.Drawing.Color.FromArgb(((byte)230), ((byte)236), ((byte)238))); 
InBlock.gif            
int m_CurrentRow = this.DataGridTableStyle.DataGrid.CurrentRowIndex; 
InBlock.gif            
if (m_CurrentRow == rowNum) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                g.FillRectangle(dbrush, bounds); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                g.FillRectangle(Brushes.White, bounds); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            Font font 
= new Font("宋体",9);
InBlock.gif            DrawText(
"Assign Stock",font,g,bounds,dbrush);
InBlock.gif            
if(OnClick!=null)
InBlock.gif                OnClick(
this,m_CurrentRow);
InBlock.gif
InBlock.gif            
InBlock.gif            
//base.Paint (g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
protected  override int GetPreferredHeight(Graphics g, object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return 20;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected  override Size GetPreferredSize(Graphics g, object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new Size ();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected  override int GetMinimumHeight()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void SetDataGrid(DataGrid value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.SetDataGrid (value);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void DrawText(String Text,Font font,Graphics g,Rectangle bounds,Brush dbrush)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int xMargin,yMargin;
InBlock.gif            xMargin 
= 2;
InBlock.gif            yMargin 
= 2 ;
InBlock.gif            
float penWidth   =  0.5f;
InBlock.gif            
float penWidth2  =  1f;
InBlock.gif
InBlock.gif            g.DrawLine(
new Pen(Brushes.DarkGray,penWidth),new Point(bounds.Left+xMargin,bounds.Top+yMargin),new Point(bounds.Left+xMargin,bounds.Bottom-yMargin));
InBlock.gif            g.DrawLine(
new Pen(Brushes.DarkGray,penWidth),new Point(bounds.Right-xMargin,bounds.Top+yMargin),new Point(bounds.Left+xMargin,bounds.Top+yMargin));
InBlock.gif
InBlock.gif            g.DrawLine(
new Pen(Brushes.DimGray,penWidth2),new Point(bounds.Left+xMargin,bounds.Bottom-yMargin),new Point(bounds.Right-xMargin,bounds.Bottom-yMargin));
InBlock.gif            g.DrawLine(
new Pen(Brushes.DimGray,penWidth2),new Point(bounds.Right-xMargin,bounds.Top+yMargin),new Point(bounds.Right-xMargin,bounds.Bottom-yMargin));
InBlock.gif
InBlock.gif
//            g.FillRectangle(dbrush,bounds.Left+xMargin,bounds.Top+yMargin,bounds.Width-2*xMargin,bounds.Height-2*yMargin);
InBlock.gif

InBlock.gif
InBlock.gif
InBlock.gif            PointF p 
= new PointF(bounds.Left+2,bounds.Top+5);
InBlock.gif            StringFormat FontFormat 
= new StringFormat();
InBlock.gif            FontFormat.Alignment 
= System.Drawing.StringAlignment.Near;
InBlock.gif
InBlock.gif            g.DrawString(Text, font, Brushes.Black,p,FontFormat);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值