Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to know how I can get the index of a dataGridView Row over which the mouse is passing in the MouseMove Event of dataGridView?

Thank you very much
Posted

use this code may be it will help you......

C#
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (!((DataGridViewRow)(dataGridView1.Rows[e.RowIndex])).Selected)
                {
                    dataGridView1.ClearSelection();
                    ((DataGridViewRow)dataGridView1.Rows[e.RowIndex]).Selected = true;
                    if (dataGridView1.SelectedRows.Count > 0)
                    {
                        DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[e.RowIndex];
                        TextBox1.Text = e.RowIndex.ToString();// return row index of dataGridView On CellMouseMove Event and Display RowIndex in TextBox1.
                    }
                }
            }
        }
 
Share this answer
 
Comments
ahhashemi 27-Jul-10 3:19am    
Thank you very much Dear Jitendra_rtpl,

Inspired from your code, these two line directly meet the need of my question

dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;

or:

dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Green;
pulkit1111111111 28-Oct-10 16:55pm    
sir u r really really very g88 u solved my prob u knw i have searched for 3 hours to get this sol but didn't get den its my luck i got ur code thanks frm bottom of my heart
e.RowIndex and e.ColumnIndex will return the RowIndex and ColumnIndex in the CellMouseMove Event Handler of the DataGridView
 
Share this answer
 
v2
Comments
ahhashemi 27-Jul-10 3:26am    
Dear Kabeer,

My problem was that one can not have access to "RowIndex" or "ColumnIndex" property in "MouseMove Event" and that made me ask a question. Now learning from Dear Jitendra_rtpl, I noticed that these properties could be easily used in "CellMouseMove Event" instead.

Regards,
koool.kabeer 27-Jul-10 5:44am    
sorry dear ahhashemi i wrote wrong i wanted to write CellMouseMove Event Handler i forgot to write "Cell"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900