How do you add a blank to the top of a comboBox in a Windows Form? I've done
this with drop down lists in web forms but the ListItem that I used there
doesn't appear to exist in a win form. I'm loading the ddlCategory comboBox
with an ID(int) for value and Category(string) for Display out of a SQL DB
using a SP. Once these values are loaded I want to add a blank to the top of
the list and have it be selected initially. What I've tried gives me an
error of "Items collection cannot be modified when the DataSource property is
set."

private void LoadCategory()
{
// Load the Source DDL
ExpenseSpreadSheet.ExpenseClass.ExpenseClass getCategory = new
ExpenseSpreadSheet.ExpenseClass.ExpenseClass();

System.Data.DataSet ds;
ds = getCategory.GetCategory();
DataTable dt = ds.Tables[0];

ddlCategory.DataSource = ds.Tables[0];
ddlCategory.DisplayMember = "Category";
ddlCategory.ValueMember = "id";

// I have been trying to do this to add the blank.
ddlCategory.Items.Add(" ");
}

Thanks,
Matt

Re: adding a blank to a ComboBox by RobinS

RobinS
Thu Jan 18 23:54:05 CST 2007

You can't if the combobox is databound. Try setting the SelectedIndex
to -1 instead.

Robin S.
--------------------------------------
"Matt" <Matt@discussions.microsoft.com> wrote in message
news:78CBD3F6-4BEB-457E-8864-07216FF72F13@microsoft.com...
> How do you add a blank to the top of a comboBox in a Windows Form?
> I've done
> this with drop down lists in web forms but the ListItem that I used
> there
> doesn't appear to exist in a win form. I'm loading the ddlCategory
> comboBox
> with an ID(int) for value and Category(string) for Display out of a
> SQL DB
> using a SP. Once these values are loaded I want to add a blank to the
> top of
> the list and have it be selected initially. What I've tried gives me
> an
> error of "Items collection cannot be modified when the DataSource
> property is
> set."
>
> private void LoadCategory()
> {
> // Load the Source DDL
> ExpenseSpreadSheet.ExpenseClass.ExpenseClass getCategory =
> new
> ExpenseSpreadSheet.ExpenseClass.ExpenseClass();
>
> System.Data.DataSet ds;
> ds = getCategory.GetCategory();
> DataTable dt = ds.Tables[0];
>
> ddlCategory.DataSource = ds.Tables[0];
> ddlCategory.DisplayMember = "Category";
> ddlCategory.ValueMember = "id";
>
> // I have been trying to do this to add the blank.
> ddlCategory.Items.Add(" ");
> }
>
> Thanks,
> Matt