首页 > 开发 > 综合 > 正文

捕捉DataGrid的双击事件(C#版本)

2024-07-21 02:18:33
字体:
来源:转载
供稿:网友
namespace datagriddoubleclick
{
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;

public class form1 : system.windows.forms.form
{
private system.windows.forms.datagrid datagrid1;
private dataset mydataset;
datetime gridmousedowntime;
private system.windows.forms.label label1;

private system.componentmodel.container components = null;

public form1()
{
initializecomponent();
gridmousedowntime = datetime.now;
setup();
}

private void setup()
{
// 用2个table和1和relation创建dataset
makedataset();
// 数据绑定
datagrid1.setdatabinding(mydataset, "customers");

//添加样式
addcustomdatatablestyle();
}

private void makedataset()
{
// 创建dataset.
mydataset = new dataset("mydataset");

// 创建2个datatables.
datatable tcust = new datatable("customers");

// 创建两个列,并添加到第一个表
datacolumn ccustid = new datacolumn("custid");
datacolumn ccustname = new datacolumn("custname");
datacolumn ccurrent = new datacolumn("custcity");
tcust.columns.add(ccustid);
tcust.columns.add(ccustname);
tcust.columns.add(ccurrent);

// 把tables添加到dataset.
mydataset.tables.add(tcust);


/* 计算tables.对每个客户,创建datarow变量 */
datarow newrow1;

// 添加记录到 customers table.
for(int i = 1; i < 4; i++)
{
newrow1 = tcust.newrow();
newrow1["custid"] = (100*i).tostring();
tcust.rows.add(newrow1);
}

tcust.rows[0]["custname"] = "【孟宪会之精彩世界】";
tcust.rows[1]["custname"] = "net_lover";
tcust.rows[2]["custname"] = "http://xml.sz.luohuedu.net/";


tcust.rows[0]["custcity"] = "北京";
tcust.rows[1]["custcity"] = "上海";
tcust.rows[2]["custcity"] = "河南";
}

private void addcustomdatatablestyle()
{
datagridtablestyle ts1 = new datagridtablestyle();
ts1.mappingname = "customers";
// 设置属性
ts1.alternatingbackcolor = color.lightgray;

// 添加textbox列样式,以便我们捕捉鼠标事件
datagridtextboxcolumn textcol = new datagridtextboxcolumn();
textcol.mappingname = "custid";
textcol.headertext = "序号";
textcol.width = 100;

//添加事件处理器
textcol.textbox.mousedown += new mouseeventhandler(textboxmousedownhandler);
textcol.textbox.doubleclick += new eventhandler(textboxdoubleclickhandler);
ts1.gridcolumnstyles.add(textcol);

textcol = new datagridtextboxcolumn();
textcol.mappingname = "custname";
textcol.headertext = "姓名";
textcol.width = 100;
//添加事件处理器
textcol.textbox.mousedown += new mouseeventhandler(textboxmousedownhandler);
textcol.textbox.doubleclick += new eventhandler(textboxdoubleclickhandler);
ts1.gridcolumnstyles.add(textcol);

textcol = new datagridtextboxcolumn();
textcol.mappingname = "custcity";
textcol.headertext = "地址";
textcol.width = 100;
//添加事件处理器
textcol.textbox.mousedown += new mouseeventhandler(textboxmousedownhandler);
textcol.textbox.doubleclick += new eventhandler(textboxdoubleclickhandler);
ts1.gridcolumnstyles.add(textcol);

datagrid1.tablestyles.add(ts1);

}

protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}

#region windows form designer generated code
private void initializecomponent()
{
this.datagrid1 = new system.windows.forms.datagrid();
this.label1 = new system.windows.forms.label();
((system.componentmodel.isupportinitialize)(this.datagrid1)).begininit();
this.suspendlayout();
//
// datagrid1
//
this.datagrid1.captionbackcolor = system.drawing.systemcolors.info;
this.datagrid1.captionforecolor = system.drawing.systemcolors.windowtext;
this.datagrid1.captionvisible = false;
this.datagrid1.datamember = "";
this.datagrid1.headerforecolor = system.drawing.systemcolors.controltext;
this.datagrid1.location = new system.drawing.point(11, 9);
this.datagrid1.name = "datagrid1";
this.datagrid1.size = new system.drawing.size(368, 144);
this.datagrid1.tabindex = 0;
this.datagrid1.mousedown += new system.windows.forms.mouseeventhandler(this.datagrid1_mousedown);
//
// label1
//
this.label1.location = new system.drawing.point(4, 166);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(383, 23);
this.label1.tabindex = 1;
this.label1.textalign = system.drawing.contentalignment.middlecenter;
this.label1.click += new system.eventhandler(this.form1_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(5, 13);
this.clientsize = new system.drawing.size(387, 201);
this.controls.addrange(new system.windows.forms.control[] {
this.label1,
this.datagrid1});
this.name = "form1";
this.text = "鼠标双击事件的例子";
((system.componentmodel.isupportinitialize)(this.datagrid1)).endinit();
this.resumelayout(false);

}
#endregion

[stathread]
static void main()
{
application.run(new form1());
}

private void textboxdoubleclickhandler(object sender, eventargs e)
{
messagebox.show("双击事件发生。鼠标双击到的值:"+((textbox)sender).text.tostring());
}

private void textboxmousedownhandler(object sender, mouseeventargs e)
{
if(datetime.now < gridmousedowntime.addmilliseconds(systeminformation.doubleclicktime))
{
messagebox.show("双击事件发生。鼠标双击到的值:"+((textbox)sender).text.tostring());
}
label1.text = "textbox 鼠标按下了。 ";
}

private void datagrid1_mousedown(object sender, system.windows.forms.mouseeventargs e)
{
gridmousedowntime = datetime.now;
label1.text = "datagrid1 鼠标按下了。 ";
}

private void form1_click(object sender, system.eventargs e)
{
label1.text="";
}
private void label1_click(object sender, system.eventargs e)
{
label1.text="";
}
}
}



国内最大的酷站演示中心!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表