Solved

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

Posted on 2014-01-23
9
348 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
[X]
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
  • 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
Industry Leaders: 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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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