adding a blank to a ComboBox

G

Guest

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
 
R

RobinS

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

Robin S.
 
B

Bryan Phillips

In cases where I am binding to a data source, I insert a dummy, blank
item in the beginning of the data source.

--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com



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

Robin S.
--------------------------------------
Matt said:
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
 
R

RobinS

That's a good idea.

Robin S.
-----------------------------------------
Bryan Phillips said:
In cases where I am binding to a data source, I insert a dummy, blank
item in the beginning of the data source.

--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com



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

Robin S.
--------------------------------------
Matt said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top