[RESOLVED] Setting Selected Index on DataGridView Combobox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: [RESOLVED] Setting Selected Index on DataGridView Combobox

  1. #1
    Join Date
    Oct 2008
    Posts
    2

    [RESOLVED] Setting Selected Index on DataGridView Combobox

    I can't seem to set the selected index or selected value of a DataGridView combobox when loading data from a database. My DGV has two combobox columns which are bound to datatables. The DGV itself is not bound initially. After adding a row, I select a value from each combobox which then populates the rest of the cells in the row. I'm using the EditingControlShowing event to add an eventhandler to catch the SelectedIndexChange of the combo boxes. This all works as needed. My issue comes when populating the DGV with saved data, I need to add the rows and set the selected index and/or selected value of each combo box. I've searched through various forums and articles for the last week with no luck.

    I'm using VS2008 sp1, .NET 3.5 sp1 and SQL 2005 sp2 Express.

    I'm fairly new to .NET and winforms development, coming mostly from a web background so any help is greatly appreciated.

    Please let me know if I need to give more details or post any code.

    Thanks

  2. #2
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Setting Selected Index on DataGridView Combobox

    Welcome to the forums HatMaster! sorry for the extremely delayed reply!

    When you add the row, you should be able to just access the cell which is the ComboBoxCell and set the value property. Here's a quick example which assumes that column 0 is the combo box:
    Code:
    'First, get the cell as a useful object
    'You'll need to know the index of the new row
    Dim CBox as DataGridViewComboBoxCell = CType(DGV.Rows(NewRowIndex).Cell(0), DataGridViewComboBoxCell)
    Dim CCol as DataGridViewComboBoxColumn = CType(DGV.Columns(0)), DataGridViewComboBoxColumn)
    
    CBox.Value = CCol.Items(CCol.Items.Count - 1)
    This sets the value of the combobox in column 0 of row NewRowIndex to the last value from the list defined in the column settings for column 0
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  3. #3
    Join Date
    Oct 2008
    Posts
    2

    Talking Re: Setting Selected Index on DataGridView Combobox

    Thanks javajawa!!!

    It worked. I thought I had already tried something similar but your example worked perfectly. I appreciate the help!

  4. #4
    Join Date
    Sep 2011
    Posts
    1

    Re: Setting Selected Index on DataGridView Combobox

    Quote Originally Posted by javajawa View Post
    Welcome to the forums HatMaster! sorry for the extremely delayed reply!

    When you add the row, you should be able to just access the cell which is the ComboBoxCell and set the value property. Here's a quick example which assumes that column 0 is the combo box:
    Code:
    'First, get the cell as a useful object
    'You'll need to know the index of the new row
    Dim CBox as DataGridViewComboBoxCell = CType(DGV.Rows(NewRowIndex).Cell(0), DataGridViewComboBoxCell)
    Dim CCol as DataGridViewComboBoxColumn = CType(DGV.Columns(0)), DataGridViewComboBoxColumn)
    
    CBox.Value = CCol.Items(CCol.Items.Count - 1)
    This sets the value of the combobox in column 0 of row NewRowIndex to the last value from the list defined in the column settings for column 0

    I tested the code and it did not work. I had to change it. My DataGridViewComboBoxColumn is bound to a database table. I filled its DataSourse, DataValue and DataDisplay properties. My DataValue data type is System.Int32 and my DataDisplay data type is String. So the value property of the DataGridViewComboBoxCell variable CBox will be System.int32. Therefore If I want to display the lastest items of the combo list I would have to write:

    Code:
     Private Sub ComboCell_Fill(ByVal pFil As Integer, _
                                     ByVal pCol As Integer, _
                                     ByRef pG As DataGridView, _
                                     ByVal pId As Long)
    
    Dim CBox As DataGridViewComboBoxCell = CType(pG.Rows(pFil).Cells(pCol), DataGridViewComboBoxCell)
    Dim CCol As DataGridViewComboBoxColumn = CType(pG.Columns(pCol), DataGridViewComboBoxColumn)
    
    'This option if you want to display then combo by its DataValue property       
     CBox.Value = CType(pId, System.Int32)
    'This option if you want to display the last item of the combobox collection.
     CBox.Value = CType(CCol.Items(CCol.Items.Count - 1), DataRowView).Row(0)         
    
    
    pG.UpdateCellValue(pCol, pFil)
    
     End Sub
    I hope this to be usefull, because I had to spend much time searching and debugging to learn this.

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: [RESOLVED] Setting Selected Index on DataGridView Combobox

    Quote Originally Posted by PedroZorro View Post
    Thanks a lot. If this code works I will kiss ....
    Woooaaa .. Please... this is a respectable All ages forum... Please remove the profanity...

    When registering you agreed to the AUP (acceptable use policy) where it states:

    By using these Websites, you agree to the following:
    * .......
    * You will not use profanity and will neither post with language or Content that is obscene, sexually oriented, sexually suggestive or otherwise objectionable, nor link to websites that contain such content.
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Dec 2011
    Posts
    1

    Re: Setting Selected Index on DataGridView Combobox

    Quote Originally Posted by PedroZorro View Post
    I tested the code and it did not work. I had to change it. My DataGridViewComboBoxColumn is bound to a database table. I filled its DataSourse, DataValue and DataDisplay properties. My DataValue data type is System.Int32 and my DataDisplay data type is String. So the value property of the DataGridViewComboBoxCell variable CBox will be System.int32. Therefore If I want to display the lastest items of the combo list I would have to write:

    Code:
     Private Sub ComboCell_Fill(ByVal pFil As Integer, _
                                     ByVal pCol As Integer, _
                                     ByRef pG As DataGridView, _
                                     ByVal pId As Long)
    
    Dim CBox As DataGridViewComboBoxCell = CType(pG.Rows(pFil).Cells(pCol), DataGridViewComboBoxCell)
    Dim CCol As DataGridViewComboBoxColumn = CType(pG.Columns(pCol), DataGridViewComboBoxColumn)
    
    'This option if you want to display then combo by its DataValue property       
     CBox.Value = CType(pId, System.Int32)
    'This option if you want to display the last item of the combobox collection.
     CBox.Value = CType(CCol.Items(CCol.Items.Count - 1), DataRowView).Row(0)         
    
    
    pG.UpdateCellValue(pCol, pFil)
    
     End Sub
    I hope this to be usefull, because I had to spend much time searching and debugging to learn this.
    Thanks! It works for me. I searched for 2 days and this is the only solution that really works!

  7. #7
    Join Date
    Dec 2011
    Posts
    2

    Re: [RESOLVED] Setting Selected Index on DataGridView Combobox

    Hi... is this code functional in C#? Hope someone can help me, I've been google-ing for over a week... I will try to addapt the code to C# and I'll let you know if it works.

  8. #8
    Join Date
    Dec 2011
    Posts
    2

    Re: [RESOLVED] Setting Selected Index on DataGridView Combobox

    I wrote the following code:

    columnaProducto = new CType(dataGridViewDetalle.Columns[0], new DataGridViewComboBoxColumn());

    The type or namespace name 'CType' could not be found (are you missing a using directive or an assembly reference?)

    anyone know if I need to add a reference or something? Remenber I'm developing in C#

  9. #9
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,844

    Re: [RESOLVED] Setting Selected Index on DataGridView Combobox

    CType as well as several other functions beginning with C are members of the VB lanuage classes. I think you can add a reference to these and use them under C#.
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Jan 2014
    Posts
    3

    Re: [RESOLVED] Setting Selected Index on DataGridView Combobox

    Can you post your complete code?
    I don't understand when you call sub "ComboCell_Fill"

    P.S.: Sorry for my bad English...

  11. #11
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    Re: [RESOLVED] Setting Selected Index on DataGridView Combobox

    Quote Originally Posted by gino.o View Post
    Can you post your complete code?
    I don't understand when you call sub "ComboCell_Fill"

    P.S.: Sorry for my bad English...
    You do realise that this thread is 6 years old! Please do not revive old threads in the future.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center