Solved

Is it possible to assign programatically the combobox  valuemember - item by item ?

Posted on 2014-01-23
9
340 Views
Last Modified: 2014-01-23
Hi Experts!

Is it possible to assign programatically the valuemember - item by item on a combobox the valuemember ?

The reason is the first line must be empty.

     this.comboBox5.Items.Insert(0, "             ");

            for (int i = 1; i <= MyDT.Rows.Count; i++)
            {
                this.comboBox5.Items.Add(MyDT.Rows[i]["DESCRICAO"].ToString());

               // Item valuemember - just to ilustrate, this code doesn't run
                this.comboBox5.ValueMember = MyDT.Rows[i]["SB_PGE"].ToString();

            }

Open in new window


Thanks in advance!
0
Comment
Question by:Eduardo Fuerte
  • 3
  • 3
  • 3
9 Comments
 
LVL 9

Assisted Solution

by:dustock
dustock earned 500 total points
ID: 39803815
You shouldn't need the for statement.  This code will have the first item be blank and still be connected to your datatable.

            comboBox5.Items.Insert(0, "");

            comboBox5.DataSource = MyDT;
            comboBox5.ValueMember = "SB_PGE";
            comboBox5.DisplayMember = "DESCRICAO";

Open in new window

0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39803821
Assuming you're talking web:
     this.comboBox5.Items.Insert(0, "             ");

            for (int i = 1; i <= MyDT.Rows.Count; i++)
            {
                this.comboBox5.Items.Add(New ListItem(MyDT.Rows[i]["SB_PGE"].ToString(), MyDT.Rows[i]["DESCRICAO"].ToString()));
            }

Open in new window

0
 

Author Comment

by:Eduardo Fuerte
ID: 39803927
Hello

@dustock
Your solution doesn't work.... the first line is simply ignored when the combobox is filled.

@Angelplay
I'm using WebForms... any workaround to ListItem?
0
 
LVL 9

Accepted Solution

by:
dustock earned 500 total points
ID: 39803934
Sorry that solution, maybe this one will.

            DataRow dtRow = MyDT.NewRow();
            dtRow["SB_PGE"] = 0;
            dtRow["DESCRICAO"] = "";
            dtData.Rows.InsertAt(dtRow, 0);
            
          comboBox5.DataSource = MyDT;
            comboBox5.ValueMember = "SB_PGE";
            comboBox5.DisplayMember = "DESCRICAO";

Open in new window

0
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.

 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39803952
ListItem should work fine in WebForms.

You need to ensure System.Web.UI.WebControls is in scope i.e. either
using System.Web.UI.WebControls;

Open in new window

or
New System.Web.UI.WebControls.ListItem(text, value)

Open in new window

0
 

Author Comment

by:Eduardo Fuerte
ID: 39804051
@dustock
The code
            DataRow dtRow = MyDT.NewRow();
            dtRow["SB_PGE"] = 0;
            dtRow["DESCRICAO"] = "";
            MyDT.Rows.InsertAt(dtRow, 0);
            
            comboBox5.DataSource = MyDT;
            comboBox5.ValueMember = "SB_PGE";
            comboBox5.DisplayMember = "DESCRICAO"; 

Open in new window


Works!

@AngelPlay
I referenced:
using System.Web;

ListItem still unavailable
What is the correct .net reference for it?
0
 
LVL 9

Expert Comment

by:dustock
ID: 39804066
Well, glad I was able to help now :)
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39804085
If dustock's method is working it's a cleaner solution.

System.Web.UI.WebControls (see above). Maybe at somepoint in the future if you have something more dynamic you might need to manually add listitems.
0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 39804176
Pretty good solution!

Now my combobox is much more configurable than before.

Thanks for all!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

22 Experts available now in Live!

Get 1:1 Help Now