Results 1 to 6 of 6

Thread: [RESOLVED] Change the Selected Row of a DGV dynamiclly

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Resolved [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?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Change the Selected Row of a DGV dynamiclly

    vb.net Code:
    1. Private Sub DataGridView1_SelectionChanged(sender As System.Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged
    2.         Dim MatchIndex = CType((From r In DataGridView2.Rows Where CType(r, DataGridViewRow).Cells("Dates2").FormattedValue.Equals(DataGridView1.CurrentRow.Cells("Dates").FormattedValue)).First, DataGridViewRow).Index
    3.         DataGridView2.Rows(MatchIndex).Selected = True
    4.     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!

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Re: Change the Selected Row of a DGV dynamiclly

    Did as you said,
    added some error checking and such , small adjustments and TADA!

    Thanks

  5. #5
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Change the Selected Row of a DGV dynamiclly

    Quote Originally Posted by jmcilhinney View Post
    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Change the Selected Row of a DGV dynamiclly

    Quote Originally Posted by Crzyrio View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width