Solved

Add "Please Select" in DataBound ComboBox

Posted on 2004-08-18
7
683 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

19 Experts available now in Live!

Get 1:1 Help Now