Click to See Complete Forum and Search --> : getting cell details in datagrid from mousehover event


raghavkotha
May 7th, 2003, 12:15 AM
hai,

i need to get the row and column numbers in a datagrid from the point where i placed the mouse. That is, i need that row and column number of that cell on which i placed the mouse. I examined HitTest function, but it is only for the clicked position. can any one suggest me the solution?

bye

aewarnick
May 17th, 2003, 10:33 PM
Is there any way to get a row and columb from a Point()? If so, my only thought of how to do it would be to create an instance Point variable then set the point according to a MouseMove event.

Then when the mouse if hovering you can use that Point to get the cell.

raghavkotha
May 26th, 2003, 01:53 AM
hi,

But it wont solve my problem. My actual requirement is to get cell from the mousehover event. Your suggestion is to get it from mousemove. But there is no function in datagrid that will give cell position(row,col) from the mousemove event(or by the specification of point).

aewarnick
May 27th, 2003, 10:30 AM
It will solve your problem if you make the Point a global variable. In you hover event just use that global Point variable to get a cell.

Satishpp
May 28th, 2003, 06:16 PM
This will get you the row and column number your mouse is placed on

private void dataGrid1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
DataGrid d1 = (DataGrid)sender;
DataGrid.HitTestInfo hitTestInfo = d1.HitTest(new Point(e.X,e.Y));
textBox5.Text = hitTestInfo.Column.ToString();
textBox6.Text = hitTestInfo.Row.ToString();
}

Satish

raghavkotha
May 29th, 2003, 12:21 AM
thanks, satish

thanks for your post. I solved my problem by using MouseMove event. I placed a link label dynamically at the place. In that way, i can able to create hyperlinks in the datagrid.

bye

redsolo
May 29th, 2003, 10:45 AM
I used the mousehover event, but there has another problem,
it always treat the ColumnHeader as the first row.

Perhaps we can solve the problem together!!!

private void dataGrid1_MouseHover(object sender, System.EventArgs e)
{
string str;
//get this form's location
Point ptfrm = new Point(this.Location.X,this.Location.Y);
//get the dbgrid's location
Point ptgrid = new Point(dataGrid1.Location.X,dataGrid1.Location.Y);

Point pt = DataGrid.MousePosition;
//get the Point to the dbgrid
pt.X = pt.X - (ptfrm.X + ptgrid.X);
pt.Y = pt.Y - (ptfrm.Y + ptgrid.Y );


DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);

if(hti.Type == DataGrid.HitTestType.Cell)
{
str = "Column: " + hti.Column.ToString();
str += ", Row: "+ hti.Row.ToString();
MessageBox.Show(str);
}



}

raghavkotha
May 30th, 2003, 01:10 AM
hi,

Yes, if we use MouseHover event, it will treat column header also a row. It will start its index from -1. Header row is -1, first row is 0, second row is 1 and so on... I think, there is no problem in giving like that. You can write your logic according to these
index values. Then problem will be solved.

bye