• 重写DataGrid的DataGridBoolColumn,添加bool值改变事件。


    首先新建一个类:DataGridBoolColumnPlus,继承System.Windows.Forms.DataGridBoolColumn
    然后定义事件。此事件可以返回当前单元格的内容
    /**
     * UtilityControl.DataGrid 类库
     * 
     * 整理编写:99StAr,Email:sunhuangmin@163.com 电话:13576017263
     *
     * 最后更新:2005-10-11
     
    */


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

    namespace UtilityControl.DataGrid
    {
     
    /// <summary>
     
    /// DataGridBoolColumnPlus 的摘要说明。
     
    /// </summary>

     public class DataGridBoolColumnPlus : System.Windows.Forms.DataGridBoolColumn
     
    {
      
    public event BoolValueChangedEventHandler BoolValueChanged;
      
      
    bool saveValue;
      
    int saveRow ;
      
    bool lockValue;
      
    bool beingEdited;
      
    public const int VK_SPACE = 32 ;// 0x20

      [System.Runtime.InteropServices.DllImport(
    "user32.dll")]
      
    static extern short GetKeyState(int nVirtKey);
      
      
    public DataGridBoolColumnPlus()
      
    {
       saveValue 
    = false;
       saveRow 
    = -1;
       lockValue 
    = false;
       beingEdited 
    = false
      }


      
    protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
      
    {
       lockValue 
    = true;
       beingEdited 
    = true;
       saveRow 
    = rowNum;
       saveValue 
    = (boolbase.GetColumnValueAtRow(source, rowNum);
       
    base.Edit(source, rowNum,  bounds, readOnly, instantText, cellIsVisible);
      }

      
      
    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)
      
    {   
       Point mousePos 
    = this.DataGridTableStyle.DataGrid.PointToClient(Control.MousePosition);   
       System.Windows.Forms.DataGrid dg 
    = this.DataGridTableStyle.DataGrid;
       
    bool isClickInCell = ((Control.MouseButtons == MouseButtons.Left) && 
        dg.GetCellBounds(dg.CurrentCell).Contains(mousePos) );

       
    bool changing = dg.Focused && ( isClickInCell 
        
    || GetKeyState(VK_SPACE) < 0 ); //如果是空格
       
       
    if(!lockValue && beingEdited && changing && saveRow == rowNum)
       
    {    
        saveValue 
    = !saveValue;
        lockValue 
    = false;    
        
    //事件激发
        if(BoolValueChanged != null)
        
    {
         BoolValueChangedEventArgs e 
    = new BoolValueChangedEventArgs(rowNum, saveValue);
         BoolValueChanged(
    this, e);
        }

       }

       
    if(saveRow == rowNum)
        lockValue 
    = false
     
       
    base.Paint( g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
      }


      
    protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum)
      
    {
       lockValue 
    = true;
       beingEdited 
    = false;
       
    return base.Commit(dataSource, rowNum);
      }


     }
     


    /// <summary>
     
    /// 
     
    /// </summary>

     public class BoolValueChangedEventArgs : EventArgs
     
    {   
      
    private int _row;
      
    private bool _value;

      
    /// <summary>
      
    /// 
      
    /// </summary>
      
    /// <param name="row"></param>
      
    /// <param name="val"></param>

      public BoolValueChangedEventArgs(int row, bool val)
      
    {
       _row 
    = row;    
       _value 
    = val;
      }
     
      
    /// <summary>
      
    /// 
      
    /// </summary>

      public int Row
      
    {
       
    getreturn _row;}
       
    set{ _row = value;}
      }

      
    /// <summary>
      
    /// 
      
    /// </summary>

      public bool BoolValue
      
    {
       
    getreturn _value;}
       
    set{ _value = value;}
      }

     }


     
    /// <summary>
     
    /// 
     
    /// </summary>

     public delegate void BoolValueChangedEventHandler(object sender, BoolValueChangedEventArgs e);

    }

  • 相关阅读:
    Bayesian CTR Prediction for Bing
    Gaussian and Truncated Gaussian
    An Introduction to Variational Methods (5.3)
    An Introduction to Variational Methods (5.2)
    An Introduction to Variational Methods (5.1)
    Variational Bayes
    微软的一篇ctr预估的论文:Web-Scale Bayesian Click-Through Rate Prediction for Sponsored Search Advertising in Microsoft’s Bing Search Engine。
    第五十二课、命令行参数的应用------------------狄泰软件学院
    第五十一课、程序中的配置文件------------------狄泰软件学院
    第五十课、关于对话框(About)------------------狄泰软件学院
  • 原文地址:https://www.cnblogs.com/ami/p/455681.html
Copyright © 2020-2023  润新知