dataGrid(winform)问题,实现单击选定整行,双击弹出详细信息 网上有很多资料,耐心找找www.chinaaspx.com 上绝对有,我看过,忘了在哪块了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 点击 属性窗口的小闪电 选择 DoubleClick 事件!编辑代码 完成后一定要点击最前面的dataGrid本身的那列 private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e){ System.Drawing.Point pt = new Point(e.X, e.Y); DataGrid.HitTestInfo hti = ((DataGrid)sender).HitTest(pt); try { if(hti.Type == DataGrid.HitTestType.Cell) { ((DataGrid)sender).CurrentCell = new DataGridCell(hti.Row, hti.Column); ((DataGrid)sender).Select(hti.Row); } } catch{}}private void dataGrid1_DoubleClick(object sender, System.EventArgs e){ try { string XXX = dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString(); TxxxDialog xdialog = new TxxxDialog( xxx ); //自定义详细信息的窗体 xxx.ShowDialog( this ); } catch { }} http://dev.csdn.net/develop/article/44/44698.shtmhttp://dev.csdn.net/develop/article/37/37129.shtm两个都是CSDN文档里关于datagrid双击事件的代码,看看吧,原来的不行,就换一种方法吧 总算找到了:www.chinaaspx.com/archive/dotnet/19071.htm内容不多就贴出来了:给DataGrid单元行添加双击事件hbzxf(阿好)http://www.cnblogs.com/hbzxf 现在我需要做到的功能是当我单击DataGrid某行时显示相对应选中的数据信息,在双击此相同行时弹出删除对话框,应该怎么做呢。由于单击问题很简单就不再阐述了,下面我说一下双击事件是怎么实现的。 这里用到了DataGrid的ItemDataBound事件,我们可以把下面的代码加入到所需的程序中就可实现双击的功能。详细源码如下:private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.SelectedItem) ) { e.Item.Attributes.Add ("ondblclick", "javascript:return confirm('确定删除" + e.Item.Cells[1].Text + "?');"); } }其实就是一个小技巧,楼主看行吗? To OriesMap() : 可是,双击dataGrid内的表格,跟本不会确发 doubleClick事件!~ 谢谢大家的帮助,,............感谢,感谢hedonister(冰戈) ....请问winform 应该怎么改写,..小弟初学呀! TO pyuan(菜鸟) 你这两种和我的那个是一样的! 看using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;//主窗体代码namespace ZZ{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dataGrid1; private DataSet ds; private System.Windows.Forms.Button button1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); ds = new DataSet("MyDataSet"); InitData(ds); this.dataGrid1.DataSource = this.ds; this.dataGrid1.DataMember = this.ds.Tables[0].TableName; } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.button1 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(16, 20); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(340, 160); this.dataGrid1.TabIndex = 0; this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick); // // button1 // this.button1.Location = new System.Drawing.Point(264, 196); this.button1.Name = "button1"; this.button1.TabIndex = 1; this.button1.Text = "修改"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(372, 229); this.Controls.Add(this.button1); this.Controls.Add(this.dataGrid1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion private void InitData(DataSet ds) { DataTable dt = new DataTable("TabeHost"); DataColumn dc = new DataColumn("IpAddress",typeof(string)); dt.Columns.Add(dc); dc = new DataColumn("UserName",typeof(string)); dt.Columns.Add(dc); dc = new DataColumn("Password",typeof(string)); dt.Columns.Add(dc); ds.Tables.Add(dt); DataRow dr = dt.NewRow(); dr["IpAddress"] = "192.192.132.229"; dr["UserName"] = "zhzuo"; dr["Password"] = "zhengzuo"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["IpAddress"] = "192.192.132.230"; dr["UserName"] = "11"; dr["Password"] = "12"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["IpAddress"] = "192.192.132.231"; dr["UserName"] = "123"; dr["Password"] = "12"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["IpAddress"] = "192.192.132.232"; dr["UserName"] = "22"; dr["Password"] = "788"; dt.Rows.Add(dr); } private void button1_Click(object sender, System.EventArgs e) { DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current; Form2 form2 = new Form2(drv); form2.ShowDialog(); } private void dataGrid1_DoubleClick(object sender, System.EventArgs e) { button1_Click(button1,EventArgs.Empty); } }}//子窗体代码=======================using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace ZZ{ /// <summary> /// Form2 的摘要说明。 /// </summary> public class Form2 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private DataRowView drv; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form2(DataRowView dr) { InitializeComponent(); this.drv = dr; this.textBox1.Text = (string)drv["IpAddress"]; this.textBox2.Text = (string)drv["UserName"]; this.textBox3.Text = (string)drv["Password"]; } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(28, 16); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 3; this.textBox1.Text = "textBox1"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(28, 56); this.textBox2.Name = "textBox2"; this.textBox2.TabIndex = 4; this.textBox2.Text = "textBox2"; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(28, 92); this.textBox3.Name = "textBox3"; this.textBox3.TabIndex = 5; this.textBox3.Text = "textBox3"; // // button1 // this.button1.Location = new System.Drawing.Point(152, 152); this.button1.Name = "button1"; this.button1.TabIndex = 6; this.button1.Text = "保存"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(236, 152); this.button2.Name = "button2"; this.button2.TabIndex = 7; this.button2.Text = "退出"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(324, 201); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Name = "Form2"; this.Text = "Form2"; this.ResumeLayout(false); } #endregion private void button1_Click(object sender, System.EventArgs e) { drv["IpAddress"] = this.textBox1.Text; drv["UserName"] = this.textBox2.Text; drv["Password"] = this.textBox3.Text; } private void button2_Click(object sender, System.EventArgs e) { this.Close(); } }}选定行业比较简单,private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e){ Point pt = new Point(e.X,e.Y); DataGrid.HitTestInfo hit = dataGrid1.HitTest(pt); if(hit.Type == DataGrid.HitTestType.Cell) { dataGrid1.Select(hit.Row); }} zhzuo(秋枫) 你这个是用按钮弹出呀,...我想要的是双击谢过 高手进来帮助下 如何读出一个链接下的所有文件,并显示出来 C# 二维数组的问题(超郁闷) 救命!为什么一运行就假死,帮忙看看这个代码 用递归调用怎么解决刷新问题?不会重复出现多个根节点。 进来帮我看看这方案如何解决. 一个调试得小东东~请大虾帮忙解释哈~` 有人用过在储存过程里用Exists吗? 我想用C#开发一套C/S模式的windows程序.问一下如何具体实现几点功能?请高手指点. 给我公司的员工加分 小弟也想和大侠换c#程序学习 小弟也想和大侠换c#程序学习
完成后一定要点击最前面的dataGrid本身的那列
{
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = ((DataGrid)sender).HitTest(pt);
try
{
if(hti.Type == DataGrid.HitTestType.Cell)
{
((DataGrid)sender).CurrentCell = new DataGridCell(hti.Row, hti.Column);
((DataGrid)sender).Select(hti.Row);
}
}
catch{}
}private void dataGrid1_DoubleClick(object sender, System.EventArgs e)
{
try
{
string XXX = dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString();
TxxxDialog xdialog = new TxxxDialog( xxx ); //自定义详细信息的窗体
xxx.ShowDialog( this );
}
catch
{ }
}
http://dev.csdn.net/develop/article/37/37129.shtm两个都是CSDN文档里关于datagrid双击事件的代码,看看吧,原来的不行,就换一种方法吧
http://www.cnblogs.com/hbzxf
现在我需要做到的功能是当我单击DataGrid某行时显示相对应选中的数据信息,在双击此相同行时弹出删除对话框,应该怎么做呢。由于单击问题很简单就不再阐述了,下面我说一下双击事件是怎么实现的。 这里用到了DataGrid的ItemDataBound事件,我们可以把下面的代码加入到所需的程序中就可实现双击的功能。详细源码如下:private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.SelectedItem) )
{
e.Item.Attributes.Add ("ondblclick", "javascript:return confirm('确定删除" + e.Item.Cells[1].Text + "?');");
}
}
其实就是一个小技巧,楼主看行吗?
你这两种和我的那个是一样的!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
//主窗体代码
namespace ZZ
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataSet ds;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
ds = new DataSet("MyDataSet");
InitData(ds);
this.dataGrid1.DataSource = this.ds;
this.dataGrid1.DataMember = this.ds.Tables[0].TableName;
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 20);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(340, 160);
this.dataGrid1.TabIndex = 0;
this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick);
//
// button1
//
this.button1.Location = new System.Drawing.Point(264, 196);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "修改";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(372, 229);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion private void InitData(DataSet ds)
{
DataTable dt = new DataTable("TabeHost");
DataColumn dc = new DataColumn("IpAddress",typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("UserName",typeof(string));
dt.Columns.Add(dc);
dc = new DataColumn("Password",typeof(string));
dt.Columns.Add(dc);
ds.Tables.Add(dt);
DataRow dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.229";
dr["UserName"] = "zhzuo";
dr["Password"] = "zhengzuo";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.230";
dr["UserName"] = "11";
dr["Password"] = "12";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.231";
dr["UserName"] = "123";
dr["Password"] = "12";
dt.Rows.Add(dr); dr = dt.NewRow();
dr["IpAddress"] = "192.192.132.232";
dr["UserName"] = "22";
dr["Password"] = "788";
dt.Rows.Add(dr);
} private void button1_Click(object sender, System.EventArgs e)
{
DataRowView drv = (DataRowView)this.BindingContext[this.ds,this.ds.Tables[0].TableName].Current;
Form2 form2 = new Form2(drv);
form2.ShowDialog(); }
private void dataGrid1_DoubleClick(object sender, System.EventArgs e)
{
button1_Click(button1,EventArgs.Empty);
}
}
}
//子窗体代码
=======================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace ZZ
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private DataRowView drv;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form2(DataRowView dr)
{
InitializeComponent();
this.drv = dr;
this.textBox1.Text = (string)drv["IpAddress"];
this.textBox2.Text = (string)drv["UserName"];
this.textBox3.Text = (string)drv["Password"];
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(28, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 3;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(28, 56);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 4;
this.textBox2.Text = "textBox2";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(28, 92);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 5;
this.textBox3.Text = "textBox3";
//
// button1
//
this.button1.Location = new System.Drawing.Point(152, 152);
this.button1.Name = "button1";
this.button1.TabIndex = 6;
this.button1.Text = "保存";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(236, 152);
this.button2.Name = "button2";
this.button2.TabIndex = 7;
this.button2.Text = "退出";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(324, 201);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false); }
#endregion private void button1_Click(object sender, System.EventArgs e)
{
drv["IpAddress"] = this.textBox1.Text;
drv["UserName"] = this.textBox2.Text;
drv["Password"] = this.textBox3.Text;
} private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
选定行业比较简单,
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point pt = new Point(e.X,e.Y);
DataGrid.HitTestInfo hit = dataGrid1.HitTest(pt);
if(hit.Type == DataGrid.HitTestType.Cell)
{
dataGrid1.Select(hit.Row);
}
}