private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    DataGrid myGrid = CType(sender, DataGrid);
    System.Windows.Forms.DataGrid.HitTestInfo hti = myGrid.HitTest(e.X, e.Y);    switch case hti.Type
    {
    case System.Windows.Forms.DataGrid.HitTestType.None: 
       Console.WriteLine("You clicked the background.");
       break;
    case System.Windows.Forms.DataGrid.HitTestType.Cell: 
       Console.WriteLine("You clicked cell at row " & hti.Row & ", col " & hti.Column);
       break;
    case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader:
       Console.WriteLine("You clicked the column header for column " & hti.Column);
       break;
    case System.Windows.Forms.DataGrid.HitTestType.RowHeader: 
       Console.WriteLine("You clicked the row header for row " & hti.Row);
       break;
    case System.Windows.Forms.DataGrid.HitTestType.ColumnResize:
       Console.WriteLine("You clicked the column resizer for column " & hti.Column);
       break;
    case System.Windows.Forms.DataGrid.HitTestType.RowResize: 
       Console.WriteLine("You clicked the row resizer for row " & hti.Row);
       break;
    case System.Windows.Forms.DataGrid.HitTestType.Caption:
       Console.WriteLine("You clicked the caption");
       break;
    case System.Windows.Forms.DataGrid.HitTestType.ParentRows: 
       Console.WriteLine("You clicked the parent row");
       break;
    }
}根据VB改的,语法不对或大小写有问题的,请自己修改

解决方案 »

  1.   

    错了,是值应该是与它绑定的datatable中的行列dataset.Tables["datatablename"].Rows[rownum][colnum]
      

  2.   

    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);    
    if(hti.Type==System.Windows.Forms.DataGrid.HitTestType.Cell)
    {
     MessageBox.Show("You clicked cell at row "&hti.Row&",col "&hti.Column);
    MessageBox.Show(this.dtMaterialDetail.Rows[hti.Row][hti.Column].tostring());
    }
    }
      

  3.   

    private System.Windows.Forms.DataGrid.HitTestInfo hit;
    dataGrid[hit.Row, 列].ToString();
    hit.Row为当前行
      

  4.   

    如果你要取当前单元格可用
    dataGrid[dataGrid.CurrentRowIndex,RowColumnNumber].ToString;
    但是如果你要取任意单元格我感觉可以使用楼上的说法.
      

  5.   

    dataGrid[int x, int y].ToString()
    //x 是行值,y 是列值
      

  6.   

    DataGrid myGrid = (DataGrid) sender;
    System.Windows.Forms.DataGrid.HitTestInfo hti;
    hti = myGrid.HitTest(e.X, e.Y);
                
    if(hti.Type == System.Windows.Forms.DataGrid.HitTestType.Cell || hti.Type==System.Windows.Forms.DataGrid.HitTestType.RowHeader)
    {//判断点击的类型,是否为单元格,此例中我选择获取开始的三个单元格的内容,根据hti.row的值确定单击的行
    myGrid.CurrentCell = new DataGridCell(hti.Row,0);
    textBox1.Text = dataGrid1[dataGrid1.CurrentCell].ToString();
    myGrid.CurrentCell = new DataGridCell(hti.Row,1);
    textBox2.Text = dataGrid1[dataGrid1.CurrentCell].ToString();
    myGrid.CurrentCell = new DataGridCell(hti.Row,2);
    textBox3.Text = dataGrid1[dataGrid1.CurrentCell].ToString();
    }