Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Add "Please Select" in DataBound ComboBox

Posted on 2004-08-18
7
687 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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