Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-06
3
Medium Priority
?
538 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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