zoukankan      html  css  js  c++  java
  • 浅谈DataGridTableStyle心得

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

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


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


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



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

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

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

    namespace Stock
    {
        
    /// <summary>
        
    /// ButtonColumnStyle2 的摘要说明。
        
    /// </summary>

        public class ButtonColumnStyle2 : System.Windows.Forms.DataGridColumnStyle
        
    {
            
    /// <summary> 
            
    /// 必需的设计器变量。
            
    /// </summary>

            private System.ComponentModel.Container components = null;
            
    public  delegate void OnClickEventHandler(object sender,int rowIndex);
            
    public event OnClickEventHandler OnClick;

            
    public ButtonColumnStyle2()
            
    {
                
    // 该调用是 Windows.Forms 窗体设计器所必需的。
                InitializeComponent();

                
    // TODO: 在 InitializeComponent 调用后添加任何初始化
                Invalidate();
                

            }


            
    /// <summary> 
            
    /// 清理所有正在使用的资源。
            
    /// </summary>

            protected override void Dispose( bool disposing )
            
    {
                
    if( disposing )
                
    {
                    
    if(components != null)
                    
    {
                        components.Dispose();
                    }

                }

                
    base.Dispose( disposing );
            }


            
    组件设计器生成的代码

            
    继承datagridcolumnstyle

            
    private void DrawText(String Text,Font font,Graphics g,Rectangle bounds,Brush dbrush)
            
    {
                
    int xMargin,yMargin;
                xMargin 
    = 2;
                yMargin 
    = 2 ;
                
    float penWidth   =  0.5f;
                
    float penWidth2  =  1f;

                g.DrawLine(
    new Pen(Brushes.DarkGray,penWidth),new Point(bounds.Left+xMargin,bounds.Top+yMargin),new Point(bounds.Left+xMargin,bounds.Bottom-yMargin));
                g.DrawLine(
    new Pen(Brushes.DarkGray,penWidth),new Point(bounds.Right-xMargin,bounds.Top+yMargin),new Point(bounds.Left+xMargin,bounds.Top+yMargin));

                g.DrawLine(
    new Pen(Brushes.DimGray,penWidth2),new Point(bounds.Left+xMargin,bounds.Bottom-yMargin),new Point(bounds.Right-xMargin,bounds.Bottom-yMargin));
                g.DrawLine(
    new Pen(Brushes.DimGray,penWidth2),new Point(bounds.Right-xMargin,bounds.Top+yMargin),new Point(bounds.Right-xMargin,bounds.Bottom-yMargin));

    //            g.FillRectangle(dbrush,bounds.Left+xMargin,bounds.Top+yMargin,bounds.Width-2*xMargin,bounds.Height-2*yMargin);



                PointF p 
    = new PointF(bounds.Left+2,bounds.Top+5);
                StringFormat FontFormat 
    = new StringFormat();
                FontFormat.Alignment 
    = System.Drawing.StringAlignment.Near;

                g.DrawString(Text, font, Brushes.Black,p,FontFormat);

            }

        }

    }


  • 相关阅读:
    [转]谈谈技术原则,技术学习方法,代码阅读及其它
    ImageMagick 详细安装使用 linux (jmagick)
    Sql Server中的表访问方式Table Scan, Index Scan, Index Seek
    在SQL Server中创建用户角色及授权
    LINQ to SQL:创建你的第一个程序
    Sql Server Statement Output
    内置系统账户:Local system/Network service/Local Service 区别
    SQL Server的备份
    SQL Server 使用Agent自动备份数据库
    将sql server 2000的备份文件导入到sql server 2012中
  • 原文地址:https://www.cnblogs.com/king_astar/p/76266.html
Copyright © 2011-2022 走看看