I have a Combobox, I want to "Change in place" the selected value in comboBox in c#, and I get its data from the database.
When I select an item from combobox, I want to edit in place and change it, I do that by clicking Update Button to change it. But it doesn't work!
SpmoDataBase.Instance.PhoneType.PhoneType_Edit(cmbPhoneTypeEdit.Text);
the error is:
NullReferenceException Class
Object reference not set to an instance of an object.
Related
I'm using Visual Studio, so at this point I have not written any code, it's all just property settings and drag-n-drop in the GUI. I have a form with several controls bound to a table. To populate a ComboBox on the form , I used the ComboBox task panel to bind the options to a view that has two fields (Type_Num and Type_Desc).
In the properties box for the ComboBox, I have my DisplayMember set to Type_Desc and my ValueMember set to Type_Num. When I run the form, the correct values are there in the dropdown, but when I select one and save, it tries to save the Type_Desc in the control instead of the Type_Num. What am I missing?
This happens when you have setup data-binding to Text property, while you need to save selected value. You should bind SelectedValue.
To change it at designer, expand the (+DataBindings) property group and remove data binding from text property and setup data binding to SelectedValue:
I am trying to manually change the selected item in a combo box so that it will physically change the value on the interface. I have tried changing the index value and display value on the drop down list, but this does not change the value on the interface. Has anyone ever done this?
Use case:
A user adds a new value to the database. The observable collection that feedsd the combo box is updated in memory with the new value to choose from. But, you want to save the user a click and just make the new value they added as the selected item in the combo box, rather than forcing them to add the new item, and then go over to the combo box to select it.
If you really want to do this...
You could try:
yourComboBox.SelectedItem = // the new Object that you want to set the comboBox to;
If the object is found, it will also automatically update the SelectedIndex.
How to allow Combobox placed inside Datagrid to type text.
It just binding the values already stored in the database. but i need to type the value in the Combobox which is not shown in the bound values.
How to make the Combobox as editable column?
You can set ComboBoxStyle to DropDown
Specifies that the list is displayed by clicking the down arrow and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.
I use VS2008 C# + Windows Forms. I can't understand why comboBox does not behave the way it should.
In Design mode, I added a comboBox to my form, and edit Items to add "A" and "B". Double-clicking brings me to SelectedIndexChanged event, which I edit to display the selected text with MessageBox.
private void comboBoxImageSet_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBoxImageSet.SelectedText);
}
When I run, and select "A" or "B" in the comboBox, the MessageBox appears, but nothing is written.
Why?
Thanks.
Here the differences between the selection properties of a ComboBox control.
SelectedIndex;
SelectedItem;
SelectedText;
SelectedValue.
The SelectedIndex property :
Gets or sets the index specifying the currently selected item.
Simply indicates the index of the selected item in the selection list. (Information provided for your kind information only. =))
The SelectedItem property :
Gets or sets currently selected item in the ComboBox.
The SelectedItem represents the element that is currently selected as per the ListControl of the ComboBox. That is why this is what you want to use, to answer your question.
The SelectedText property :
Gets or sets the text that is selected in the editable portion of a ComboBox.
That is, when you edit the TextBox portion of the ComboBox, the text that might be selected when you enter for edit, or any other type of text selection. This indeed does include any selection made through the ListControl portion of the ComboBox. For instance, if your ComboBox.DropDownStyle property is set to ComboBoxStyle.DropDownList, then you will never be able to select any text in the editable portion of the ComboBox. Despite, you're able to select another item within the its list. That is why it is not the right property to use to serve your purpose.
The SelectedValue property :
Gets or sets the value of the member property specified by the ValueMember property.
Only used when using DataBinding, in conjunction with the DisplayMember property. For instance, when you want to display the name of a customer, and select him by his database Id, then the DisplayMember should display the customer's name, and the ValueMember the Id. This way, when you select one customer, the SelectedValue changes and raises the SelectedValueChanged event inherited from the ListControl. (Information provided for your kind information only. =))
The SelectedText property returns the text that is marked in the combobox, not the selected item. If the combobox is editable you can mark a part of the text and the SelectedText property will return the marked text. Look here.
What you are interested in is the SelectedItem property or the SelectedValue property.
ComboBox.SelectedText
A string that represents the currently
selected text in the combo box. If
DropDownStyle is set to DropDownList,
the return value is an empty string
("").
Use SelectedItem instead of SelectedText
SelectedText:
Gets or sets the text that is selected
in the editable portion of a ComboBox.
That is, it gets the text that is currently marked.
You want to use SelectedItem.ToString().
I have a typical requirement.
I have a datagridview with a combobox column(items loaded at design time). When a user selects an item from combobox, remaining rows gets updated in database based on the selectedItem and dgv gets refreshed.
Problem is the combo box will lose its current selection and goes to unselected state.
I want to retain the selected item even after dgv refreshed.
Could anyone help me out
Thanks in advance
Do you mean that you're using an unbound comboboxcolumn? If so, the value can't automatically be persisted when refreshing the datasource. You need to store the selected value before updating and setting it in code after refresh.
If your column is actually databound, the selected value is either not stored in the database or you have some data type problem.
Is the combobox there to let the user select a value for the field or do you use it as a way to execute a command on the record?
Do you have any code you can post?
Datagrid Combo-Box value will retain the string value but will refresh any integer values automatically.
Here is what you need to do :
-When populating Combo-Box value to just convert its value to toString().
-Also if you are setting a default select value also set it with string type.
-Your Combo-box will automatically retain the value selected even after refreshing.
:)
have a combobox in ur datagridview. Assign values to it using a bindingsource.
then write an eventhandler for the datagridviews "EditingControlShowing" event.
in that, remove had handler if any exists for the comboboxes Selectedindexchanged event. then add an event handler for the selectedIndexChanged event say "ComboBoxValueChanged"
in that "ComboBoxValueChanged",
DirectCast the sender to System.Windows.Forms.DataGridViewComboBoxEditingControl and get the selected value of it.
Now use it to compute any value you want.
you may wana refer to this
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx