CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2011
    Posts
    19

    retrieving seleted value from combobox in datagridview

    hello im having a problem retrieving selected value from a combobox in datagridview. my datagridview is getting its data from an sql database. so the comboboxes are filled during runtime and i dont know how to get the selected combobox item's value. all i can think of is this--
    Code:
     
    string hehe = dataGridView4.Rows[dataGridView4.SelectedCells[0].RowIndex].Cells[3].Value.ToString();
    please help me get the selected item from combobox residing inside the datagridview so that i can use it in my code

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: retrieving seleted value from combobox in datagridview

    handle the CellValidated event of the gridview
    inside, use the e.RowIndex to ensure it is >=0 (else you're not on a datarow)
    use the e.ColumnIndex to disocver on which colum you are if you need it
    to retrieve the value from the datagridviewcomboboxcolumn use:
    Code:
    DataGridViewComboBoxCell thisCboCell = (DataGridViewComboBoxCell)YourGridView.CurrentRow.Cells[indexOfComboBoxColumn];
           if (thisCboCell .Value != null)
          {
    
                string theSelectedValue = thisCboCell.Value.ToString();
          }
    provided you set a ValueMember and a DisplayMember for that column, that code should give you the valuemember
    Last edited by Cimperiali; March 14th, 2012 at 12:02 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured