-
October 13th, 2008, 09:50 PM
#1
[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
-
October 22nd, 2008, 09:40 AM
#2
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.
-
October 22nd, 2008, 12:41 PM
#3
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!
-
September 6th, 2011, 03:01 PM
#4
Re: [RESOLVED] Setting Selected Index on DataGridView Combobox
Originally Posted by PedroZorro
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.
-
September 6th, 2011, 09:17 AM
#5
Re: Setting Selected Index on DataGridView Combobox
Originally Posted by javajawa
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.
-
December 15th, 2011, 12:13 PM
#6
Re: Setting Selected Index on DataGridView Combobox
Originally Posted by PedroZorro
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!
-
December 20th, 2011, 11:11 AM
#7
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#
-
December 20th, 2011, 11:00 AM
#8
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.
-
December 20th, 2011, 04:18 PM
#9
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.
-
January 31st, 2014, 05:30 AM
#10
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...
-
February 1st, 2014, 03:15 AM
#11
Re: [RESOLVED] Setting Selected Index on DataGridView Combobox
Originally Posted by gino.o
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|