Combo Box Question - c#

I have added items in a combo box using datatable and rows. First it returned the value, and now it's returning dataRow. I don't know the reason why it's returning like that. Can anyone help?
string selectedValue = cbx_language.Text.ToString();

It should be
string selectedValue = cbx_language.SelectedValue

Use string selectedValue = cbx_language.SelectedItem.ToString
This way, it does not matter what type of items you add using Items.Add/Insert.

Is the value you are getting is System.data.datarow?
& if It is then Please set valueMember & DisplayMember properties of combo box to the column name u fetching in datatable.

try this
string selectedValue = cbx_language.SelectedValue.ToString()

Related

Retrieve Data from ComboBox selection to TextBlock

I have populated the comboBox with the Data from SQLite
Here's how I did it.
conn.CreateTable<AccountName>();
var query = conn.Table<AccountName>();
myComboBox.ItemsSource = query.ToList();
So when I try to retrieve the selected ComboBox Item to a TextBlock, I get the output as
'Apps.Models.AccountName'
How can I retrieve the values?
Look for SelectedValue, SelectedItem Gets currently selected item in the ComboBox. Use either of these.
comboBox1.SelectedValue;
or
comboBox1.GetItemText(comboBox1.SelectedItem);
I finally found the answer by using this code.
Demo.Text = ((AccountName)myComboBox.SelectedItem).AccName;

display the display member of a combo box for a Given integer Value member

I have a ComboBox in my project which I have assigned a DisplayMember and a ValueMember. I want the ComboBox to display the relevant DisplayMember when a ValueMember value is given to the ComboBox.
Code examples will be appreciated.
Thank you
Just simple;
comboBox1.SelectedValue = "2"; // it will show you respected display member

How to get the TEXT of Datagridview Combobox selected item?

How to get the Combobox selected item text which is inside a DataGridView?
I have tried using the below code:
dataGridView1.Rows[1].Cells[1].Value.ToString()
But, this gives the value associated with this cell, not the Combobox selected item text.
I also tried this:
DataGridViewComboBoxCell cell = dataGridView1[1,1] as DataGridViewComboBoxCell;
string value = cell.Value.ToString();
But, this also didn't help.
I would appreciate your help. Thanks in advance!
EDIT:
Let's say, we have a Combobox with text as No and Yes and the values as 0 and 1 respectively. What I want to get here's the text Yes or No, when the Combobox is changed. But what I am getting is the values 0/1 using the above codes. Hope that makes things clear.
UPDATE:
Ok, so I have been working on this issue and after lots of efforts and with help from my fellow members, I have been able to resolve the issue and get the required solution:
Here's the solution:
string SelectedText = Convert.ToString(dataGridView1.Rows[0].Cells[1].FormattedValue.ToString());
To get selected value and selected text of Combobox in DataGridView try following 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 managed to pull that string value out of the cell this way:
DataGridViewComboBoxCell dgvcmbcell = dataGridView1[1, 0] as DataGridViewComboBoxCell;
String text = dgvcmbcell.EditedFormattedValue.ToString();
Easiest way to figure this out is use the debugger and look into the dgvcmdcell object. In this you will find the expandable node "base". Expand it and just look through it and you will find whatever information you need.
Also try this:
DataGridView.CurrentRow.Cells(Column.name).EditedFormattedValue.ToString()
EditedFormattedValue object gives the current, formatted value of the cell in DataGridView, regardless of the cell is edited or in edit mode. It's more convenient to capture the ComboBox selection or any value of cell in DataGridView.
To access the currently selected text in the datagridview, you need a reference to the CurrencyManager of the Combobox column. The CurrencyManager has nothing to do with money but instead manages the binding between the the column and it's datasource. The CurrencyManager can tell you what the current selection of the combobox is.
Teh codes:
CurrencyManager cm = (CurrencyManager)DataGridView1.BindingContext[((System.Windows.Forms.DataGridViewComboBoxColumn)DataGridView1.Columns[0]).DataSource];
Note: it is not necessary to cast the column to a combobox, i just did that to show you what column I was passing in. I used index 0 but use whatever index is the actual index of your combobox column.
Now using the currency manager you can access the current selection of the datagrid for that column (because that was the column you passed in).
cm.Current; //returns the current selection whatever that is
So in my case the datasource of the combobox column was a class called Choice, choice looks like this:
public class Choice
{
public string Text
{
get;
set;
}
}
When I access the cm.Current property it will return an instance of the choice class, I can now access the Text property of my choice class to see what value was selected. You will obviously have to adapt this to your particular situation. I hope this helps.
You could Try this :-
dataGridView1.CurrentRow.Cells[0].Value.ToString();
Index the column of the cell you need to look at, whichever is the index of your ComboBoxColumn.

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);
}

c# select a value in combobox

My combobox in c# windows forms is filled with data from my database...
Display Members are strings, value members are ints
I now have to preselect it before showing the form.
I have tried:
combobox.DisplayMember = string;
combobox.Text = string;
combobox.SelectedItem = string;
combobox.SelectedText = string;
combobox.SelectedValue = string;
Anyone that can give me a little help?
Would be much appriciated :-)
EDIT : ei. maybe solution for others...
Remember that the load created by VS2010 designer is loaded after constructor. not within initializeComponents(), as I thought.
If your ComboBox is data-bound and you have properly set up the DisplayMember and ValueMember properties, then you can simply set the SelectedValue property to the value of the item you want to select.
For example, if you had the following objects in your combo box:
ID Description
-- -----------------
2 Lorem
4 Ipsum
6 Dolor
8 Sit
You would set the DisplayMember to "Description" and ValueMember to "ID". Then in order to select the item "Dolor", you would just set SelectedValue = 6.
Find the Item then set the SelectedItem property of combobox to true.
EDIT:
comboBox.SelectedItem = comboBox.Items.Cast<string>().First(o => o == "blala");
use the Cast<string>() if your Items is string, Quick Qatching the combobox.Items will show you the object.
In a case that I can't remember exactly whether it was winforms or not, you should set the selected Item's selected property to false, then set another one to true.
check it and if that's the case just add this line:
combobox.SelectedIndex = -1;
Use ComboBox.SelectedIndex.
E.g.:
myComboBox.SelectedIndex = [index of item to select];
Note that ComboBox.Items is an ObjectCollection, which has a method called IndexOf(). Pass it a reference to the object you want to select, and you should get the proper index back.
Another way:
combobox.SelectedIndex = combobox.FindStringExact("myString")

Categories

Resources