Hi,
I am attempting to get the "RowIndex" of the mouse pointer in the DragEnter Event of the DataGridView, But the value is always "-1". I am using the following code below,
void dgvTestGrid_DragEnter(object sender, DragEventArgs e)
{
Point clientPoint = dgvTestGrid.PointToClient(new Point(e.X, e.Y));
DataGridView.HitTestInfo dgvHitTest = dgvTestGrid.HitTest(clientPoint.X, clientPoint.Y);
int iPointerRowIndex = dgvHitTest.RowIndex.ToString();
}
But when I do the same thing in the DragDrop Event I get the correct RowIndex, Can anyone let me know How can I get the RowIndex in the DragEnter Event?
Thanks,
Theo. | | Theo_10 Monday, August 18, 2008 10:36 AM |
Hi Theo_10,
Based on my understanding, the problem you are facing is “Why the RowIndex is always -1 when you try to get in the DragEnter event of the DataGridView�
As MSDN document said, �/span>If the mouse enters another control, the DragEnter event for that control is raised.�We can get a lot of information from this sentence.
Assume that we drag from the ListBox to the DataGridview, once the dragged item of the ListBox get in touch with the DataGridViewm, the DragEnter event of DataGridView is raised. So when DragEnter event happens, the mouse is in a position thatisn't contained any rows or columns of the DataGridView. It will make sense that the RowIndex is always -1 with your code.
From the above we get to know that “why we can’t get the row with your code in DragEnter event handler� We can easily get to know why in DragDrop event can do it. Since DragDrop event is raised after you released your mouse, the position is where you want to place the dragged item. It will be easy for your code to get the RowIndex from the position of your mouse.
If you have further problems, please let me know.
Best Regards,
Bruce Zhou
Windows Forms General FAQs Windows Forms Data Controls and Databinding FAQs
| | Bruce.Zhou Thursday, August 21, 2008 8:15 AM |
Hi Theo_10,
Based on my understanding, the problem you are facing is “Why the RowIndex is always -1 when you try to get in the DragEnter event of the DataGridView�
As MSDN document said, �/span>If the mouse enters another control, the DragEnter event for that control is raised.�We can get a lot of information from this sentence.
Assume that we drag from the ListBox to the DataGridview, once the dragged item of the ListBox get in touch with the DataGridViewm, the DragEnter event of DataGridView is raised. So when DragEnter event happens, the mouse is in a position thatisn't contained any rows or columns of the DataGridView. It will make sense that the RowIndex is always -1 with your code.
From the above we get to know that “why we can’t get the row with your code in DragEnter event handler� We can easily get to know why in DragDrop event can do it. Since DragDrop event is raised after you released your mouse, the position is where you want to place the dragged item. It will be easy for your code to get the RowIndex from the position of your mouse.
If you have further problems, please let me know.
Best Regards,
Bruce Zhou
Windows Forms General FAQs Windows Forms Data Controls and Databinding FAQs
| | Bruce.Zhou Thursday, August 21, 2008 8:15 AM | Hi Bruce Zhou,
Thats Perfect!!! Thank you very muchfor your reply. Now I understood why I am not able to get the Row Index on DragEnter event.
But I am trying to solve a business problem here where in I want to validate if the dragged item can be inserted into a perticular row. I would be helpful for me if you can help meout in figureing out how to get the row details where the user is currently pointing to, so that I can change the DragDropEffects accordingly.
--
Thanks,
Theo | | Theo_10 Thursday, August 21, 2008 8:28 AM |
Hi Theo_10,
Glad my answer can help you. Since what you asked sounds like a new question, I suggest you start a new thread. So you may also get a lot of help from our community members who may already have this kind of experience.
Best Regards,
Bruce Zhou
Windows Forms General FAQs Windows Forms Data Controls and Databinding FAQs
| | Bruce.Zhou Thursday, August 21, 2008 8:41 AM | Hi Bruce Zhou,
Thanks, Will raise this query as a new thread.
--
Thanks,
Theo | | Theo_10 Thursday, August 21, 2008 8:45 AM | For god shake, you did not help at all. I found your answer is totally bogus. And I think yuu ducked the question because you don't know the answer.
Hi Theo_10,
To get the row index of the place where the drop occurred, you need to get to coordinator of the client, as shown below:
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y)); // Get the row index of the item the mouse is below. rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
DataGridViewRow row = this.dataGridView1.Rows[rowIndexOfItemUnderMouseToDrop];
David N.
David N. | | David N_2 Monday, April 27, 2009 4:12 PM | For god shake, you did not help at all. I found your answer is totally bogus. And I think yuu ducked the question because you don't know the answer. Hi Theo_10, To get the row index of the place where the drop occurred, you need to get to coordinator of the client, as shown below: Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y)); // Get the row index of the item the mouse is below. rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex; DataGridViewRow row = this .dataGridView1.Rows[rowIndexOfItemUnderMouseToDrop]; David N.
David N.
I think you need to re-read the title, and the content of this thread to know what Theo_10 wants to know.
Please mark the replies as answers if they help and unmark if they don't. | | Bruce.Zhou Tuesday, April 28, 2009 12:33 AM |
|