Results 1 to 5 of 5

Thread: DataGridView: using a loop of selected items rather than currentrow

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    42

    DataGridView: using a loop of selected items rather than currentrow

    First real time working with a datagridview (Model_dgv) and at this point I have a loop cycling through all the selected cells. Ideally, each cell will have information from one of the columns added to a textbox. Currently, instead of referencing the object "model" as set up in the loop, I am adding based off of my current row, so obviously I am not getting all of the items, but the most recent selection repeated N times (with N = model_dgv.selectedcells.count)

    Code:
                For Each model In Model_dgv.SelectedCells
                    'if it's different add it (so '' becomes SERIES1; SERIES1 remains SERIES1, SERIES2 becomes SERIES2; SERIES1)
                    If lvl1_tb.Text <> Model_dgv.Item("Model Lvl1", Model_dgv.CurrentRow.Index).Value.ToString.Trim Then
                        lvl1_tb.Text &= Model_dgv.Item("Model Lvl1", Model_dgv.CurrentRow.Index).Value.ToString.Trim
                    End If
                ...
    simply, I don't know enough about DGV to know how to write the line to call the cell based on the loop rather than the current selection. Any thoughts/comments? Places to start and research.

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

    Re: DataGridView: using a loop of selected items rather than currentrow

    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

  3. #3
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    482

    Re: DataGridView: using a loop of selected items rather than currentrow

    EDIT: slight correction to the if statement

    I haven't tested it, but try this:

    Code:
            For Each row As DataGridViewRow In Model_dgv.SelectedRows
                If Not InStr(lvl1_tb.Text, row.Cells.Item("Model Lvl1").Value.ToString.Trim) Then
                    lvl1_tb.Text &= row.Cells.Item("Model Lvl1").Value.ToString.Trim
                End If
            Next

    This should loop through the selected rows, check to see if the value is already part of lvl1_tb.Text and if not then add the value.
    NOTE: The compare is case sensitive as it is written.
    Last edited by Maverickz; Aug 20th, 2012 at 07:02 PM.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: DataGridView: using a loop of selected items rather than currentrow

    Your duplicate thread has been merged with this one.

  5. #5
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: DataGridView: using a loop of selected items rather than currentrow

    Quote Originally Posted by ichthuso1 View Post
    First real time working with a datagridview (Model_dgv) and at this point I have a loop cycling through all the selected cells. Ideally, each cell will have information from one of the columns added to a textbox. Currently, instead of referencing the object "model" as set up in the loop, I am adding based off of my current row, so obviously I am not getting all of the items, but the most recent selection repeated N times (with N = model_dgv.selectedcells.count)

    Code:
                For Each model In Model_dgv.SelectedCells
                    'if it's different add it (so '' becomes SERIES1; SERIES1 remains SERIES1, SERIES2 becomes SERIES2; SERIES1)
                    If lvl1_tb.Text <> Model_dgv.Item("Model Lvl1", Model_dgv.CurrentRow.Index).Value.ToString.Trim Then
                        lvl1_tb.Text &= Model_dgv.Item("Model Lvl1", Model_dgv.CurrentRow.Index).Value.ToString.Trim
                    End If
                ...
    simply, I don't know enough about DGV to know how to write the line to call the cell based on the loop rather than the current selection. Any thoughts/comments? Places to start and research.
    From what I understand, you're just having issues looping through all the cells in the datagridview control? So this is all you need help with?

    Here's an example I've put together for you:
    vbnet Code:
    1. For Each DataRow As DataGridViewRow In DataGridView1.Rows
    2.     For Each DataCell As DataGridViewCell In DataRow.Cells
    3.         'Do something with DataCell here
    4.     Next
    5. Next

    You can also use LINQ to retrieve data, if that's the purpose for looping through each cell.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

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