Re: Combobox display und value member



Hello Anna,

Die Werte für Display und Valuemember sind Eigenschaftsnamen, welche jedes Object in der DataSource-Auflistung anbieten muss.
Wenn nichts angegebn wird, wird einfach die ToString() Methode ausgeführt.

Dadurch dass deine eingefügten Werte strings sind, kann deine Data oder Value Eigenschaft nur eine Property des String datentyps sein.

Deine ComboBox weiss nichts von deinem Reader, damit kannst du auch keine Felder des Readers als Member angeben. Wobei du noch aufpassen musst, dass du nicht, wie in deinem Beispiel, FeldWerte sondern FeldName angibst.

Wenn du keine DataSource benutzen willst, brauchst du ein Object, welches deine Display und ValueMember- Werte aufnimmt und entsprechende Properties für den Zugriff bietet, etwas in der Art:


public class cboItem
{

private string dispVal = string.Empty;
private string dataVal = string.Empty;

public cboItem(string displayValue, string dataValue)
{
this.dispVal = displayValue;
this.dataVal = dataValue;
}



public string DisplayValue
{
get { return this.dispVal; }
}

public string DataValue
{
get { return this.dataVal; }
}


public override string ToString()
{
return this.DisplayValue;
}

}


// Usage

while (reader.Read())
{
cboItem ci = new cboItem(reader[0].ToString(), reader[1].ToString());
cb mainAgent.Items.Add(ci);
}


cb mainAgent.DisplayMember = "DisplayValue";
cb mainAgent.ValueMember = "DataValue";


Der Hintergrund ist, dass aus deinen einzelnen Elementen im Add(object) ein AuflistungsObject generiert wird und die ComboBox für jedes Element in der Liste nach einer Eigenschaft sucht, welche den Namen, den du in Display oder ValueMember eingetragen hast, hat und deren Rückgabewert benutzt, um die Felder zu füllen.




Hallo,

meine Combobox wird 'von Hand' gefüllt, damit ich eine leere Zeile
einfügen kann. Jetzt habe ich aber ein Problem mit dem display und
value member. Ich weiß nicht, wie ich meiner Box sagen soll, dass eine
Splate der Abfrage das display und die andere das valu member sein
soll.

Im Moment wird meine Box so gefüllt:

OleDbCommand cmd = new OleDbCommand("select agtId, agtName from agent
order By agtId ", conn);
OleDbDataReader reader = cmd.ExecuteReader();
cb mainAgent.DataSource = null;
cb mainAgent.Items.Add("");

while (reader.Read())
{
cb mainAgent.Items.Add(reader[0].ToString());
}
Jetzt habe ich schon einige Kombinationen probiert z.B.:

cb mainAgent.DisplayMember = reader[0].ToString(); cb
mainAgent.ValueMember = reader[1].ToString();

Aber es wird immer eine falsche ID übergeben, immer die letzte in
meiner Tabelle und nie die zu dem Namen gehörende. Und schließlich
bekomme ich die Fehlermeldung (wenn ich einen Eintrag in der Combobox
auswähle): 'Index Out Of Range Exception was unhandled. There is no
row at position 0.'

Wie mache ich das am besten? Irgendwie habe ich was von einer eigenen
Item Klasse gehört, aber wie schreibe ich eine? (Ein Beispiel würde
mir sehr helfen)

Danke im voraus.



.



Relevant Pages

  • Re: ComboBox - Problem beim Auslesen von SelectedValue
    ... DataSource für Deine Combobox ist und um welchen Datentyp es sich dabei ... Daten sind in einer DataTable und damit die Combobox weiss, ... Sub Combobox_SelectedIndexChanged ... ComboBox.SelectedValue einen String zurück. ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: newbie help for winform combo box needed
    ... You can create your own ListItem class or just put "30" and override the drawing algorithm to display the item text + " days". ... public string value1; ... The ComboBox will call ToString to determine what to display. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: help - populating a combox box from a text file and updating a listbox automatically
    ... The item in the ComboBox have some text that should be displayed in the TextBox. ... This text is taken from another file and can be shared among several items in the ComboBox. ... you could also put it in the same file as the ComboBox values and store the text along with the display value. ... public string Display ...
    (microsoft.public.dotnet.languages.csharp)
  • Data manipulation before binding
    ... I'm using data binding to display the values of a dataset in a form. ... I fill a combobox with custom objects that basically have two ... In the combobox, I need to select an item based on the value of the ... dataset (after having appropriately converted from string to int). ...
    (microsoft.public.dotnet.languages.csharp)
  • Filtering ComboBox based on entered text
    ... I am trying to alter the Rowsource of a ComboBox to display only records ... ComboBox loose their displayed contents when I ... Dim strWhereSQL As String ...
    (microsoft.public.access.forms)

Loading