ComboBox SelectedItem显示System.Data.DataRowView

时间:2016-11-04 19:03:55

标签: c# winforms combobox

我将数据库中的数据插入到combobox,现在我想将此combobox的值显示在label中,但每次都不是获取combobox的值,我在System.Data.DataRowView中获得了label

我使用此代码进行连接,它运行正常:

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();

然后我想使用此代码获取SelectedItem

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

如何解决?

1 个答案:

答案 0 :(得分:1)

您可以使用GetItemText方法:

label1.Text = cmbBooks.GetItemText(cmbBooks.SelectedItem);
相关问题