组合框出错了
我的代码:
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;
}
显示:
FleetName
FleetID
它不显示值
答案 0 :(得分:14)
您必须完全重写代码。 DisplayMember 和 ValueMember 指向columnNames!此外,您应该使用using block
- 因此在执行查询后连接会被置于(以及已关闭)。
我没有使用 dataReader 来访问我选择 dataTable 的值,而是将其作为dataSource绑定到comboBox上。
using (SqlConnection conn = new SqlConnection(@"Data Source=SHARKAWY;Initial Catalog=Booking;Persist Security Info=True;User ID=sa;Password=123456"))
{
try
{
string query = "select FleetName, FleetID from fleets";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "Fleet");
cmbTripName.DisplayMember = "FleetName";
cmbTripName.ValueMember = "FleetID";
cmbTripName.DataSource = ds.Tables["Fleet"];
}
catch (Exception ex)
{
// write exception info to log or anything else
MessageBox.Show("Error occured!");
}
}
使用 dataTable 可能比 dataReader 慢一点,但我不必创建自己的类< / em>的。如果您确实必须/想要使用 DataReader ,您可以选择 @Nattrass方法。无论如何,你应该写一个 using block !
修改强>
如果你想获得组合框的当前值,试试这个
private void cmbTripName_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbTripName.SelectedItem != null)
{
DataRowView drv = cmbTripName.SelectedItem as DataRowView;
Debug.WriteLine("Item: " + drv.Row["FleetName"].ToString());
Debug.WriteLine("Value: " + drv.Row["FleetID"].ToString());
Debug.WriteLine("Value: " + cmbTripName.SelectedValue.ToString());
}
}
答案 1 :(得分:7)
要以您想要的方式使用Combobox
,您可以将对象传递给cmbTripName.Items.Add
方法。
该对象应具有FleetID
和FleetName
属性:
while (drd.Read())
{
cmbTripName.Items.Add(new Fleet(drd["FleetID"].ToString(), drd["FleetName"].ToString()));
}
cmbTripName.ValueMember = "FleetId";
cmbTripName.DisplayMember = "FleetName";
Fleet
班级:
class Fleet
{
public Fleet(string fleetId, string fleetName)
{
FleetId = fleetId;
FleetName = fleetName
}
public string FleetId {get;set;}
public string FleetName {get;set;}
}
或者,您可以通过使用匿名类型来完全取消对Fleet
课程的需求...
while (drd.Read())
{
cmbTripName.Items.Add(new {FleetId = drd["FleetID"].ToString(), FleetName = drd["FleetName"].ToString()});
}
cmbTripName.ValueMember = "FleetId";
cmbTripName.DisplayMember = "FleetName";
答案 2 :(得分:1)
在循环外侧,设置如下。
cmbTripName.ValueMember = "FleetID"
cmbTripName.DisplayMember = "FleetName"
答案 3 :(得分:0)
private void StudentForm_Load(object sender, EventArgs e)
{
string q = @"SELECT [BatchID] FROM [Batch]"; //BatchID column name of Batch table
SqlDataReader reader = DB.Query(q);
while (reader.Read())
{
cbsb.Items.Add(reader["BatchID"].ToString()); //cbsb is the combobox name
}
}
答案 4 :(得分:0)
SqlConnection conn = new SqlConnection(@"Data Source=TOM-PC\sqlexpress;Initial Catalog=Northwind;User ID=sa;Password=xyz") ;
conn.Open();
SqlCommand sc = new SqlCommand("select customerid,contactname from customers", conn);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("customerid", typeof(string));
dt.Columns.Add("contactname", typeof(string));
dt.Load(reader);
comboBox1.ValueMember = "customerid";
comboBox1.DisplayMember = "contactname";
comboBox1.DataSource = dt;
conn.Close();
答案 5 :(得分:0)
string query = "SELECT column_name FROM table_name"; //query the database
SqlCommand queryStatus = new SqlCommand(query, myConnection);
sqlDataReader reader = query.ExecuteReader();
while (reader.Read()) //loop reader and fill the combobox
{
ComboBox1.Items.Add(reader["column_name"].ToString());
}
答案 6 :(得分:0)
void Fillcombobox()
{
con.Open();
cmd = new SqlCommand("select ID From Employees",con);
Sdr = cmd.ExecuteReader();
while (Sdr.Read())
{
for (int i = 0; i < Sdr.FieldCount; i++)
{
comboID.Items.Add( Sdr.GetString(i));
}
}
Sdr.Close();
con.Close();
}