Auslesen von Werten aus Combobox

Heijo

Grünschnabel
Ich bekomme beim Ausgeben des gerade selektierten Wertes einer Combobox statt des angezeigten Wertes (der Combobox) ein "System.Data.DataRowView".
Die Combobox ist an eine Datenbanktabelle eines SQL-Servers gebunden.

Irgendjemand eine Idee, wie das richtig machen muss?

Hier der Code:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace Partnerabrechnung
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Data.SqlClient.SqlConnection sqlConnectionDialogDB;
private System.Data.DataSet dataSetFirm;
private System.Windows.Forms.Label labelFirm;
private System.Data.SqlClient.SqlCommand sqlCommandGetFirm;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapterFirm;
private System.Windows.Forms.ComboBox comboBoxFirm;



public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//

//
// Laden und Anzeigen der Daten für die Controls usw.
//
this.getDBData4Form();
}

[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}

#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.comboBoxFirm = new System.Windows.Forms.ComboBox();
this.sqlDataAdapterFirm = new System.Data.SqlClient.SqlDataAdapter();
this.sqlCommandGetFirm = new System.Data.SqlClient.SqlCommand();
this.labelFirm = new System.Windows.Forms.Label();
this.dataSetFirm = new System.Data.DataSet();
this.sqlConnectionDialogDB = new System.Data.SqlClient.SqlConnection();
//
// comboBoxFirm
//
this.comboBoxFirm.DataSource = this.dataSetFirm;
this.comboBoxFirm.Location = new System.Drawing.Point(152, 32);
this.comboBoxFirm.Name = "comboBoxFirm";
this.comboBoxFirm.Size = new System.Drawing.Size(121, 21);
this.comboBoxFirm.TabIndex = 5;
this.comboBoxFirm.SelectedIndexChanged += new System.EventHandler(this.ComboBoxFirmSelectedIndexChanged);
//
// sqlDataAdapterFirm
//
this.sqlDataAdapterFirm.SelectCommand = this.sqlCommandGetFirm;
//
// sqlCommandGetFirm
//
this.sqlCommandGetFirm.CommandText = "select * from tblFirm where ysnPartner = 0 and ysnDeleted = 0";
this.sqlCommandGetFirm.Connection = this.sqlConnectionDialogDB;
//
// labelFirm
//
this.labelFirm.Location = new System.Drawing.Point(8, 32);
this.labelFirm.Name = "labelFirm";
this.labelFirm.Size = new System.Drawing.Size(144, 23);
this.labelFirm.TabIndex = 7;
this.labelFirm.Text = "Firma (abgebende):";
//
// dataSetFirm
//
this.dataSetFirm.DataSetName = "NewDataSet";
this.dataSetFirm.Locale = new System.Globalization.CultureInfo("de-DE");
//
// sqlConnectionDialogDB
//
this.sqlConnectionDialogDB.ConnectionString = "server=GH;database=Dialog35Test131;trusted_connection=false;password=dialog;User " +
"Id=dialog;";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
.....
}
#endregion
void Button1Click(object sender, System.EventArgs e)
{
try {
sqlConnectionDialogDB.Open();
MessageBox.Show("Datenbank offen");

sqlConnectionDialogDB.Close();
MessageBox.Show("Datenbank geschlossen");
} catch {
MessageBox.Show("Datenbankverbindung konnte nicht aufgebaut werden " + sqlConnectionDialogDB.ConnectionString);
}
}

void ButtonCloseClick(object sender, System.EventArgs e)
{
Application.Exit();
}

void getDBData4Form()
{

this.sqlDataAdapterFirm.TableMappings.Add("Table","tblFirm");
this.sqlDataAdapterFirm.Fill(dataSetFirm);
this.comboBoxFirm.DisplayMember = "tblFirm.strCustomId";
this.comboBoxFirm.ValueMember = "tblFirm.strCustomId";
}

void ComboBoxFirmSelectedIndexChanged(object sender, System.EventArgs e)
{
this.labelFirm.Text = this.comboBoxFirm.SelectedItem.ToString();
MessageBox.Show(this.comboBoxFirm.SelectedItem.ToString());
MessageBox.Show(this.comboBoxFirm.ValueMember.ToString());
}
}
}
 
Hi,

ich könnte mir nur vorstellen, dass die Eigenschaft "DisplayMember" falsch belegt ist.
Hast du es schonmal mit "strCustomId" anstatt "tblFirm.strCustomId" versucht ?
 
SelectedItem liefert ein Objekt zurückt. Jetzt ist es anscheinend so, dass dein DisplayMember vom Typ System.Data.DataRowView ist. Über das Property Item kannst auf deine einzelnen Spalten zugreifen bzw. sie ändern.

Das heißt du machst die Abfrage so:
Code:
void ComboBoxFirmSelectedIndexChanged(object sender, System.EventArgs e)
{
this.labelFirm.Text = ((System.Data.DataRowView)this.comboBoxFirm.SelectedItem).Item["welches_item_auch_immer"].ToString;
}

Genauere Informationen dazu:
http://msdn.microsoft.com/library/d...html/frlrfsystemdatadatarowviewclasstopic.asp
 
Vielen Dank für Eure Antworten! Die Lösung lautet:

this.labelFirm.Text = ((System.Data.DataRowView)this.comboBoxFirm.SelectedItem)["strCustomId"].ToString();

strCustomId ist einer der Spalten, die über System.Data.SqlClient.SqlCommand.CommandText angefragt werden.
 

Neue Beiträge

Zurück