有谁能解决让winform的datagrid响应row的单击事件和双击事件

wfhlxl 2006-05-28 09:04:58
就是说单击某个单元格,让datagrid感知单元格所在行被单击了,调用 用户能在其中写处理代码的事件函数,而实际上datagrid要做这样的事真是力不从心,microsoft是怎么想的呀。
...全文
371 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wfhlxl 2006-05-29
  • 打赏
  • 举报
回复
怎么没人理解我说的呀,DataGrid 不能响应单击和双击事件呀
wfhlxl 2006-05-29
  • 打赏
  • 举报
回复
楼上的几位,你们究竟试过没有,真的能让DataGrid当单元格被单击时候,DataGrid能捕捉到事件,就是说你的DataGrid的Click事件函数没被调用,不信,你们试试.
wcmj 2006-05-29
  • 打赏
  • 举报
回复
调用 用户能在其中写处理代码的事件函数,而实际上datagrid要做这样的事真是力不从心,microsoft是怎么想的呀。

我晕,你不会用就不要乱说!

你在属性里加事件就行了, 不会加事件吗? 点属性里的闪电符号后,想要哪个加哪个
要是这个也不知道的话那没办法了
wcmj 2006-05-29
  • 打赏
  • 举报
回复
建议你用muoseclick做,我现在在做的项目就是用这个解决的
wcmj 2006-05-29
  • 打赏
  • 举报
回复
dataGrid1_Click是能响应的,但是在某此情况时会取到错的单元格,不知道算不算dataGrid
的bug
jedliu 2006-05-29
  • 打赏
  • 举报
回复
private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
DataGrid myGrid = (DataGrid) sender;
System.Windows.Forms.DataGrid.HitTestInfo hti;
hti = myGrid.HitTest(e.X, e.Y);
string message = "You clicked ";

switch (hti.Type)
{
case System.Windows.Forms.DataGrid.HitTestType.Cell :
message += "cell at row " + hti.Row + ", col " + hti.Column;
break;
case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader :
message += "the column header for column " + hti.Column;
break;
case System.Windows.Forms.DataGrid.HitTestType.RowHeader :
message += "the row header for row " + hti.Row;
break;
case System.Windows.Forms.DataGrid.HitTestType.Caption :
message += "the caption";
break;
case System.Windows.Forms.DataGrid.HitTestType.ParentRows :
message += "the parent row";
break;
}

MessageBox.Show(message);
}


试试,单击完全可以。
另外,这个方法只适用于单击。如果双击,我看可以参考楼上的!
zhoujijunnt 2006-05-29
  • 打赏
  • 举报
回复
全部代码如下:
namespace DataGridDoubleClick
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataSet myDataSet;
DateTime gridMouseDownTime;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

gridMouseDownTime = DateTime.Now;
// Call SetUp to bind the controls.
SetUp();
}

private void SetUp()
{
// Create a DataSet with two tables and one relation.
MakeDataSet();
/* Bind the DataGrid to the DataSet. The dataMember
specifies that the Customers table should be displayed.*/
dataGrid1.SetDataBinding(myDataSet, "Customers");

//create and add a custom table style so we can
//easily get at the behavior of a cell...
AddCustomDataTableStyle();
}

private void MakeDataSet()
{
// Create a DataSet.
myDataSet = new DataSet("myDataSet");

// Create two DataTables.
DataTable tCust = new DataTable("Customers");

// Create two columns, and add them to the first table.
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);

// Add the tables to the DataSet.
myDataSet.Tables.Add(tCust);


/* Populates the tables. For each customer and order,
creates two DataRow variables. */
DataRow newRow1;

// Create three customers in the Customers Table.
for(int i = 1; i < 4; i++)
{
newRow1 = tCust.NewRow();
newRow1["custID"] = (100*i).ToString();
tCust.Rows.Add(newRow1);
}
// Give each customer a distinct name.
tCust.Rows[0]["custName"] = "John Summers";
tCust.Rows[1]["custName"] = "Phil Seagram";
tCust.Rows[2]["custName"] = "Sam Robinson";

// And address
tCust.Rows[0]["custCity"] = "Chicago";
tCust.Rows[1]["custCity"] = "Los Angeles";
tCust.Rows[2]["custCity"] = "Washington";
}

private void AddCustomDataTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Customers";
// Set other properties.
ts1.AlternatingBackColor = Color.LightGray;
//
// Add textbox column style so we can catch textbox mouse clicks
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custID";
TextCol.HeaderText = "CustomerID";
TextCol.Width = 100;
//add handler
TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol);

TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custName";
TextCol.HeaderText = "Customer Name";
TextCol.Width = 100;
//add handler
TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol);

TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custCity";
TextCol.HeaderText = "Customer Address";
TextCol.Width = 100;
//add handler
TextCol.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
TextCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol);

dataGrid1.TableStyles.Add(ts1);

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((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(32, 24);
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);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(424, 189);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void TextBoxDoubleClickHandler(object sender, EventArgs e)
{
MessageBox.Show("TrueDoubleClick");
this.Close();
}

private void TextBoxMouseDownHandler(object sender, MouseEventArgs e)
{
if(DateTime.Now < gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))
{
MessageBox.Show("GridDoubleClick");
}
Console.WriteLine("TextBoxMouseDownHandler " );
}

private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
gridMouseDownTime = DateTime.Now;
Console.WriteLine("dataGrid1_MouseDown " );
}
}
}
zhoujijunnt 2006-05-29
  • 打赏
  • 举报
回复
lz留个mail,我发个例子你看一下就明白了
nikita007 2006-05-29
  • 打赏
  • 举报
回复
忘了补充了:
在FormLoad中要加上 AddCustomDataTableStyle();
nikita007 2006-05-29
  • 打赏
  • 举报
回复
这个是可以实现的,我给你一个我的代码:
private void SetUp()
{
string select="SELECT SUP_NO, COMPANY_NAME, ADDRESS FROM t_b_SUPPLIER WHERE STATUS!=9 ORDER BY SUP_NO ASC";
SqlDataAdapter da = new SqlDataAdapter(select,conn);
DataSet ds=new DataSet();
da.Fill(ds,"t_b_SUPPLIER");
dataGrid1.SetDataBinding(ds,"t_b_SUPPLIER");
}


private void AddCustomDataTableStyle()
{


DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "t_b_SUPPLIER";
// 设置属性
ts1.AlternatingBackColor = Color.LightGray;


// 添加Textbox列样式,以便我们捕捉鼠标事件
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "SUP_NO";
TextCol.HeaderText = "供应商编号";
TextCol.Width = 100;
TextCol.ReadOnly = true;
//添加事件处理器
TextCol.TextBox.Click += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol);


TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "COMPANY_NAME";
TextCol.HeaderText = "供应商名称";
TextCol.Width = 150;
TextCol.ReadOnly = true;
TextCol.TextBox.Click += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol);


TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "ADDRESS";
TextCol.HeaderText = "供应商地址";
TextCol.Width = 200;
TextCol.ReadOnly = true;
TextCol.TextBox.Click += new EventHandler(TextBoxDoubleClickHandler);
ts1.GridColumnStyles.Add(TextCol);

ts1.SelectionBackColor = System.Drawing.Color.Blue;

dataGrid1.TableStyles.Add(ts1);



}
private void dataGrid1_CurrentCellChanged(object sender,System.EventArgs e)
{
dataGrid1.Select(this.dataGrid1.CurrentCell.RowNumber);
}

private void TextBoxDoubleClickHandler(object sender, EventArgs e)
{
SUP_NO = this.dataGrid1[this.dataGrid1.CurrentCell.RowNumber,0].ToString();
contract_sp_new myForm = new contract_sp_new();
myForm.ShowDialog();
}
anycool 2006-05-29
  • 打赏
  • 举报
回复
private void dataGrid1_Click(object sender, System.EventArgs e)
{
if(ds!=null)
{
if((MessageBox.Show("确定要删除此记录吗?","删除记录操作",MessageBoxButtons.YesNo))==DialogResult.Yes)
{
using(SqlConnection conn=new SqlConnection("server=.;uid=sa;pwd=ok;database=musicmanagment"))
{
string sql=" delete musicTable where Musicid="+ds.Tables[0].
Rows[this.BindingContext[ds.Tables[0]].Position][0];
ds.Tables[0].Rows[this.BindingContext[ds.Tables[0]].Position].Delete();
SqlCommand com=new SqlCommand(sql,conn);
conn.Open();
int i=com.ExecuteNonQuery();
conn.Close();
this.dataGrid1.DataSource=ds.Tables[0];
if(i>0)
{
MessageBox.Show("删除成功!");
}
}
}
}

}

这样可以写出在datagrid中的单击事件,确定删除的行数

不知是不是这个呢~~

上面只是一个例子.
wfhlxl 2006-05-29
  • 打赏
  • 举报
回复
DataGrid 不能响应单击和双击事件呀 ,不信,你们试试
tonysunny 2006-05-28
  • 打赏
  • 举报
回复
你可以到datagrid 的属性框里面添加事件啊
Firestone2003 2006-05-28
  • 打赏
  • 举报
回复
实现这个功能没有什么问题啊
bhwhy 2006-05-28
  • 打赏
  • 举报
回复
http://www.syncfusion.com/FAQ/WindowsForms/Default.aspx#44
参考这个网址。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧