C#版Multiselected DataGrid
2024-07-21 02:17:49
供稿:网友
,欢迎访问网页设计爱好者web开发。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;
}
}
}
}