datagrid使用技巧(三)
--------如何实现下拉列表
有时候听有些朋友抱怨.net的datagrid不是很好用。就我个人的体会,datagrid的功能非常强大,可以使我们随心所欲的完成各种各样的工作,可惜就是实现起来不够简单明了。我对平时经常碰到的一些问题积累了一些解决的方法,现在把它们总结一下供大家参考。
比较经常碰到的一个问题是:在编辑单元格内容时我们希望出现这样的下拉列表,如图1所示:
图1
思路:
1 写一个类comboform表示下拉列表,类包含两个成员:form窗体和datagrid组件。
2 写一个类nokeyupcombobox(继承combobox),目的是屏蔽wm_keyup消息,避免在按tab键时出现问题。
3 写一个继承于datagridtextboxcolumn的类,命名为datagridcomboformcolumn。在类中加入一个combobox和一个comboform,类实现下面几个功能:
a 编辑单元格内容时显示组件nokeyupcombobox;
b combobox下拉时显示下拉列表comboform;
c 鼠标点击下拉列表时,隐藏comboform并将用户选定的内容写入单元格(当然,你也可以设置其他隐藏下拉列表的操作,比如按回车键);
d 下拉列表comboform不具有焦点时隐藏。
代码:
//comboform类:
public class comboform:form
{
private datagrid datagrid;
public datagrid datagrid
{
get {return datagrid;}
set {datagrid=value;}
}
public comboform()
{
this.formborderstyle=formborderstyle.none;
this.startposition=formstartposition.manual;
datagrid=new datagrid();
this.controls.add(datagrid);
datagrid.dock=dockstyle.fill;
datagrid.captionvisible=false;
}
}
//nokeyupcombobox类:
public class nokeyupcombobox:combobox
{
const int wm_keyup=0x101;
protected override void wndproc(ref message msg)
{
if (msg.msg==wm_keyup)
return;
base.wndproc(ref msg);
}
}
//datagridcomboformcolumn类:
public class datagridcomboformcolumn:datagridtextboxcolumn
{
private nokeyupcombobox combobox;
private currencymanager _source;
private int rownum;
private comboform frm;
public comboform frm
{
get {return frm;}
}
//我们将使用index属性表示单元格内容与下拉列表的第index列的内容相联系
private int index;
public int index
{
get {return index;}
set {index=value;}
}
public datagridcomboformcolumn()
{
frm=new comboform();
combobox=new nokeyupcombobox();
frm.deactivate+=new eventhandler(frm_deactive);
frm.datagrid.click+=new eventhandler(grid_click);
this.combobox.dropdown+=new eventhandler(combobox_dropdown);
this.combobox.leave+=new eventhandler(combobox_leave);
}
//combobox不具有焦点时隐藏
private void combobox_leave(object sender,eventargs e)
{
combobox.visible=false;
}
//下拉列表--frm不具有焦点时隐藏
private void frm_deactive(object sender,eventargs e)
{
frm.hide();
combobox.visible=false;
}
//combobox下拉时显示下拉列表--frm
private void combobox_dropdown(object sender,eventargs e)
{
//在这里您还可以根据下拉列表的长与宽是否超出屏幕设置下拉列表的位置
frm.left=combobox.pointtoscreen(new point(0,combobox.height)).x;
frm.top=combobox.pointtoscreen(new point(0,combobox.height)).y;
frm.show();
frm.bringtofront();
}
//点击下拉列表的datagrid时,将选中的内容写入单元格并隐藏下拉列表--frm
private void grid_click(object sender,eventargs e)
{
bindingmanagerbase cm=frm.bindingcontext[frm.datagrid.datasource, frm.datagrid.datamember];
combobox.text=((datarowview)cm.current)[index].tostring();
this.textbox.text=((datarowview)cm.current)[index].tostring();
frm.hide();
combobox.visible=false;
this.setcolumnvalueatrow(_source,rownum,this.textbox.text);
}
//重载edit方法,使用combobox代替textbox
protected override void edit(currencymanager datasource,int rownum,rectangle bounds,bool readonly,string instanttext,bool cellvisible)
{
base.edit(datasource,rownum,bounds,readonly,instanttext,cellvisible);
combobox.parent=this.textbox.parent;
combobox.left=this.textbox.left-2;
combobox.top=this.textbox.top-2;
combobox.size=new size(this.textbox.width,this.combobox.height);
combobox.text=this.textbox.text;
this.textbox.visible=false;
combobox.visible=true;
combobox.bringtofront();
combobox.focus();
_source=datasource;
this.rownum=rownum;
}
}
下面的例子说明了如何使用datagridcombofrom类:
新建一个windows 应用程序,加入sqlconnection,连接sql数据库northwind,加入下面代码。
private void form1_load(object sender, system.eventargs e)
{
sqldataadapter da=new sqldataadapter("select productname from products",this.sqlconnection1);
dataset ds=new dataset();
da.fill(ds,"products");
dataset ds_combo=new dataset();
da.selectcommand=new sqlcommand("select productname,quantityperunit,unitprice from products",this.sqlconnection1);
da.fill(ds_combo,"products");
datagridtablestyle dts=new datagridtablestyle();
dts.mappingname="products";
mydatagridcolumn col=new mydatagridcolumn();
col.mappingname="productname";
col.width=100;
col.index=0;
col.headertext="productname";
col.frm.datagrid.datasource=ds_combo;//设置下拉列表的数据源
col.frm.datagrid.datamember="products";
dts.gridcolumnstyles.add(col);
this.datagrid1.tablestyles.add(dts);
this.datagrid1.setdatabinding(ds,"products");
}