How to get selected value from DevExpress comboBox - c#

I'm trying to get the text from the selected value in the combo box by using:
lblMessage.Text = cmbArchivoModificado.SelectedItem.Text;
I've already set the valuefield and textfield on the combobox settings, but visual studio keep telling me this:
nullreferenceException was unhandled by user code.
Object reference not set to an instance of an object.

Usually the problem, when the ASPxComboBox's SelectedItem / SelectedIndex is incorrect, occurs when the ASPxComboBox.ValueType property is specified incorrectly.
Ensure that the ValueType is set, corresponding to the "Data Type Mappings (ADO.NET)" table.
Try to use the ASPxComboBox.Value property instead:
lblMessage.Text = cmbArchivoModificado.Value != null
? cmbArchivoModificado.Value.ToString()
: string.Empty;

Looks to me like you need to first check whether cmbArchivoModificado.SelectedItem is null.
lblMessage.Text = cmbArchivoModificado.SelectedItem == null ? "NA"
: cmbArchivoModificado.SelectedItem.Text;

Get the value on this way:
var value = comboboxExample.SelectecText;
or
var value = comboboxExample.EditValue;

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..

Dynamically populating and setting value in dropdownlist in ASP.Net

I have a form where I have 4 dropdowns Country, State, District and City. Country is populated on form load and rest are populated on selected index changed event. The sequence is On Country's selected index changed States are populated. On State-> Districts populated and on Districts -> Cities are populated.
For saving it worked fine but when i was updating value, it shows null reference error. On debugging I got, the dropdowns are not populated, whereas I was trying set values. Below is my code.
using (var manager = new LocationManager())
{
var dt = manager.GetLocationById(i);
if (dt.Rows.Count == 1)
{
Countries.SelectedValue = Countries.Items.FindByText(dt.Rows[0]["Country"].ToString()).Value;
BindStates(Convert.ToInt32(Countries.SelectedItem.Value));
States.SelectedValue = States.Items.FindByText(dt.Rows[0]["State"].ToString()).Value;
BindDistricts(Convert.ToInt32(States.SelectedItem.Value));
Districts.SelectedValue = Districts.Items.FindByText(dt.Rows[0]["District"].ToString()).Value;
BindCities(Convert.ToInt32(Districts.SelectedItem.Value));
Cities.SelectedValue = Cities.Items.FindByText(dt.Rows[0]["City"].ToString()).Value;
Pincode.Text = dt.Rows[0]["Pincode"].ToString();
ViewState["Id"] = dt.Rows[0]["LocationId"].ToString();
}
}
I am getting error: {"Object reference not set to an instance of an object."} at method
BindStates(Convert.ToInt32(Countries.SelectedItem.Value));
I think the problem is not with the population of drop downs, but with finding values, when
Countries.Items.FindByText(dt.Rows[0]["Country"].ToString())
wont find any matching value it will return null. So applying 'Value' property on null is giving the error.
you can put a null check before selecting it.
something like below:
countryDropDown.SelectedValue = countryDropDown.Items.FindByText(dt.Rows[0]["Country"].ToString()) != null ? countryDropDown.Items.FindByText(dt.Rows[0]["Country"].ToString()).Value : countryDropDown.Items.FindByText("-Please select-").Value;
Check if Countries.SelectedItem != null
#Amit Ranjan: Bind all values in page init method and at page load make the perticular value selected for all drop downs:
you are getting null reference because, there is no value already bind to the drop down.
At page init bind all values to dropdown
India
US
UK
asp:DropDownList>
At page load
DropDownList .SelectedValue= dt.Rows[0]["Country"].ToString()).Value;

What is the correct way to change selection of a DropDownList?

I am having a problem to change a selected item in a drop-down.
The way I use is (a property in the code behind which sets the new selection):
public char Candy
{
set
{
var newSelection = ddlCandy.Items.FindByValue(value.ToString());
ddlCandy.ClearSelection();
newSelection.Selected = true;
}
}
Is this a recommended and proper way?
recommended approach is to simply assign the SelectedValue property with the Value you have and the DropDownList control will find and select the proper item for you, if any.
Safe way is fist Find the given item from DropDownList and set it as SelectedValue
ListItem oListItem = DropDownList1.Items.FindByValue("yourValue");
if(oListItem != null)
{
DropDownList1.SelectedValue = oListItem.Value;
}
if you directly assign SelectedValue it may through an exception if it is not exist in the list like bellow.
'DropDownList' has a SelectedValue which is invalid because it does
not exist in the list of items.
I usually prefer to use SelectedValue:
DropDownList1.SelectedValue = "Foo";

XtraTreeList - How can I reset the value of Unbound Boolean Cell to Indeterminate?

I have an XtraTreeList with an UnboundColumn of type Boolean, i.e.:
column.UnboundType = DevExpress.XtraTreeList.Data.UnboundColumnType.Boolean;
Initially, all CheckEdits are displayed 'grayed', in the Indeterminate State, and their value is Null.
Is there any way I can reset the values of particular check boxes to this Indeterminate state?
I have tried:
treeListNode[columnID] = null;
but an Error Message Box pops up, with the message: "Null object cannot be converted to a value type."
Also:
treeListNode[columnID] = DefaultBoolean.Default;
and:
treeListNode[columnID] = CheckState.Indeterminate;
but both set the cell's value to True.
Any help would be much appreciated.
Not sure if this still helps you but the other approach that you can try is setting the unbound column type to object and later restoring it.
foreach (var column in treeListNode.Columns)
{
var tc= column as TreeListColumn;
if (tc!= null && tc.Name == columnID)
{
var originalType = tc.UnboundType;
tc.UnboundType = UnboundColumnType.Object;
treeListNode[columnID] = null;
tc.UnboundType = originalType;
break;
}
}
try:
treeListNode[columnID] = DBNull.Value
but I am not sure it works, eventually we have to set the state of the CheckEdit manually.

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