Get Value from ASPxComboBox get value - c#

I have combobox with 2 values, ID and Name. I need to get ID from selected item, and I don't know how.
ASPxComboBox1.SelectedItem.GetValue(ID);
Not working.

ASPxComboBox1.TextField = "Name"; //This is the displayMember
ASPxComboBox1.ValueField = "ID"; //This is the valueMember
ASPxComboBox1.ValueType = typeof(String);
ASPxComboBox1.DataSource = DataTableWithIDandNameColumns;
ASPxComboBox1.DataBind();
String theID = Convert.ToString(ASPxComboBox1.Value);//The column in the datasource that is specified by the ValueField property.
OR:
String theID = Convert.ToString(ASPxComboBox1.SelectedItem.GetValue("ID"));//Any column name in the datasource.
Also:
String theName = Convert.ToString(ASPxComboBox1.SelectedItem.GetValue("Name"));

Use ASPxComboBox.Value property.

A combobox can only have one value per item and this is retrieved in your case by:
ASPxComboBox1.Value
See here in the documentation.
Since the value returned will be of type object, you will need to cast this to the type originally set, e.g. String. Then you will be able to work with it.

Usually the problem, when the ASPxComboBox's SelectedItem / SelectedIndex is incorrect, occurs when the ASPxComboBox's ValueType http://documentation.devexpress.com/#AspNet/DevExpressWebASPxEditorsASPxComboBox_ValueTypetopic property is specified incorrectly.
Ensure that the ValueType is set, corresponding to the "Data Type Mappings (ADO.NET)" http://msdn.microsoft.com/en-us/library/cc716729.aspx table.

Related

DataGridViewComboBoxCell Value not being set properly

I've been trying to set the value of an individual DataGridViewComboBoxCell for the last 4 hours and I've been getting nowhere. The most common solution I've seen was to set the .Value member of the DataGridViewComboBoxCell to one of the Items, which I tried and it complained the value was not valid.
DataTable documentTypes = _codedValues.GetCodedValues(Database.DOCUMENT_TYPE_TABLE); documentTypes.Columns[Database.PROFESSION_ID_COLUMN].AllowDBNull = true;
documentTypes.Columns[Database.CODE].AllowDBNull = true;
this.cbxDocumentType.DisplayMember = Database.VALUE;
this.cbxDocumentType.ValueMember = Database.CODE;
this.cbxDocumentType.DataSource = documentTypes.DefaultView;
int rowId = this.dgvDocumentList.Rows.Add(doc.actualName, doc.fileName);
DataGridViewComboBoxCell obj = (DataGridViewComboBoxCell)this.dgvDocumentList.Rows[rowId].Cells[2];
obj.Value = obj.Items[0];
After the message box comes up that tells me DataGridViewComboBoxCell view is not valid, I see the .ToString output of the object being set, which is System.Data.DataRowValue.
Depending what your datasource contains you must use the correct casts to access the correct fields.
Try this:
DataGridViewComboBoxCell cell =
(DataGridViewComboBoxCell)this.dgvDocumentList.Rows[rowId].Cells[2];
cell.Value = ((DataRowView)cell.Items[0]).Row.ItemArray[0];
This assumes that the Items are DataRowViews and that the ValueMember is in the first field.
You can test the type by writing an intermediate step:
var item = cell.Items[0];
And use the debugger to look into the resulting type..

LINQ - Accessing a column with the column name as a string parameter

(Similar to this question)
I have tables called 'Questions' and 'FinalFigures':
Questions (QuestionID, Text, FinalFiguresColumnName)
FinalFigures (FinalFigureID, TotalDays, TotalCost, etc, etc)
'FinalFiguresColumnName' would have values like "TotalDays", "TotalCost", etc. Is there a simple way to loop through a set of Questions and have the 'Text' value saved to the corresponding column name on the 'FinalFigures' table?
Ie. instead of:
var item = new FinalFigure();
item.TotalDays = "x";
I need:
var item = new FinalFigure();
// access the column 'TotalDays' with its name as a string, not a property
You can use a bit of reflection to get the property value by the string name of the property instead of directly accessing it.
var value = (int)item.GetType().GetProperty("TotalDays").GetValue(item);
To set the value, simply call SetValue();
item.GetType().GetProperty("TotalDays").SetValue(item, value);

DisplayMember not always being set in the CheckedListBox

I have a CheckedListBox that I populate with a List of custom "DBitemtype" objects which I do through first setting the DataSource, then the DisplayMember, and ValueMember properties of the CheckedListBox "clbItemType". Now at runtime, the user has options to change the items so I have an event that rebinds the CheckedListBox with a different list of the same "DBitemtype" objects by first setting the DataSource to null, and then setting the DataSource to the new list, then setting the DisplayMember and ValueMember properties again to the exact same values like so:
clbItemType.DataSource = null;
clbItemType.DataSource = _SelectedItemTypes; // A List<DBitemtype>
clbItemType.DisplayMember = DBitemtype.Columns.ItemName; // String constant - value "ItemName"
clbItemType.ValueMember = DBitemtype.Columns.ID; // String constant - value "ID"
clbItemType.Refresh();
Both string constants used for DisplayMember and ValueMember describe public string properties in the DBitemtype class.
The issue I'm having is that sometimes the DisplayMember gets set with "ItemName" and sometimes it doesn't and just stays as "" when I step through the code. I can't seem to find a pattern as to why it happens only sometimes. No error or exception is thrown.
Al I could find was "If the new value of the DisplayMember cannot be set, the previous value is maintained." from the msdn article here CheckedListBox.DisplayMember Property
So I guess this is a 2-part question:
1) Has anyone else had this problem?
2) What are the conditions that will cause the DisplayMember to not be set?
I'm not sure if this helps, but I did find out that DisplayName does not like public fields (public int x;), only public properties (public int x { get; set; };).
So, I would try to make sure that ItemName or any other potential DisplayName values are in fact public properties of DBitemtype.
It might look the same, but:
clbItemType.DataSource = _SelectedItemTypes; // A List<DBitemtype>
clbItemType.DisplayMember = DBitemtype.Columns.ItemName; // String constant - value "ItemName"
clbItemType.ValueMember = DBitemtype.Columns.ID; // String constant - value "ID"
is not the same as:
clbItemType.DisplayMember = DBitemtype.Columns.ItemName; // String constant - value "ItemName"
clbItemType.ValueMember = DBitemtype.Columns.ID; // String constant - value "ID"
clbItemType.DataSource = _SelectedItemTypes; // A List<DBitemtype>
Set your members first, then change your DataSource. Changing a DataSource will fire event triggers and then return to the code block.

Combo Boxes With Multiple Columns

I've read that combo boxes cannot have multiple columns. Which leaves me a bit stuck since what I want to do is display one field from a table, but return the corresponding value from a second field in the same table i.e.
I'd like to show CustomerNames in the combo box, but when the user selects a name, the CustomerID field is returned instead. Whats the best work around for this?
Best way is to use ComboBoxes DisplayMember and ValueMember properties
set the ComboBox.DisplayMember to the property you want to display. Use ValueMember for property you want to return. Then you can use the ComboBox.SelectedValue to get the current/selected ValueMember
You don't need multiple columns to implement it.
class Member
{
public string Name{get;set;}
public string Address{get;set;}
public int ID{get;set;}
public string Description
{
get
{
return string.Format("{0}, {1}", Name, Address);
}
}
}
var members = new []
{
new Member(){ID = 1, Name = "John", Address = "Addr 1"},
new Member(){ID = 2, Name = "Mary", Address = "Addr 2"}
};
m_ComboBox.DataSource = members;
m_ComboBox.DisplayMember = "Description"
m_ComboBox.ValueMember = "ID";
now you can access seleceted ID
var selectedID = m_ComboBox.CelectedValue();
The value of the ComboBoxItem does not have to be the same as the Text, consider using the ID as the value.
You can achieve the desired behaviour by setting the DisplayMember and ValueMember properties of the ComboBox to "CustomerName" and "CustomerID" respectively.
Take a look at the ValueMember property. You should set this to CustomerID. After you bind to your combobox, the actual field displayed to the user will be the CustomerName, but when you want to get the value of 'CustomerName', it will return the CustomerID.
When you want to get the value of the combobox, simply reference the SelectedValue.
If you are adamant about displaying both of these in the combobox, there are some hackish ways of doing this but I would recommend reviewing your requirements again and seeing if it is absolutely necessary.
Alternatively, you can define KeyValuePair objects with your intended Ids and text fields. Feed them to the combo, since its Items property is a collection of objects. Then, for retrieval use a cast like
var x = (KeyValuePair)combo.Items[0];
and then acces then Key and Value properties of x.
You can use Tag property of ComboBoxItem to store the value.

Get the select value from a combobox that is bound to a dataset

I want to get the select value from a combobox that is bound to a dataset.
For binding the combobox I use:
cboEmployees.DataSource = ds.Tables["employees"];
cboEmployees.ValueMember = "employee_number";
cboEmployees.DisplayMember = "employee_name";
To get selected value:
string SelectedValue = cboEmployees.SelectedValue.ToString();
I got this error message: Object reference not set to an instance of an object.
Could anyone help me with this?
Thanks in advance!
you should be setting the cboEmployees.ItemsSource. The reason for the error is because your:
cboEmployees.SelectedValue is null and ToString() method can not be called on it.
EDIT: just thinking about this more and I think should be used like so:
cboEmployees.DataSource = ds.Tables("Employee");
cboEmployees.ValueMember = ds.Tables[0].Columns[0].ToString();
cboEmployees.DisplayMember = ds.Tables[0].Columns[1].ToString();
col[0] is employee number and col[1] would be employee_name
Hope this helps!!!

Categories