Results 1 to 4 of 4

Thread: [2.0] DataGridView -- Select Items

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    66

    [2.0] DataGridView -- Select Items

    Ok, so I have six Colums that are all loaded into the DataGridView, each coloumn as info in it. How could I get the text of each selected item in the 2 column?

    Somthing like.

    Code:
    foreach(object item in this.DGV.SelectedItemsINCol2)
    {
    MessageBox.Show(item.ToString());
    }
    Thanks

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

    Re: [2.0] DataGridView -- Select Items

    The DataGridView has a SelectedRows property that is a DataGridViewSelectedRowCollection.
    The DataGridViewSelectedRowCollection has an Item property that is a DataGridViewRow.
    The DataGridViewRow has a Cells property that is a DataGridViewCellCollection.
    The DataGridViewCellCollection has an Item property that is a DataGridViewCell.
    The DataGridViewCell has a Value property that is an Object and is the value displayed in the cell.
    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

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    66

    Re: [2.0] DataGridView -- Select Items

    Do you think you could give me a small example? I couldnt firgure it out.

  4. #4
    Hyperactive Member BrandonTurner's Avatar
    Join Date
    Sep 2001
    Location
    East Lansing, Michiagn
    Posts
    268

    Re: [2.0] DataGridView -- Select Items

    Code:
             foreach (DataGridViewRow dgvr in ConcernTableView.SelectedRows)
             {
                //
                // You can do it by cell index number
                //
                MessageBox.Show(dgvr.Cells[2].Value.ToString());
    
                //
                // Or you can do it by name which is much better!!!!
                //
                MessageBox.Show(dgvr.Cells["id"].Value.ToString());
    
                //
                // Or if you are binding something, you can get the class
                //
                MessageBox.Show(((Concern)dgvr.DataBoundItem).Id.ToString());
             }

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