Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, April 29, 2010 3:45 AM
Once the added button in grid (window control Grid, not Gridview or datagrid) is clicked, how to find which row and column the button is located in the grid event handler, like click event or some other events? Not the button click event handler
#region Grid event handler setup
myGrid.MouseEnter += new MouseEventHandler(myGrid_MouseEnter);
myGrid.MouseLeave += new MouseEventHandler(myGrid_MouseLeave);
myGrid.MouseDown += new MouseButtonEventHandler(myGrid_MouseDown);
myGrid.MouseUp += new MouseButtonEventHandler(myGrid_MouseUp);
#endregion
Thanks
I notice that Boyan has some solution for the button click event handler case
In the Click event handler for the button you say:
int row;
Button btn = sender as Button;
if (btn != null)
{
row = Grid.GetRow(btn); // And you have the row number...
}
else
{
// A nasty error occurred...
}
All replies (6)
Wednesday, May 5, 2010 8:39 AM âś…Answered
Hello ThomasJH,
Thanks for your post.
I searched for a solution, you may have a try. Hope it helpful.
UIelement element = (UIelement) Grid.InputHitTest(e.GetPosition(Grid));
row= Grid.GetRow(element);
For more information, you can see:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.inputhittest.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.grid.aspx
If you have any problems, please feel free to follow up.
Best regards,
Liliane
Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
Thursday, April 29, 2010 4:05 AM
Grid.CurrentRow.RowIndex
or
Grid.CurrentCell.RowIndex
Thursday, April 29, 2010 4:11 AM
to get the column value of the selected row:
this.theDataGridView.Rows[index ].Cells**[Index]** .Value
you would have to probably take the first index from using my example earlier of obtaining the selected rows, something like maybe:
(untested)
DataGridViewSelectedRowCollection t = this.theDataGridView.SelectedRows;
object theValue = this.theDataGridView.Rows[t[0].Index].Cells[index ].Value;
Or:
object theValue = this.theDataGridView[columnIndex , rowIndex ].Value;
Regards,
Rahul.
Thursday, April 29, 2010 4:12 AM
The problem is your using the mouse click as the event instead you should be subscribing to the CellContentClickedEvent
This is the equivalent to left mouse click but also handles keyboard or programmatic clicks. More importantly it will provide you with DataGridViewCellEventArgs which will give you the row and column of the item clicked.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int row = e.RowIndex;
int col = e.ColumnIndex;
}
Edit: I noticed that article was about WPF so what I said above might not be valid I haven't messed with WPF controls much yet.
Thursday, April 29, 2010 5:02 AM
Grid.CurrentRow.RowIndex
or
Grid.CurrentCell.RowIndex
Thanks. they are for DataGrid
Thursday, April 29, 2010 5:08 AM
Thanks.
I didn't make it clear in the original post that I am trying windows control grid, not gridview or datagrid.