PC Review


Reply
Thread Tools Rate Thread

adding a blank to a ComboBox

 
 
=?Utf-8?B?TWF0dA==?=
Guest
Posts: n/a
 
      19th Jan 2007
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
 
Reply With Quote
 
 
 
 
RobinS
Guest
Posts: n/a
 
      19th Jan 2007
You can't if the combobox is databound. Try setting the SelectedIndex
to -1 instead.

Robin S.
--------------------------------------
"Matt" <(E-Mail Removed)> wrote in message
news:78CBD3F6-4BEB-457E-8864-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Bryan Phillips
Guest
Posts: n/a
 
      19th Jan 2007
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



"RobinS" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed):

> You can't if the combobox is databound. Try setting the SelectedIndex
> to -1 instead.
>
> Robin S.
> --------------------------------------
> "Matt" <(E-Mail Removed)> wrote in message
> news:78CBD3F6-4BEB-457E-8864-(E-Mail Removed)...
> > 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


 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      19th Jan 2007
That's a good idea.

Robin S.
-----------------------------------------
"Bryan Phillips" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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
>
>
>
> "RobinS" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed):
>
>> You can't if the combobox is databound. Try setting the SelectedIndex
>> to -1 instead.
>>
>> Robin S.
>> --------------------------------------
>> "Matt" <(E-Mail Removed)> wrote in message
>> news:78CBD3F6-4BEB-457E-8864-(E-Mail Removed)...
>> > 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

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a blank row, or "All" to top of Combobox list alfaista Microsoft Access Form Coding 2 12th Mar 2010 12:25 PM
combobox blank deb Microsoft Access 1 29th Jan 2010 08:06 PM
When Combobox is blank, textboxes should be blank RichW Microsoft Access VBA Modules 7 2nd Nov 2009 07:53 PM
Adding a blank row to top of ComboBox Jay Douglas Microsoft Dot NET Framework Forms 3 15th May 2006 07:12 PM
adding a blank row to a combobox Sam Microsoft VB .NET 2 3rd May 2005 03:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:24 AM.