ComboBox 数据绑定调试时的问题
public partial class Form1 : Form{
public Form1()
{
InitializeComponent();
departmentComboBox.DataSource = LoadDepartmentData(); //该句执行完为什么跳转到departmentComboBox_SelectedIndexChanged()????????
departmentComboBox.ValueMember = "DepartmentID";
departmentComboBox.DisplayMember = "Name";
}
private DataView LoadDepartmentData()
{
string sqlConnectionString = @"Data Source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=AdventureWorks;";
string sqlSelect = "select DepartmentID,Name from HumanResources.Department";
SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
return dt.DefaultView;
}
private void departmentComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string message = "Selected:[" + departmentComboBox.SelectedValue + "]" + ((DataRowView)departmentComboBox.Items[departmentComboBox.SelectedIndex])["Name"];
MessageBox.Show(message);
}
}