Getting selected data from Grid x & y co-ordinates: C#

  • Thread starter Thread starter maria
  • Start date Start date
M

maria

Hello

i'm developing a helpdesk application on a Windows Form
that loads up a datagrid. the grid is populated by a
select statement. the idea is that when a person loads up
the form, all their open requests will load onto the
datagrid. to edit their request, they need to right-click
and select Edit Request from a context menu. this will
then load up another form with all the details neatly
displayed.

i have gotten so far as to be able to load up the context
menu on a right click and offer the option of "Edit
Request". I have also managed to capture the X and Y co-
ordinates of wherever the user right-clicked.

so what my application does so far is:
-it loads a datagrid with open requests,
-when a right-click is issued anywhere on the datagrid a
context menu appears offering an "Edit Request" option.
-When this is selected, the x & y co-ordinates of the
right-click are displayed in a message box.

Each row in the datagrid has a ChangeID assigned to it,
so, from the x & y points, i need to be able to capture
the ChangeID from whatever row was right-clicked on. once
i have this ID, i will pass it on to a stored proc which
will then populate the EditRequests form (another form in
my app).

Would anyone have any idea on how to translate x & y co-
ordinates into data?
Any help here would be most appreciated! :)

Thanks in advance!!!!
 
Hi there

thanks for the response. I did actually use
DataGrid.HitTest to get the x and y co-ordinates. But any
idea as to how i turn the x and y coordinates into my
data? i don't know what to do with these coordinates.

Thanks
maria
 
HitTest method returns you a HitTestInfo class instance that holds Row and
Column property that you are looking for.
You might use Row and Column to retrieve the cell text by using
DataGrid[row, column] property.
 
oh, OK. I see what you mean. Just for a bit of
clarification (i am quite new to programming as i'm sure
you can tell!):

How would i use DataGrid[row, column]? Do i substitute
DataGrid with the name of my datagrid (grdRequests), and
[row, column] replaced by the x and y co-ordinates i've
captured?

i'm sorry if this seems like a basic question :)

Thanks!

-----Original Message-----
HitTest method returns you a HitTestInfo class instance that holds Row and
Column property that you are looking for.
You might use Row and Column to retrieve the cell text by using
DataGrid[row, column] property.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Hi there

thanks for the response. I did actually use
DataGrid.HitTest to get the x and y co-ordinates. But any
idea as to how i turn the x and y coordinates into my
data? i don't know what to do with these coordinates.

Thanks
maria


.
 
Hi maria,

maria said:
oh, OK. I see what you mean. Just for a bit of
clarification (i am quite new to programming as i'm sure
you can tell!):

No problem - we all were once new :)
How would i use DataGrid[row, column]? Do i substitute
DataGrid with the name of my datagrid (grdRequests), and
[row, column] replaced by the x and y co-ordinates i've
captured?

Here is the sample (I put the code in MouseDown event)
[C#]
DataGrid.HitTestInfo hti = dataGrid1.HitTest(e.X, e.Y);

if (hti.Type == DataGrid.HitTestType.Cell)

{

object o = dataGrid1[hti.Row, hti.Column];

Console.WriteLine(o);

}

[VB]

Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(e.X, e.Y)

if hti.Type = DataGrid.HitTestType.Cell Then

Dim o As object = dataGrid1[hti.Row, hti.Column] ' retrieve the
object from cell

Console.WriteLine(o)
' output object's text (o.ToString())

End If

Do not hesitate to ask if need more info.


--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
i'm sorry if this seems like a basic question :)

Thanks!

-----Original Message-----
HitTest method returns you a HitTestInfo class instance that holds Row and
Column property that you are looking for.
You might use Row and Column to retrieve the cell text by using
DataGrid[row, column] property.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Hi there

thanks for the response. I did actually use
DataGrid.HitTest to get the x and y co-ordinates. But any
idea as to how i turn the x and y coordinates into my
data? i don't know what to do with these coordinates.

Thanks
maria
-----Original Message-----
Hi maria,

message
Hello

<snip>

Would anyone have any idea on how to translate x & y co-
ordinates into data?
Any help here would be most appreciated! :)

DataGrid.HitTest method should do the job.

--
Miha Markic - RightHand .NET consulting & software
development
miha at rthand com
www.rthand.com


.


.
 
Once you get the row you can:

BindingManagerBase BMB = BindingContext
[dataGrid.DataSource, dataGrid.DataMember];
BMB.Position = row;
DataRowView rowData = (DataRowView)BMB.Current;

This will get you the entire row of data from your
dataset for the row upon which the HitTest happened.

You can then pass this informattion to the form your
doing the edit with.
-----Original Message-----
oh, OK. I see what you mean. Just for a bit of
clarification (i am quite new to programming as i'm sure
you can tell!):

How would i use DataGrid[row, column]? Do i substitute
DataGrid with the name of my datagrid (grdRequests), and
[row, column] replaced by the x and y co-ordinates i've
captured?

i'm sorry if this seems like a basic question :)

Thanks!

-----Original Message-----
HitTest method returns you a HitTestInfo class instance that holds Row and
Column property that you are looking for.
You might use Row and Column to retrieve the cell text
by
using
DataGrid[row, column] property.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Hi there

thanks for the response. I did actually use
DataGrid.HitTest to get the x and y co-ordinates. But any
idea as to how i turn the x and y coordinates into my
data? i don't know what to do with these coordinates.

Thanks
maria
-----Original Message-----
Hi maria,

message
Hello

<snip>

Would anyone have any idea on how to translate x &
y
.
 
Hello,

thank you for all your help regarding this. and for your
quick responses. My colleague and I worked with DataGrid
[row, colum] and have managed to get it working. Here's
what we did:

string strCell = grdRequests
[m_LastGridRowSelected,0].ToString();

grdRequests is my datagrid. m_LastGridRowSelected is the
my x-axis coordinate on right-click, and "0" is my y-
coordinate.

This worked really well. But i did have one more question:
initially i tried to use DataGrid.HitTestInfo but it
wouldn't resolve. i could resolve Hittest, but not
HittestInfo. is there something else you need to do before
you can just simply use HitTestInfo?

Thanks again!

Maria
-----Original Message-----
Hi maria,

oh, OK. I see what you mean. Just for a bit of
clarification (i am quite new to programming as i'm sure
you can tell!):

No problem - we all were once new :)
How would i use DataGrid[row, column]? Do i substitute
DataGrid with the name of my datagrid (grdRequests), and
[row, column] replaced by the x and y co-ordinates i've
captured?

Here is the sample (I put the code in MouseDown event)
[C#]
DataGrid.HitTestInfo hti = dataGrid1.HitTest(e.X, e.Y);

if (hti.Type == DataGrid.HitTestType.Cell)

{

object o = dataGrid1[hti.Row, hti.Column];

Console.WriteLine(o);

}

[VB]

Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(e.X, e.Y)

if hti.Type = DataGrid.HitTestType.Cell Then

Dim o As object = dataGrid1[hti.Row,
hti.Column] ' retrieve the
object from cell

Console.WriteLine(o)
' output object's text (o.ToString())

End If

Do not hesitate to ask if need more info.


--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
i'm sorry if this seems like a basic question :)

Thanks!

-----Original Message-----
HitTest method returns you a HitTestInfo class instance that holds Row and
Column property that you are looking for.
You might use Row and Column to retrieve the cell text
by
using
DataGrid[row, column] property.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Hi there

thanks for the response. I did actually use
DataGrid.HitTest to get the x and y co-ordinates. But any
idea as to how i turn the x and y coordinates into my
data? i don't know what to do with these coordinates.

Thanks
maria
-----Original Message-----
Hi maria,

message
Hello

<snip>

Would anyone have any idea on how to translate x
& y
co-
ordinates into data?
Any help here would be most appreciated! :)

DataGrid.HitTest method should do the job.

--
Miha Markic - RightHand .NET consulting & software
development
miha at rthand com
www.rthand.com


.



.


.
 
Back
Top