Solved

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

Posted on 2014-01-23
9
341 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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:…
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…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

948 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