|
-
Apr 23rd, 2013, 01:04 PM
#1
Thread Starter
Member
[RESOLVED] Change the Selected Row of a DGV dynamiclly
Hello all,
I have two DGV's on a form. DGV1 is a list of dates. and DGV is a list of requests from users. the user adds requests to the database for changes of items in DGV1. when the admin user opens FORM1 he have two DGV's,
DGV1 is a list of all the "ITEMS" and the date of each Item. DGV2 is a list of user requests to change info of a certain Date.
I would like to allow the Admin user to be able to Select the row of DGV2 (requests) and when that happens the "selected row" of DGV1 with that corrosponding date be selected. (so the admin user would not have to scroll through the DGV1 to find the date)
My logic is something like
When DGV2 SELECT ROW SELECTED
SET DGV1 SELECTED ROW = TO ROW with Date that Matches DGV2 SELECTED ROW DATE.
am I making any sense?
-
Apr 23rd, 2013, 02:15 PM
#2
Re: Change the Selected Row of a DGV dynamiclly
vb.net Code:
Private Sub DataGridView1_SelectionChanged(sender As System.Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged
Dim MatchIndex = CType((From r In DataGridView2.Rows Where CType(r, DataGridViewRow).Cells("Dates2").FormattedValue.Equals(DataGridView1.CurrentRow.Cells("Dates").FormattedValue)).First, DataGridViewRow).Index
DataGridView2.Rows(MatchIndex).Selected = True
End Sub
This is by no means complete. You'll need some error checking/validating etc. and a way to deal with no matches available. Also it requires single select, select full row mode. But it will give you the basis of a workable solution
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 23rd, 2013, 08:33 PM
#3
Re: Change the Selected Row of a DGV dynamiclly
I would suggest that you should have DataTables or some other lists bound to the grids via BindingSources. You can then get the record selected in one grid via the Current property of its BindingSource, call the Find method of the second BindingSource and use the result to set its Position. The grids will take care of themselves thanks to data-binding. E.g.
Code:
Dim row1 = DirectCast(BindingSource1.Current, DataRowView)
Dim index2 = BindingSource2.Find("Date", row1("Date"))
BindingSource2.Position = index2
That code would go in the CurrentChanged event handler of the first BindingSource.
-
Apr 25th, 2013, 12:52 PM
#4
Thread Starter
Member
Re: Change the Selected Row of a DGV dynamiclly
Did as you said,
added some error checking and such , small adjustments and TADA!
Thanks
-
Apr 25th, 2013, 01:44 PM
#5
Addicted Member
Re: Change the Selected Row of a DGV dynamiclly
 Originally Posted by jmcilhinney
I would suggest that you should have DataTables or some other lists bound to the grids via BindingSources. You can then get the record selected in one grid via the Current property of its BindingSource, call the Find method of the second BindingSource and use the result to set its Position. The grids will take care of themselves thanks to data-binding. E.g.
Code:
Dim row1 = DirectCast(BindingSource1.Current, DataRowView)
Dim index2 = BindingSource2.Find("Date", row1("Date"))
BindingSource2.Position = index2
That code would go in the CurrentChanged event handler of the first BindingSource.
I hope it is ok if I add a small question on topic here.
I am currently doing what the op is but would much prefer to keep my data and UI separate.
I tried doing it the way you suggested but had a lot of bumps so I put it on hold for awhile. I was just wondering how would you go about finding multiple entries with that same property?
-
Apr 25th, 2013, 09:41 PM
#6
Re: Change the Selected Row of a DGV dynamiclly
 Originally Posted by Crzyrio
I hope it is ok if I add a small question on topic here.
I am currently doing what the op is but would much prefer to keep my data and UI separate.
I tried doing it the way you suggested but had a lot of bumps so I put it on hold for awhile. I was just wondering how would you go about finding multiple entries with that same property?
If your data source is a DataTable then you can use its Select method or create a new DataView and set its RowFilter property. If you want to filter the displayed data toshow only those records then you can set the Filter property of the BindingSource.
If your data source is not a DataTable, or even if it is, you could use a loop to find all matching records or, more succinctly, a LINQ query, e.g.
Code:
Dim searchResults = myList.Where(Function(row) row.SomeProperty = TextBox1.Text)
That sort of query will return an enumerable list of matching items from the list, which you can then use as appropriate. That might mean calling ToArray and binding the results or possibly looping through the results and using IndexOf to find it in the original list.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|