我有一个组合框错误
我的代码:
SqlConnection conn = new SqlConnection();
try
{
conn = new SqlConnection(@"Data Source=SHARKAWY;Initial Catalog=Booking;Persist Security Info=True;User ID=sa;Password=123456");
string query = "select FleetName, FleetID from fleets";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.CommandText = query;
conn.Open();
SqlDataReader drd = cmd.ExecuteReader();
while (drd.Read())
{
cmbTripName.Items.Add(drd["FleetName"].ToString());
cmbTripName.ValueMember = drd["FleetID"].ToString();
cmbTripName.DisplayMember = drd["FleetName"].ToString();
}
}
catch
{
MessageBox.Show("Error ");
}
数据显示在组合框中,但是当您更改选择值成员时,显示成员不会改变。
它现在正在工作,但是当我单击按钮以显示数据时
private void button1_Click(object sender, EventArgs e)
{
label1.Text = cmbTripName.DisplayMember;
label2.Text = cmbTripName.ValueMember;
}
显示如下:
车队名称 车队
ID
它不显示值