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