0

我有一个绑定到Datable[ employee] 的列表框,其中列表框显示员工姓名。

如何从数据库中获取所选项目的id喜欢?employee_id

这是我的部分,如果代码

XAML-

ListBox ItemsSource="{Binding}" DisplayMemberPath="employee_name" HorizontalAlignment="Left" Margin="16.362,108.171,0,44.541" Name="lstbxEmloyee" Width="333.603" SelectionChanged="lstbxEmployee_SelectionChanged" 

。CS

EmployeeData _employeeData = new EmployeeData();

_employeeData.GetEmployeeName();

lstbxEmloyee.ItemsSource = _employeeData._dataTable.DefaultView;

我想从列表框中获取employee_id所选内容employee_name

请帮帮我

谢谢你....

4

2 回答 2

0

你可以试试这个:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
         // you can bind your data as per your requirement
        lstbxEmloyee.ItemsSource = GetTable().DefaultView; 
    }

    private void lstbxEmloyee_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      DataRow dr=((DataRowView) (lstbxEmloyee.SelectedItem)).Row;
      int Emp_id = Convert.ToInt32(dr["employee_ID"]);
      MessageBox.Show(Emp_id.ToString());
    }

    DataTable GetTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("employee_ID", typeof(int));
        table.Columns.Add("employee_name", typeof(string));        

        table.Rows.Add(1,"David");
        table.Rows.Add(2,"Sam");
        table.Rows.Add(3,"Christoff");
        table.Rows.Add(4, "Janet");
        table.Rows.Add(5, "Melanie");

        return table;
    }     
于 2013-02-21T08:43:47.680 回答
0

是的,我做到了。我为我的问题找到了解决方案我在代码中进行了如下更改 XAML- ListBox ItemsSource="{Binding}" DisplayMemberPath="employee_name" SelectedValuePath="employee_id " Horizo​​ntalAlignment="Left" Margin="16.362,108.171 ,0,44.541" 名称="lstbxEmloyee" 宽度="333.603" SelectionChanged="lstbxEmployee_SelectionChanged"

.cs 字符串 _selectedProject = lstbxProjects.SelectedItem.ToString();

它在“SelectedValuePath”和“SelectedValue”之间绑定,其中 - SelectedValuePath - 引用数据表 SelectedValue 中的哪一列 - 从

于 2013-02-22T06:28:40.380 回答