Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2007

Tip: Looking for answers? Try searching our database.

adding a blank to a ComboBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matt - 19 Jan 2007 00:24 GMT
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
RobinS - 19 Jan 2007 05:53 GMT
You can't if the combobox is databound. Try setting the SelectedIndex
to -1 instead.

Robin S.
--------------------------------------
> How do you add a blank to the top of a comboBox in a Windows Form?
> I've done
[quoted text clipped - 33 lines]
> Thanks,
> Matt
Bryan Phillips - 19 Jan 2007 11:45 GMT
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.
[quoted text clipped - 38 lines]
> > Thanks,
> > Matt
RobinS - 19 Jan 2007 21:43 GMT
That's a good idea.

Robin S.
-----------------------------------------
> In cases where I am binding to a data source, I insert a dummy, blank
> item in the beginning of the data source.
[quoted text clipped - 49 lines]
>> > Thanks,
>> > Matt

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.