Clicked cells row and column

Aug 27, 2008 at 9:54 PM
I guess I'm just missing it.  But when you click on a given cell.  How do you read the coordinates from the cell you clicked on?  (i.e. If you click on row 2 column 2)

Aug 28, 2008 at 6:54 PM
You can use DataGrid.CurrentCell to retrieve the current Column and the current Item.  With those two objects you can find the row index with the current Item and DataGrid.Items and you can find the column index with the current column and DataGrid.Columns.  Also, with the current column you have access to DataGridColumn.DisplayIndex so you can get that as well.  Be aware though that a column's DisplayIndex may not be the same as the column's index in the DataGrid.Columns collection.
Aug 28, 2008 at 8:24 PM
Ok, this seems to work.  But there is something a miss that I must be doing wrong. The substring that was put in the MessageBox is because the string always came back with cell = Microsoft.Windows.Controls.DataGridCell: (cell contents).  Any reason for this? 

Also, the naming for column makes sense.  (i.e. DataGrid1.CurrentCell.Column.DisplayIndex).  Shouldn't there be a Row property just like the Column? (i.e. DataGrid1.CurrentCell.Row.DisplayIndex)?
DataGrid1.Items.IndexOf(DataGrid1.CurrentCell.Item) seems weird!


private void dataGrid1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)




    int row = dataGrid1.Items.IndexOf(dataGrid1.CurrentCell.Item);



    int col = dataGrid1.CurrentCell.Column.DisplayIndex;



    string cell = Convert.ToString(GetCell(row, col));






Thanks for all your help.  You've been great!!!!!!!!!!!!!

Aug 28, 2008 at 10:34 PM
Why not try to use cell.Content.ToString() instead of the Convert.ToString?

Anyway, the reason for the 'Row' name being 'Item' is b/c it is following the ItemsControl pattern where 'Items' is the list.  I talk a little bit about this here,
Aug 28, 2008 at 10:44 PM
I used Convert.ToString because cell.Content.ToString() gave the aformentioned

cell.Content.ToString() = "Microsoft.Windows.Controls.DataGridCell:"

With no contents.  The second time the cell was clicked I would get

cell.Content.ToString() = "Microsoft.Windows.Controls.DataGridCell: {Contents of cell}".  Convert.ToString contained the contents everytime??????????????????????????

Anyway, how the hell do you get rid of "Microsoft.Windows.Controls.DataGridCell" without using cell.Substring(cell.IndexOf(":"+1,6)).  Or is this just happening to me? 



Aug 28, 2008 at 11:42 PM
I was being a little absent minded there. cell.Content represents the visual tree for the cell's content.  So for example if the cell is in a DataGridTextColumn, the Content will be a TextBlock in non-editing state and TextBox in an editing state.  So you can get the value of the visual element by doing something like this, (cell.Content as TextBlock).Text.  It will obviously be different for other types of columns.
Aug 29, 2008 at 1:59 PM

That worked great.  You've been a great help and I really apprieciate it.