Solved

Add "Please Select" in DataBound ComboBox

Posted on 2004-08-18
7
682 Views
Last Modified: 2008-02-26
Hi,

I have a databound combobox which bound to a datatable. I want to put the initial value in my combobox, such as "Please Select Item". However, I've tried setting the Text property and also try to insert a new row without any luck. It seems after the combobox bounded to a datatable, its items can't be changed anyway.

Could someone help me with some tips?

Thanks in advance

MI
0
Comment
Question by:MiBlg
  • 4
  • 3
7 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 75 total points
ID: 11828002
Hi,
i thinkg that you will have to add the "Please select item" in the datatable before bind it, like this

DataRow dr = dataTableObject.NewRow();
dr[index of the valuemember] = 0;
dr[index of the displaymember] = 'Please Select Item';
dataTableObject.Rows.InserAt(dr, 0)

Regards,
B..M
0
 
LVL 1

Author Comment

by:MiBlg
ID: 11910638
Hi mmarinov,

Sorry for the late reply.

 I've followed your suggestion but, it's failed when the combox has to be intialized to a value, for example, the 3rd item should be selected.

Your suggestion works perfectly only if the 1st item selected.

Any more ideas?

0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11914916
what do you mean by the "initialized" this is a common method of inserting data in a datatable
there didn't mension about combobox
when you have the filled datatable object how do you bind the combox to it ?

B..M
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:MiBlg
ID: 11919341
I mean the scenario is like this:

I've populated the combobox using your way:

DataRow dr = MyDataTableNewRow();
dr["Value"] = 0;
dr["Text"] = 'Please Select Item';
MyDataTable.Rows.InserAt(dr, 0)
myCombo.DataSource = myDataTable
myCombo.ValueMember = "Value"
myCombo.DisplayMember = "Text"

When The Form is in Add Mode, the selected item in the combo box is "Please Select Item" (1st item). This works fine.

However, when the Form is in Editing, I have to make myCombo show the value based on the database. For example:

mycombo.SelectedValue = "blabla"

It throws an error when doing this, but when there's no "Please select" it works fine. (I remove the code to insert new row)

What's wrong here?
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11919582
what error do you receive ?

B..M
0
 
LVL 1

Author Comment

by:MiBlg
ID: 11950461
I Initialize the dr["Value"] to int32.MaxValue, then the error is Index out of range.
0
 
LVL 1

Author Comment

by:MiBlg
ID: 12004013
I still don't know how to figure this out, but anyway you have already pointed me out the way. Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now