C # version multiselected DataGrid

Source: Internet
Author: User
Datagrid|select 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);
}
M.clear ();
if (Hit.type==datagrid.hittesttype.rowheader)
{
M.add (Hit.row);
}
return true;
}
}
catch (System.Exception ex)
{
Throw ex;
}
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.