views:

77

answers:

3

I am using a DevExpress LookUpEdit control. My database has two tables that control the population of control options. One called MaintCategory and one called MaintItem. This introduces a Magic Number into my program(the CategoryID) but allows for lots of runtime customization.

My problem lies in how to allow my users to un-select a dropdown WITHOUT me having to add an "Empty" Item for every CategoryID. Isn't there a way to inject a blank row? At the control level or when I construct the DataTable to return?

This is the DAL method that I use to populate all control choices.

public static DataTable GetMaintItems(int iCat)
    {
        using (var context = CmoDataContext.Create())
        {
            IQueryable<tblAdminMaintItem> tItems = context.GetTable<tblAdminMaintItem>();

            return (tItems.Where(item => item.CategoryID == iCat & item.Active == true).OrderBy(item => item.OrderID).Select(
                item => new { item.ItemID, item.ItemDescription })).CopyLinqToDataTable();
        }
    }
A: 

Hi,

Why don't you try something like this:

    dropdown.DataSource = GetMaintItems(x);
    dropdown.DataTextField = "Name";
    dropdown.DataValueField = "ID";
    dropdown.DataBind();
    dropdown.Items.Insert(0, new ListItem("Select item", "-1"));

Thanks, Flores

Flores
Thank you for your help, unfortunately I am using a *DevExpress LookUpEdit Control* and it does not have an `Items`.
Refracted Paladin
+2  A: 

I searched devexpress comunity content and found that it cannot be added directly in control.

But it can be done by manipulations with datasource. I have found the example here:

http://www.devexpress.com/Support/Center/p/AS13948.aspx

Andrey Gordeyev
+2  A: 

I actually ran into a very similar issue in an app I was working on recently using DevExpress's LookUpEdit control -- the inability to revert the control to having no selection once a selection has been made.

My workaround was:

  1. Set the LookUpEdit.Properties.AllowNullInput property to true.
  2. Add an event handler to the LookUpEdit.Validating event.
  3. In that event handler, check for a blank string as the Text property and set EditValue to null manually.

In other words, something like this:

void m_lookUpEdit_Properties_Validating(object sender, CancelEventArgs e)
{
    if (string.IsNullOrEmpty(m_lookUpEdit.Text))
    {
        m_lookUpEdit.EditValue = null;
    }
}

This allows the user to clear the text in the control, and have this effectively reset its value to null.

Dan Tao