private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
SqlConnection con = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SARManagement;Data Source=AIZAZ\SQLEXPRESS");
con.Open();
string query = "Select Semester,ID FROM Batch";
SqlCommand da = con.CreateCommand();
da.CommandText = query;
SqlDataAdapter adapter = new SqlDataAdapter(da);
DataSet ds = new DataSet();
adapter.Fill(ds, "Batch");
Semester.ItemsSource = ds.Tables[0].DefaultView;
Semester.DisplayMemberPath = ds.Tables[0].Columns["Semester"].ToString();
Semester.SelectedValuePath = ds.Tables[0].Columns["ID"].ToString();
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
}
private void Semester_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
here how can i get combobox items.. that are listed in combobox as displaymemberpath
}
如何获取从数据库中检索的组合框作为displaymemberpath ...或共享另一种方式来检索数据并以listboxitem的形式插入数据
答案 0 :(得分:0)
DisplayMemberPath
和SelectedValuePath
应该只有一个字符串,其中包含相应列的名称。试试这个:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
SqlConnection con = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SARManagement;Data Source=AIZAZ\SQLEXPRESS");
con.Open();
string query = "Select Semester,ID FROM Batch";
SqlCommand da = con.CreateCommand();
da.CommandText = query;
SqlDataAdapter adapter = new SqlDataAdapter(da);
DataSet ds = new DataSet();
adapter.Fill(ds, "Batch");
Semester.ItemsSource = ds.Tables[0].DefaultView;
Semester.DisplayMemberPath = "Semester";
Semester.SelectedValuePath = "ID";
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
}
private void Semester_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataRowView data = (DataRowView)Semester.SelectedItem;
string selstr = data["Semester"].ToString();
int sel = (int)data["ID"];
MessageBox.Show("ID: " + sel + " Value: " + selstr);
}