Solved

How can I set the equivlent of the "selectedIndex" of a DataGridViewComboboxCell?

Posted on 2011-09-06
3
520 Views
Last Modified: 2012-05-12
Basically, I want to set a "SelectedIndex" value of the comboboxcell.

From the code shown, objTypes object will contain something like:-
Value          Desc
10               First
20               Second
30               Third
etc....

I want to default some of the rows to specific values.  If I set the ".Value" property of the cell to 20, I want this to display "Second" within the combobox cell but I get the error attached as screen shot:-

Any help would be appreciated
 Error screen shot
Dim dgCombo As DataGridViewComboBoxCell = grdHeader.Rows(i).Cells("colType")
dgCombo.DataSource = objTypes
dgCombo.DisplayMember = "Desc"
dgCombo.ValueMember = "Value"

Open in new window

0
Comment
Question by:bmsdev
3 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 36489529
Dim dgCombo As DataGridViewComboBoxCell = grdHeader.Rows(i).Cells("colType")
dgCombo.DisplayMember = "Desc" 'With this you say that the combo must show the "Desc" value of each object added
dgCombo.ValueMember = "Value" 'With this you say that the value member of each object is the "Value" property or field
dgCombo.DataSource = objTypes 'Set the data source

dgCombo.Value = 4 'For example, with this the object with the value 4 in the "Value" property or field will be selected

Hope that helps.
0
 
LVL 15

Expert Comment

by:x77
ID: 36489586
A ComboBoxCell contains a ComboBox only when the cell is in Edit Mode.

Dgv has only One ComboBox or Textbox ... intance for all Cells.
Then you can access to Cell Control only when the Cell is in Edit Mode.

To Get the Control, you can use the Event Dgv_EditingControlShowing.
   At this point you can use E.Control and cast it to use apropiate methods an properties.
   Dgv_EditModeChanged, notifies edit mode changes.

At Cell Level, you can Override InitializeEditingControl and DetachEditingControl.

You can get the EditingControl from Dgv an cast It, but it is valid when cell is editing mode.

   Dim Cmb = TryCast(Dgv.Editingcontrol,DataGridViewComboBoxEditingControl)
   if cmb isnot nothing ....
0
 

Author Closing Comment

by:bmsdev
ID: 36489592
That worked perfictly.  I did origionally have that code, but the ".Value" property is a Decimal, and i was setting it to an Integer.  Thanks for you help.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net/LINQ: ".Contains" condition not finding existing result in MSSQL 16 35
No Data for DropDown List 2 25
Convert datetime to time string 10 21
C# LINQ ForEach() question 6 28
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question