Identifying an object on double click in DataGridView

cadwalli

Active member
Joined
May 27, 2010
Messages
41
Location
United Kingdom
Programming Experience
10+
Hi,

I dont know whether im being incredibly stupid here (no comments :) ) but I am struggling to get some data from a data grid.

Basically, I have a collection of objections which populate the datagridview fine, however, when i double click the row, I want to retrieve the obejct that is being double clicked so that I can load it in to on screen fields for maniuplation.

However, for some reason i dont seem to be able to get the right syntax to make this work.

Ive tried retrieving the rowindex etc, but I completely confused as to how i get from the datagrid to lookign at the object in question itself.

For example, my grid could show:

Username Fullname ........
AnOther Andrew Other
PNut Peter Nut
etc...

Each line in the grid represents the data in the collection

When the line is double clicked i then want this information and more from teh object double clicked to appear in the text boxes - i.e. fields which arent shown in the grid such as Dateofbirth etc, so I need to know which object im selecting.

Sorry if my explanation was a bit tedious, its probably obvious what Im trying to achieve, just wanted to give as much info as possible

Hope someone can help

Thanks
Ian

And of course i meant collection of OBJECTS not OBJECTIONS ... its been a very long day!
 
If you're double-clicking the row then you can use the CurrentRow property. Once you've got the row, the DataBoundItem will give you the item bound to that row.

Better still, bind your data to a BindingSource and bind that to the grid, then just use the Current property of the BindingSource.
 
Great thanks for that.
Im not sure whether its terminology I may be wrong on, but have I "bound" my source correctly then?

Basically, the code I am using is below, the .fill function simply fills my object collection, then i generate teh columns automatically, and assign the collection as the datasource to the grid.

mytickettypeno_ctrls.Fill()
dgvNextNumbers.AutoGenerateColumns = True
dgvNextNumbers.DataSource = mytickettypeno_ctrls

Does this look like or have I missed something out as I dont seem to be getting the results I would expect, in teh double click event i dont seem to be able to access the values.
 
That binding looks correct, assuming that you don't want to use a BindingSource. If you're seeing the data in the grid then it's bound correctly. Youre obviously doing something wrong in the event handler, but we can't say what without seeing what you're doing.
 
Its the event handler im having the trouble with - correct - i actually dont understand what syntax i should use at all to try and get from teh line double clicked to the object behind it.

I cant really post code for what Im doing as I have simply been trying different things - none of which really make sense!

Im workign through some tutorials but they still dont seem to work.
 
Well i think my databinding now works, but interstingly when i double click ona line in the gridview i now get an exception "Indexoutofrangeexception" was unexpected"

"Make sure the maximum insex on a list is less that the list size"

Now im confused, as its not actually doing anything !
 
Hi

The code is simply:

Dim StatusBindSource As New BindingSource
StatusBindSource.DataSource = myStatusCodes
myStatusCodes.Fill()
dgvStatusCodes.AutoGenerateColumns = True
dgvStatusCodes.DataSource = StatusBindSource


Which is almosthe same as previous post, there is NO double click event or anything at all set up now. Howeer, this code runs and populates the gridview but as soon as you click the gridview (single, double chlick, tab anyting) it crashes out with the error above.
 
I meant to mention previously, setting AutoGenerateColumns to True is unnecessary because that's its default value.

As for the error, there's nothing we can tell you because we have no useful information. The exception will tell you where the actual error is occurring, specifically in the StackTrace, which is a list of method calls that led up to the error.
 
Hi

Apologies if the information is sketchy i give, butVB and the debugger are all new to me so I dont know exactly what to report when i have a problem.

The debugger exception loads form1.designervb and highlights the line "PArtial class form1" when it errors, that all the information I can see
 
OK, Ive gone back to my original gridview, the following is my form load which loads the statuscodes into the gridview:

Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click

Dim myStatusCodes As New StatusCodes
dgvStatusCodes.AutoGenerateColumns = True
myStatusCodes.Fill()
dgvStatusCodes.DataSource = myStatusCodes

End Sub

The gridview simply displays "Status Code" and "Description"

My double click command is simple:

Private Sub dgvStatusCodes_RowHeaderMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvStatusCodes.RowHeaderMouseDoubleClick

Dim i As Integer
i = e.RowIndex

MessageBox.Show(dgvStatusCodes.Item("StatusCode", i).Value)

End Sub

So all im doing here is showing the value of the status code which has been double clicked.

Now from here I can then do another search through the object collecnts (StatusCodes) and find the object with the same status code.

What I was trying to do, was to be able to retrieve the object directly without a second search (if thats possible) as in I was under the impression that I should be able to access the object directly as the datagridview is showing it. Mianly because what if i dont want to show the status code itself i the view, but just the description - how would i find a matching onject then?

When ive used combo boxes ive managed to access the object behind the item in the list directly, but i cant remember how I did it (like i said im new to this). So I thought I could do the same here.

I hope that this is a bit more informatoin and shows what im trying to achieve.

Ian
 
Back in post #2 I gave you two options for getting the item bound to the row. You've chosen to ignore them both. Either follow the advice I've already provided, explain exactly why you can't or else I can't help you further.
 
Hi

Apologies, I coudlnt get the suggestion you made to work so I tried a different way, I wasnt ignoring sir, I am new to this and learning the way the syntax works which is why im struggling to ask for help/describe the problems sometimes.

Ive jsut tried again using mytempobj = dgvStatusCodes.CurrentRow.DataBoundItem and today it works.

I think i must have been having a bad day yesterday.

Apologies

And many thanks for your help - it is appreciated.
 
Back
Top