Use selected text instead of selectedIndex - c#

I have a dataset connected to a bindinglist that is connected to a combobox in win32.
I would like to make "Computer" to be selected as a displaymember in the combobox when I have a private string with value "Computer".
The main issue is that it doesn't work to use SelectedText in the combobox to make "Computer" to be selected but it works properly when I use selectedIndex. But, in order to use selectedIndex, you have to know the row number from table in order to make the text to be selected.

(Not sure because of tags, I am assuming you are using WinForms for your application).
Try the following:
combobox.SelectedValue = "Computer";

Related

Bound Combobox changing SelectedItem when (re)filling Datasource

I encountered a very specific behave on the Comboboxes in Winform. I found a lot of quite similar questions, but no useful anwser to my problem:
I have a form where controls are bound to a SQL datasource via TableAdapter/manager. Some of the values are linked to Lookuptables which are of type Combobox. When I first fill the base dataset, all is shown correctly. I can choose items from Combobox (fields id and NamedItem) where "id" is bound as datamember to the base datset table and "NamedItem" is the DisplayMember of the Combobox.
No problems when changing values or updating but ...
once I have to refill the Combobox underlaying datasource (eg. in case, an entry was added), the selected Index of the Combobox is set to 0 - means, the first item in the list.
Sure, I could remember the selectedindex and restore it after. For one Combobox, no problem, but for dozens ...
Is there any other way to keep the last selecteditem after refilling the combobox?
If items would not be deleted from your combo list , turning the Clearbeforfill property of your Table Adapter to False can prevent that.

Eliminate null entry in combobox

Is there any property to remove the first (and empty) item in a combobox with style DropDownList ? In other words, I would like to choose the default selected item for a combobox.
I know i can validate the selected item with code, but i want to avoid showing message boxes to the user.
Set the comboBox.SelectedIndex property to 0 to set the selection to the first item in the combobox.
You should set the Text or SelectedIndex or SelectedValue property. In this way the combobox updates the text that's is showing and removes the first empty item (that actually is not a real item).

C# Datagridview: get selected item in combobox columns

I'm working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the user to add columns as an extention on my project. The column gets added to the dataset table, then updated to the datagridveiew that I use to display the xml file in. I've included the ability for the user to add a combobox column to select choices instead of entering them in constantly like.. true or false. However, that is where the problem lies. Saving a normal column was easy. The combobox column is being a pain.
I have a "save combobox column" to have it updated to the xml and a "save" button to save in a destination of the user's choice.
I've done some research and it seems like the combobox class has such a feature to gain access to the selecteditem in the combobox put in by the user.
Where we have:
ComboBox box = new ComboBox();
box.SelectedItem;
I tried applying this to the combobox column class but it does not have such a function. Thus, I cannot figure out how to directly obtain the value of the user's selected item. I tried experimenting with comboboxcell's as well, but that didn't lead me anywhere either. Both those classes I played around with do not have a... "selected item" function and even google does not have a solution for me. =( I've also tried using the cell.value, but it is "null" for some reason. Even when the user selects an item in the box, it doesn't get saved into the cell's value.
TLDR:
My question in short is, how, if possible, do you gain access to the comboboxcolumn cell's selected item? Additionally, how would you then ensure that the item value is saved in the cell?
Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.
Sincerely,
tf.rz
The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).
You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.
If we have bound a datagridcomboboxcell with a different DisplayMember and ValueMember, like so:
dgcombocell.DisplayMember = "Name";
dgcombocell.ValueMember = "Id";
dgcombocell.DataSource = dataset1.Tables[0];
Then for getting SelectedText, and SelectedValue, we can write this code:
string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);
I hope it solves your problem.
Use this to get or set selected value:
object selectedValue = currentRow.Cells["comboboxColumnName"].Value
Don't forget to set DisplayMember and ValueMember for your DataGridViewComboBoxColumn
This is how it is done
DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];
MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));
A .Net combox is actually a composite control made up of a textbox and a dropdownlist. Use box.Text to get the currently displayed information.
EDIT: The row or the cell should have a .FindControl() method. You'll need to do something like:
Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;
Basically, you're finding the control within its container (row or cell), then casting the control found as a combobox, then accessing its .Text property.
I use this:
private int GetDataGridViewComboBoxCellSelectedIndex(DataGridViewCell d)
{
return ((DataGridViewComboBoxCell)d).Items.IndexOf(d.Value);
}

How do I set a ComboBox default *not in the drop down* when a list item begins with the same text as a drop down item?

Using C#, say you have a ComboBox that has a DropDownStyle set to DropDown (there are items in the drop down but the user can manually enter a value as well). How do you set a default value for the ComboBox that is not in the list of values in the drop down, but begins with text from a possible selection? Normally setting ComboBox.Text works fine, but if there is an item in the drop down that begins with the text you want as the default, it automatically selects the first item in the list that starts with the text. For example:
Values in the drop down:
c:\program files\
c:\windows\
d:\media\
Default Value Assignment
myComboBox.Text = "C:\";
Result
Initial value of ComboBox when the form opens is "c:\program files\".
So what am I doing wrong? How do I correctly set a default value of an item not in the drop down list that begins with a possible selection?
Does the following code work?
myCombo.SelectedIndex = myCombo.FindString(#"c:\");
Note: I haven't tried it. Looked up for properties/methods that could help using reflector.
I couldn't repro the behavior you are describing. Adding the three values via the Items collection and then setting the initial value to "c:\" (you omitted an # in your code sample, by the way) worked fine. My guess is that something else in your code is setting the value of the combo box after you set it.
I was able to get this to work with having the items in the ComboBox be ComboBoxItems (I dont see why this wouldn't work with other types). Set the ComboBox.Text like you are and make sure SelectedIndex = -1 and also you need IsEditable = True.

Set selected item in a databound list box

I've got a wpf listbox that is bound to a datatable. At some times, I want to programmaticly change the selection of the listbox. I know the text of the item I want to select. But it doesn't work to set the listbox1.SelectedItem to the text I want because the type of the SelectedItem is System.Data.DataRowView.
If I have the text I want to select and the DataRow that I want to select, what is the easiest way select the associated row in the list box?
Search through your DataSet and find the appropriate DataRow. Then set SelectedItem to that DataRow.
If you know the text, then it would be:
ListBox1.SelectedValue = ListBox1.Items.FindByText("Two").Value;
You can also use the SelectedIndex property to set the selected value by 0-based index.
The ListBox control (both in Forms and WebControls) has a SelectedValue property that:
"Gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value."
You could use this to select the item based on it's value, typically a unique key.
More info from MSDN:
System.Windows.Forms.ListControl.SelectedValue
System.Web.UI.WebControls.ListControl.SelectedValue

Categories

Resources