Combobox display and value member

N

Nofi

Hello,

I'm trying to fill my combobox per code, not with a DataSet because I
want to have an empty row in it and this wasn't possible when working
with a DataSet. The fill works fine, but when I set the value and
display member I get false values.

string sConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Dokumente und Einstellungen\KosiorekA\Eigene Dateien\VS2005
Projects\CromaRENT\CromaRENT-DB\CromaRENT-DB.mdb";

OleDbConnection conn = null;
try
{
conn = new OleDbConnection(sConn);
conn.Open();

OleDbCommand cmd = new OleDbCommand("select agtName
from agent", conn);
OleDbDataReader reader = cmd.ExecuteReader();

cb_mainAgent.Items.Add("");

while (reader.Read())
{
cb_mainAgent.Items.Add(reader[0].ToString());
cb_mainAgent.DisplayMember(reader[0].ToString());
}

OleDbCommand neu = new OleDbCommand("select agtid from
agent", conn);
OleDbDataReader reader1 = neu.ExecuteReader();
while (reader1.Read())
{
cb_mainAgent.Items.Add(reader1[0].ToString());
cb_mainAgent.ValueMember(reader1[0].ToString());
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

Now, when I select an entry in the combobox I get always the same ID,
no matter which display member is selected. And it is always the last,
existing ID in my table, what should I change?

Thank you.
 
M

Marc Gravell

DisplayMember and ValueMember are not methods - they are properties, so your
code doesn't even compile at the moment (be honest now...). Further, they
don't relate to any single instance, but rather to all bound instances - it
is the name of the property (of each item) in which to look (via reflection)
for the value / display fields. For instance, you might have (for
class-bound dataa):

public class Agent {
public string Name {get;}
public int ID {get;}
}

Then DisplayMember might be "Name", and ValueMember might be "ID".

You seem to be adding the names and ids separately, in 2 queries???
I think you should change your SQL to return both columns at once ("select
agtid, agtName", and use reader[0] and reader[1] to read the two columns),
and use an object like the above. You can then (for each row) create a new
Agent object with the correct id and name, and then just add that agent to
the Items via .Add; you only need to set the DisplayMember and ValueMember
once.

Marc
 
N

Nofi

Thank you for your response.
I'm sorry, display and value member aren't metods, of course. I have
made a mistake in my posting, my code for it was looking like this:

cb_mainAgent.DisplayMember = reader1.ToString();

and this do compile, but like I get the wrong ID. Sorry.
Ok, so I don't undertsand everything you have mentioned, because I'm a
beginner, I hope you have the patience to explain it to me.

So, this is what I have changed now:

OleDbCommand cmd = new OleDbCommand("select agtId, agtName from agent",
conn);
OleDbDataReader reader = cmd.ExecuteReader();

cb_mainAgent.Items.Add("");

while (reader.Read())
{
reader[0].ToString();
reader[1].ToString();
}

And I also created the new class:

public class Agent
{
public string Name{get;}
public int Id {get;}
}

But what do I do now? Creating a new Agent right? And how do I
'connect' get with my query?

Thank you.
 
M

Marc Gravell

See the following; I have separated the data-access (in the TODO block) from
the UI code to keep things simple; you don't event need to use the
data-binding (DataSource, ValueMember) if you don't want to - you could just
cast the SelectedItem to an Agent and access the ID from there... but I've
illustrated both SelectedValue and SelectedItem usage for your convenience.

Does this explain?

Marc

using System;
using System.Windows.Forms;
using System.Collections.Generic;
class Program {
static void Main() {
List<Agent> agents = new List<Agent>();
agents.Add(Agent.Empty);
// TODO: replace with your data-access code
agents.Add(new Agent("Fred", 123));
agents.Add(new Agent("Barney", 456));
agents.Add(new Agent("Wilma", 789));
agents.Add(new Agent("Betty", 101));
// END TODO

using (Form form = new Form())
using(ComboBox cb = new ComboBox()) {
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.DisplayMember = "Name";
cb.ValueMember = "ID";
cb.DataSource = agents;
cb.SelectedIndexChanged += delegate {
// shows SelectedItem usage
Agent agent = cb.SelectedItem as Agent;
// shows SelectedValue usage and re-uses agent from
SelectedItem
form.Text = string.Format("{0}: {1}", cb.SelectedValue,
agent.Name);
};
form.Controls.Add(cb);
form.ShowDialog();
}
}
}
class Agent {
private string _name;
private int _id;
public string Name { get { return _name; } set { _name = value; } }
public int ID { get { return _id; } set { _id = value; } }
public Agent(string name, int id) {
_name = name;
_id = id;
}
public readonly static Agent Empty = new Agent("", int.MinValue);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top