Solved

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

Posted on 2014-01-23
9
345 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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