try something likeprivate void DataGrid1_Click(object sender, System.EventArgs e)
{
System.Drawing.Point pt = DataGrid1.PointToClient(Cursor.Position); 
DataGrid.HitTestInfo hti = DataGrid1.HitTest(pt); 
if(hti.Type == DataGrid.HitTestType.Cell)

MessageBox.Show(pt.ToString());   
}  }see other options like MouseUp here
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q696q

解决方案 »

  1.   

    DataGrid的单元格没有Click事件,只有控件本身的Click事件。
    可以通过控件本身的Click事件,结合DataGrid.HitTest方法判断Click的单元格。参见楼上
      

  2.   

    to saucer(思归, MS .NET MVP) 
    你的方法不可行!!!!
      

  3.   

    to saucer(思归, MS .NET MVP) 
    你的方法不可行!!!!
      

  4.   

    http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp
      

  5.   

    重写DataGrid类:
    using System;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Resources;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    namespace myNewDataGrid
    { /// <summary>
    /// 名称: Asp.Net DataGrid WebControl Plus Edition。
    /// 功能: 在原有DataGrid基础上增加以下功能
    ///(单击行事件...)
    /// </summary>
    public class mnDataGrid: DataGrid,IPostBackEventHandler
    {
    /// <summary>
    /// 鼠标单击DataGrid行事件。
    /// </summary>
    public event DataGridItemEventHandler Click;
    protected void OnClick(int index)
    {
    if(null!=Click)
    Click(this,new DataGridItemEventArgs(Items[index]));
    }
    /// <summary>
    /// 通过eventArgument触发相应事件。
    /// </summary>
    /// <param name="eventArgument">
    /// CLK前缀:单击事件(此时后缀为触发的行号,从0开始),
    /// </param>
    public void RaisePostBackEvent(string eventArgument)
    {
    if(eventArgument.StartsWith("CLK"))
    {
    eventArgument=
    eventArgument.TrimStart(new Char[]{'C','L','K'});
    OnClick(Convert.ToInt32(eventArgument));
    return;
    }
    }
    protected override void OnPreRender(EventArgs e)
    {
    //获取对客户端脚本函数的引用
    Page.GetPostBackEventReference(this);
    //注册单击事件脚本
    Page.RegisterStartupScript(
    UniqueID+"clickScript",
    clickScript((null==Click)?false:true);
    }
    /// <summary>
    /// 单击脚本。
    /// </summary>
    /// <param name="canClick">
    /// 是否允许提交DataGrid单击行事件。
    /// </param>
    /// <returns>
    /// 处理设置DataGrid单击行事件的javascript脚本。
    /// </returns>
    private string clickScript(bool canClick)
    {
    StringBuilder sbScript=new StringBuilder();
    sbScript.Append("<script language='javascript'>")//;
    //单击标志
    .Append(
    "var "+UniqueID+"_click=0;")
    //被单击行标志
    .Append(
    "var "+UniqueID+"_row;")
    //单击事件
    .Append(
    "function "+UniqueID+"_clk(row)")
    .Append("{")
    .Append(
    "if((\"TD\"==event.srcElement.tagName)||(\"TR\"==event.srcElement.tagName))")
    .Append("{")
    .Append(UniqueID+"_click++;")
    .Append(UniqueID+"_row=row;")
    .Append(
    "setTimeout('"+UniqueID+"_submit()',300)")
    .Append("}")
    .Append("}")
    //提交单击事件
    .Append(
    "function "+UniqueID+"_submit()")
    .Append("{")
    .Append(
    "if(1=="+UniqueID+"_click)
    .Append("{");
    if(true==canClick)
    {
    sbScript.Append(
    "__doPostBack('"+UniqueID+"','CLK'+"+UniqueID+"_row);");
    }
    sbScript.Append("}")
    //单击标志复位
    .Append(
    UniqueID+"_click=0;")
    .Append("}")
    .Append("</script>");
    return sbScript.ToString();
    }
    ..........
    然后在aspx中定义事件:this.DataGrid1.Click += new System.Web.UI.WebControls.DataGridItemEventHandler(this.click);
    使用该事件:
    private void click(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    this.Label1.Text="单击第"+(e.Item.ItemIndex+1)+"行";
    }
      

  6.   

    确定用户所单击的 DataGrid 部分 在适当的事件处理程序(如 MouseDown 或 Click 事件的处理程序)中调用 HitTest 方法。 
    HitTest 方法返回包含所单击区域的行和列的 DataGrid.HitTestInfo 对象。 ' Visual Basic
    Private Sub myDataGrid_MouseDown(ByVal sender As Object, _
    ByVal e As MouseEventArgs) Handles myDataGrid.MouseDown
       Dim myGrid As DataGrid = CType(sender, DataGrid)
       Dim hti As System.Windows.Forms.DataGrid.HitTestInfo
       hti = myGrid.HitTest(e.X, e.Y)
       Dim message As String = "You clicked "   Select Case hti.Type
          Case System.Windows.Forms.DataGrid.HitTestType.None
             message &= "the background."
          Case System.Windows.Forms.DataGrid.HitTestType.Cell
             message &= "cell at row " & hti.Row & ", col " & hti.Column
          Case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader
             message &= "the column header for column " & hti.Column
          Case System.Windows.Forms.DataGrid.HitTestType.RowHeader
             message &= "the row header for row " & hti.Row
          Case System.Windows.Forms.DataGrid.HitTestType.ColumnResize
             message &= "the column resizer for column " & hti.Column
          Case System.Windows.Forms.DataGrid.HitTestType.RowResize
             message &= "the row resizer for row " & hti.Row
          Case System.Windows.Forms.DataGrid.HitTestType.Caption
             message &= "the caption"
          Case System.Windows.Forms.DataGrid.HitTestType.ParentRows
             message &= "the parent row"
       End Select   Console.WriteLine(message)
    End Sub// C#
    private void myDataGrid_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.None :
             message += "the background.";
             break;
          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.ColumnResize :
             message += "the column resizer for column " + hti.Column;
             break;
          case System.Windows.Forms.DataGrid.HitTestType.RowResize :
             message += "the row resizer 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;
          }      Console.WriteLine(message);