ktos1234 ktos1234 - 1 month ago 19
C# Question

ComboBox SelectedItem shows System.Data.DataRowView

I insert data from my database to

combobox
, and now I want to display value of this
combobox
into
label
, but every time instead of getting value of
combobox
, I get
System.Data.DataRowView
in my
label
.

I use this code for connection, it works fine:

OracleConnectionStringBuilder sb = new OracleConnectionStringBuilder();
sb.DataSource = "localhost";
sb.UserID = "library";
sb.Password = "library";
OracleConnection conn = new OracleConnection(sb.ToString());
conn.Open();
OracleDataAdapter TITLES = new OracleDataAdapter("SELECT NAME FROM TITLE", conn);
DataTable dt = new DataTable();
TITLES.Fill(dt);
cmbBooks.DisplayMember = "NAME";
cmbBooks.DataSource = dt;
conn.Close();


And then I want to get
SelectedItem
using this code:

label1.Text = cmbBooks.Items[cmbBooks.SelectedIndex].ToString();


How to solve it?

Answer

You can use the GetItemText method:

label1.Text = cmbBooks.GetItemText(cmbBooks.SelectedItem);
Comments