Solved

Add "Please Select" in DataBound ComboBox

Posted on 2004-08-18
7
688 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 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