ComboBox Not Returned the selectedItem - c#

i have bounded DataSource to a combox box and then set this Code
Combox1.ValueMember = "CapacityID";
Combox1.DisplayMember = "Capacitys";
it show Data with no problem , but when i want gain selectedtext it returned me "" and using selectedItem , return name of combo box !
selectedvalue return correct data .
Combox1.SelectedItem.ToString(); //return "Combox1"
Combox1.SelectedValue.ToString(); //Work Correctly
Combox1.SelectedText.ToString(); // return ""

Combox1.SelectedItem retunrs you selected ListItem object not text value of selected item
its should be like :
ListItem li = Combox1.SelectedItem;
or
Object selectedItem = comboBox1.SelectedItem;
MessageBox.Show("Selected Item Text: " + selectedItem.ToString() );
From MSDN : http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem.aspx
Combox1.SelectedText - Check Msdn for this : http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext.aspx
FROM MSdn why its return empty string - if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.

ComboBox.Text.Tostring() returned selected text and solved my problem
String status = "The status of my combobox is " + comboBoxTest.Text
SelectedText property from MSDN
Gets or sets the text that is selected in the editable portion of a ComboBox.
while Text property from MSDN
Gets or sets the text associated with this control.

Use
Combox1.SelectedItem.Text // To get SelectedText
Combox1.SelectedItem.Value // To get SelectedValue
Instead of
Combox1.SelectedItem.ToString()

So altough, your question is not really gramatically clear, to get the calue of your selected item always use
Combox1.SelectedValue
Why?
Because:
Combox1.SelectedItem
returns 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 ("").

Related

save default value in combobox when nothing is selected c#

In my winform data entry application , I have a combo box set to "Dropdown List" so the user couldn't add any values other than items present in my combo box.
but while saving if the user doesn't select any item in the combobox I need to save the form with default value("Mg") , but whenever I try to save it shows the field can't be null as it doesn't accept null values:
what I've tried during (save button click event) 1:
string unitcb = unitComboBox.Text;
finishUOMComboBox.Text = uomtxt;
try2:
finishUOMComboBox.SelectedText = "Mg";
try3:
finishUOMComboBox.ValueMember = "Mg";
try4:
finishUOMComboBox.Text = "Mg";
try5:
finishUOMComboBox.selctedindex = 0;
please help how to save "mg" in the combbox when nothing is selected or it is null while saving.
Try this way to get ComboBox selected item:
finishUOMComboBox.selectedItem.ToString();
This code returns your selection's value

Right way to set selected text of combobox in winforms?

I have a form that takes in an object in it's constructor and populates controls on the form from properties in that object. I am having an issue where I can't set a ComboBox's SelectedText property, or at least it isn't working how I expect it to.
public Form(ValueHoldingObject obj)
{
// yeah I know this is not a very clean way to populate the combobox, the issue
// isn't limited to the combobox so I don't think this is relevant
List<int> items = Repo.GetAllItems().Reverse();
foreach (int id in checkInPrizeIds.Take(100))
// Insert at beginning to put more recently used items at the top
combobox.Items.Insert(0, id);
combobox.DropDownHeight = 200;
combobox.SelectedText = obj.StringProperty;
}
When I am testing this form the text of the combobox isn't being populated. If I add a breakpoint on the line where I assign the text it DOES get assigned, so some event is firing (multiple focus change events probably) and making it work the way I want. Obviously I can't use a breakpoint as a fix in production code. Am I assigning this value incorrectly? Should I be using a different method to populate the values?
Further testing has reviled that it isn't just the combobox, all of my controls are only being populated correctly if I have the breakpoint.
In the constructor, you need to set the selected item, for example:
foreach ( var item in combobox.Items )
if ( (string)item == obj.StringProperty )
combobox.SelectedItem = item;
Or:
foreach ( var item in combobox.Items )
if ( (int)item == Convert.ToInt32(obj.StringProperty) )
combobox.SelectedItem = item;
It's confusing but despite its name, the property SelectedText is not really the selected item... because combo box items are objects and not strings: texts shown are a representation of the item objects using ToString().
Therefore setting the selected text will not guarantee to select an item and we can prefer setting the SelectedItem.
In addition to these considerations, you set the selected text property in the constructor after populating the combo box and that can cause problems because it is before the form and the control are drawn or something like that... that is to say perhaps before the ToString() methods are called on items to prepare the visual cache, so setting the selected text can't get a match with the list.
Setting the selected text selects an existing item if done in the form load or shown events.
private void Form_Load(object sender, EventArgs e)
{
combobox.SelectedText = obj.StringProperty;
}
ComboBox.SelectedText doesn't give me the SelectedText
ComboBox.SelectedText Property

Current selected value in DataGridView using C#

I'm trying to get the current selected value from a DataGridView
MessageBox.Show(""+dataGridView1.SelectedCells.ToString()+"")
but it never shows the selected value.
It shows
System.Windows.Forms.DataGridViewSelectedCellCollection
you should do it this way
MessageBox.Show(dataGrdiView1.SelectedCells[0].Value.ToString());
try to access Value or Text of a single cell, and not the entire collection
you can also iterate through the entire SelectedCells collection
string text;
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
//MessageBox.Show(cell.Value.ToString());
text +=cell.Value.ToString();
}
MessageBox.Show(text);
If it always a single cell selected you could just use. dataGridView1.SelectedCells[0].tostring. The reason you are getting the class name ia because you are getting back a collection of selected cells as their couls be more than one cell selected at a time.
string message = string.Empty;
foreach (var c in _dataGridView1.SelectedCells)
message += " " + c.Value.ToString();
MessageBox.Show(message);

combobox item selected To be fixed

I had a databound combobox in my windows form I populate it by a function deptload() IN FORM LOAD
public void DeptcomboLoad()
{
DataTable dt = depttrans.getDeptName();
Cmb_Department.DataSource = dt;
Cmb_Department.DisplayMember = "DepartmentName"; //CHAR
Cmb_Department.ValueMember = "DepartmentPK"; //INT
}
Now when an employee of a department (say accounts DepartmentName="Accounts " , DepartmentPK=23 ) login I want the ComboBox text to be selected as "acounts "
and when I go to get the selected value of the ComboBox I should get 23
I tried
Cmb_Department.selectedtext="Accounts"
Cmb_Department.Text="Accounts"
but its not giving the selected value
Can anyone give a suggestion
Instead of trying to put a value INTO the combobox, try to GET the SelectedItem like this:
string txt= Cmb_Department.SelectedItem.Text
or just:
string txt= Cmb_Department.SelectedText
To change selected value of the combobox you can use
SelectedItem property or SelectedIndex.
Index must be exact number in your data sourse, and Item must be exact object from datasource
You can get it to select the right item by issuing something like this:
Cmb_Department.SelectedValue = 23;
Where 23 comes from some other variable, maybe on another object, maybe from a local variable, whatever works in your case.
Now, to get the selected value you can use this statement:
var val = Cmb_Department.SelectedValue;
To get the selected text (which would be the text associated with the value):
var text = ((DataRow)Cmb_Department.SelectedItem)["DepartmentName"];
The reason I'm prescribing the aforementioned is because the SelectedText property is volatile, and the Text property doesn't always work based on how the DropDownStyle is set.
However, some would probably argue to get the same as the aforementioned you could issue this statement:
var text = Cmb_Department.Text;

ComboBox.SelectedText doesn't give me the SelectedText

I am building a String and the code looks like
String status = "The status of my combobox is " + comboBoxTest.SelectedText
I am using WinForm in VS2010
The result looks like
"The status of my combobox is "
I think you want to use
String status = "The status of my combobox is " + comboBoxTest.Text
SelectedText property from MSDN
Gets or sets the text that is selected in the editable portion of a
ComboBox.
while Text property from MSDN
Gets or sets the text associated with this control.
From the documentation:
You can use the SelectedText property to retrieve or change the currently selected text in a ComboBox control. However, you should be aware that the selection can change automatically because of user interaction. For example, if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.
When the combo box loses focus, the selection point moves to the beginning of the text and any selected text becomes unselected. In this case, getting the SelectedText property retrieves an empty string, and setting the SelectedText property adds the specified value to the beginning of the text.
I face this problem 5 minutes before.
I think that a solution (with visual studio 2005) is:
myString = comboBoxTest.GetItemText(comboBoxTest.SelectedItem);
Forgive me if I am wrong.
I think you dont need SelectedText but you may need
String status = "The status of my combobox is " + comboBoxTest.Text;
To get selected item, you have to use SELECTEDITEM property of comboBox. And since this is an Object, if you wanna assign it to a string, you have to convert it to string, by using ToString() method:
string myItem = comboBox1.SelectedItem.ToString(); //this does the trick
Try this:
String status = "The status of my combobox is " + comboBoxTest.text;
Here's how I would approach the problem, assuming you want to change the text of say, a label
private void comboBoxtest_SelectedIndexChanged(object sender, EventArgs e)
{
var combotext = comboBoxtest.Text;
var status = "The status of my combo box is" + combotext;
label1.Text = status;
}
All of the previous answers explain what the OP 'should' do. I am explaining what the .SelectedText property is.
The .SelectedText property is not the text in the combobox. It is the text that is highlighted. It is the same as .SelectedText property for a textbox.
The following picture shows that the .SelectedText property would be equal to "ort".
If you bind your Combobox to something like KeyValuePair, with properties in the constructor like so...:
DataSource = dataSource,
DisplayMember = "Value",
ValueMember = "Key"
so dataSource is of type KeyValuePair...
You end up with having to do this...
string v = ((KeyValuePair)((ComboBox)c).SelectedItem).Value;
(I had a Dynamic form - where c was of type Control - so had to cast it to ComboBox)
If you just want to know the text in the ComboBox with the editable text box (or the ComboBoxStyle.DropDown style) you can use this:
string str = comboBox.SelectedItem != null ?
comboBox.GetItemText(comboBox.SelectedItem) : comboBox.Text;
or try this code
String status = "The status of my combobox is " + comboBoxTest.SelectedItem.ToString();

Categories

Resources