Autor Beitrag
Realnub
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37

Win XP
VS 2005
BeitragVerfasst: Mo 21.04.08 08:27 
hi, ich habe ein DataTable von diesem 2 Spalten in einer ComboBox angezeigt werden sollen - wie mache ich das?
Eine Spalte ist ja kein Problem, aber wie zusammen?

ausblenden C#-Quelltext
1:
cbox.DisplayMember = "Spalte1" + "Spalte2";					


geht natürlich nicht.
maro158
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 35



BeitragVerfasst: Mo 21.04.08 09:25 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
            DataColumn column = new DataColumn(); 
            column.DataType = System.Type.GetType("System.String"); 
            column.ColumnName = "CompanyContact"
            column.Expression = "[Company Name] + '->' + [Contact Name]";

            this.northwindDataSet.Customers.Columns.Add(column);
            DataView view = new DataView(this.northwindDataSet.Customers);

            this.customersBindingSource.DataSource = view;
            customersComboBox.DisplayMember = "CompanyContact";
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Mo 21.04.08 10:11 
@maro158
Zwei Sätze zur Erläuterung wären für einen Neuling wirklich nützlich.

@Realnub
Es kann grundsätzlich nur eine Spalte angezeigt werden. Maro schlägt deshalb vor, eine zusätzliche "berechnete Spalte" zu erzeugen, die die beiden Spalten in einer weiteren zusammenfasst; der Inhalt dieser Spalte soll dann angezeigt werden.

Eine fertige Lösung wäre z.B. Multi-Column ComboBox. Die Verwendung in einem DataGridView verlangt dann allerdings weitere Anpassungen (ist also vermutlich nichts für Anfänger).

Gruß Jürgen
Realnub Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37

Win XP
VS 2005
BeitragVerfasst: Mo 21.04.08 10:29 
danke euch beiden, werde das so machen. falls ich probleme bekommen sollte, melde ich mich natürlioch wieder.
maro158
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 35



BeitragVerfasst: Mo 21.04.08 18:26 
user profile iconJüTho hat folgendes geschrieben:
@maro158
Zwei Sätze zur Erläuterung wären für einen Neuling wirklich nützlich.


Du hast ohne Zweifel recht. Wenn ich ehrlich bin, scheute ich mich etwas davor, berechnete Spalten, DataView, IList, Properties und eventuell noch BindingSource zu erklären. Da hat sich die Gewissheit, dass mein Code selbstbeschreibend ist, wie von selbst eingestellt... Wenigstens ein Hinweis darauf wäre sicherlich sinnvoll gewesen.
Realnub Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37

Win XP
VS 2005
BeitragVerfasst: Mo 21.04.08 20:13 
ich frag am besten hier noch mal

die Combobox ist schon wie vermutet in einem DataGridView. Mein Problem ist nun sozusagen der anfängliche SelectedIndex. Wie setzt man diesen? Habe gegoogelt, aber die Tipps haben nicht funktioniert.

//Im Mom ist es so, das die Combobox(en) quasi leer erscheinen, wenn man sie nicht aufgeklappt hatte.
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Di 22.04.08 09:35 
SelectedIndex ist nicht praktisch (auch wenn es mit Klimmzügen ginge): Du benötigst ja in jeder Zeile einen anderen Wert, der vom Inhalt der Haupttabelle abhängt.

Auf das Verfahren hatte ich bereits unter ComboBox in DataGridView hingewiesen. Wichtig ist dabei vor allem DataPropertyName.

Jürgen
Realnub Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37

Win XP
VS 2005
BeitragVerfasst: Di 22.04.08 10:27 
ok, danke
steck noch nicht so in der materie drinne und komme immermal durcheinander :)
maro158
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 35



BeitragVerfasst: Di 22.04.08 10:54 
Ich gehe hier von einem Northwind-Setting aus. In einem DataGridView wird die Tabelle Orders dargestellt. Die Spalte die an Orders.[Customer ID] gebunden ist, soll als ComboBox dargestellt werden und ihre Elemente aus der Tabelle Customers beziehen. Gleichzeitig soll die ComboBox dem Benutzer aber einen berechneten Wert darstellen, der sich aus der Kombination zweier Felder der Combo-Datenquelle zusammensetzt:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
// Eine neue berechnete Spalte erstellen
DataColumn column = new DataColumn(); 
column.DataType = System.Type.GetType("System.String"); 
column.ColumnName = "CompanyContact"
column.Expression = "[Company Name] + '->' + [Contact Name]";

// ... und zur CustomersDataTable hinzufügen
this.northwindDataSet.Customers.Columns.Add(column);

// Daraus dann eine neue DataView erstellen
DataView view = new DataView(this.northwindDataSet.Customers);

// ... und die BindingSource damit initialisieren
this.customersBindingSource.DataSource = view;

// dataGridViewComboBoxColumn lädt ihre Daten aus Customers (DataSource),
// und zeigt dem Benutzer dann den berechneten Wert an (DisplayMember). 
// Tatsächlich verwendet die ComboBox jedoch Customers.[Customer ID] (ValueMember) 
// für die Bindung an Orders.[Customer ID] (DataPropertyName)
dataGridViewComboBoxColumn.DataSource = this.customersBindingSource;
dataGridViewComboBoxColumn.DataPropertyName = "Customer ID"// Orders.[Customer ID]
dataGridViewComboBoxColumn.ValueMember = "Customer ID";      // Customers.[Customer ID]
dataGridViewComboBoxColumn.DisplayMember = "CompanyContact"// Customers.[Company Name] + "->" +  Customers.[Contact Name]


Ich hoffe diesmal, etwas klarer gewesen zu sein.