日期:2013-05-26  浏览次数:20369 次

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
namespace cSharpDataGrid
{
 
 public class MyDataGrid : System.Windows.Forms.DataGrid
 {
  private ArrayList m=new ArrayList();
  Array MultiSelectedIndex
  {
   get
   {
    return this.m.ToArray(typeof (int));
   }
  }
   
  protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
  {
   Console.WriteLine("Datagrid has hit");
   Point _posDg = new Point(e.X,e.Y);
   System.Windows.Forms.DataGrid.HitTestInfo _hitDg =base.HitTest(_posDg);
   if (HitDataGrid(_hitDg))
   {
    base.OnMouseDown(e);
   }
   base.OnMouseDown (e);
  }
  private bool HitDataGrid(DataGrid.HitTestInfo Hit)
  {
   try
   {
    switch (MyDataGrid.ModifierKeys)
    {
     case Keys.Control:
      if (Hit.Row>-1)
      {
       if (m.IndexOf(Hit.Row)>-1)
       {
        m.Remove(Hit.Row);
        this.UnSelect(Hit.Row);
       }
       else
       {
        m.Add(Hit.Row);
        this.Select(Hit.Row);
       }
      }
      return false;
     case Keys.Shift:
      if (Hit.Row>-1)
      {
       foreach (int intIndex in m)              
       {
        this.UnSelect(intIndex);
       }
       m.Clear();
       int _count;
       int _intStep;
       if (Hit.Row>this.CurrentRowIndex)
       {
        _intStep=1;
       }
       else
       {
        _intStep=-1;
       }
       for (_count=this.CurrentRowIndex ;_count==Hit.Row;_count+=_intStep)
       {
        m.Add(_count);
        this.Select(_count);
       }
      }
      return false;
     default:
      foreach (int _intIndex in m)
      {
       this.UnSelect(_intIndex);
      }