从用 Linq to SQL 查询填充的 ListView 获取值
Obtaining a value from a ListView that was populated with a Linq to SQL query
我正在从 Linq 填充 ListView
到 SQL 查询列表:
private void loademployeelist()
{
dbml.hrdbDataContext listpeople = new dbml.hrdbDataContext();
var q = from p in listpeople.EmpIDs
join pi in listpeople.EmpBasicFiles on p.EmpID1 equals pi.EBF_EmpID
where pi.EBF_Active == activeornot
orderby p.LastName
select new classes.Employeeselected() { FirstName = p.FirstName, LastName = p.LastName, empid=p.EmpID1};
foreach (var item in q)
{
lb_active_employee_names.Items.Add(new { LastName = item.LastName, FirstName = item.FirstName, empid = item.empid });
}
}
问题是我只想将 selected 值设为 empid
。我到处都看过,找不到获得该价值的方法。获取值的代码是这样的:
private void lb_active_employee_names_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListViewItem empsel = (ListViewItem)lb_active_employee_names.SelectedItem;
string test = empsel.GetType().ToString();
MessageBox.Show(test);
}
当我 select 之后的程序 运行 时,我一直收到错误。我试着查找错误,但我迷失在这片广阔的荒野中。我想我在概念上理解发生了什么,但我不知道如何解决它。我对 C#/Visual Studio.
还是很陌生
这里是错误:
An exception of type 'System.InvalidCastException' occurred in 6DegreesGateway.exe but was not handled in user code
Additional information: Unable to cast object of type '<>f__AnonymousType103[System.String,System.String,System.Decimal]' to type 'System.Windows.Controls.ListViewItem'.
我的 XAML 看起来像这样:
<ListView x:Name="lb_active_employee_names" HorizontalAlignment="Left" Height="135" Margin="10,148,0,0" VerticalAlignment="Top" Width="365" Grid.Column="1" Grid.Row="1" Loaded="listBox_Loaded" SelectionChanged="lb_active_employee_names_SelectionChanged" SelectedItem="{Binding Path=empid}">
<ListView.View>
<GridView>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" />
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" />
<GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding empid}" />
</GridView>
</ListView.View>
</ListView>
我尝试回顾其他人所做的事情,绑定或创建 class 等。但我越来越困惑。
我在将项目添加到列表框时使用的代码将类型更改为匿名。
foreach (var item in q)
{
lb_active_employee_names.Items.Add(new { LastName = item.LastName, FirstName = item.FirstName, empid = item.empid });
}
我改为这样:
foreach (var item in q)
{
lb_active_employee_names.Items.Add(item);
}
这样一来,列表中的所有项目,包括 empid 都保留了它们的类型(至少我认为是这样)。无论如何,我可以通过这行代码从中获取我需要的数据(empid):
var empsel = (classes.Employeeselected)lb_active_employee_names.SelectedItem;
empIdSelected = empsel.empid;
然后我可以 运行 在其他表上使用该代码进一步查询。
这不是很好,我相信有更好的方法来做到这一点。
我正在从 Linq 填充 ListView
到 SQL 查询列表:
private void loademployeelist()
{
dbml.hrdbDataContext listpeople = new dbml.hrdbDataContext();
var q = from p in listpeople.EmpIDs
join pi in listpeople.EmpBasicFiles on p.EmpID1 equals pi.EBF_EmpID
where pi.EBF_Active == activeornot
orderby p.LastName
select new classes.Employeeselected() { FirstName = p.FirstName, LastName = p.LastName, empid=p.EmpID1};
foreach (var item in q)
{
lb_active_employee_names.Items.Add(new { LastName = item.LastName, FirstName = item.FirstName, empid = item.empid });
}
}
问题是我只想将 selected 值设为 empid
。我到处都看过,找不到获得该价值的方法。获取值的代码是这样的:
private void lb_active_employee_names_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListViewItem empsel = (ListViewItem)lb_active_employee_names.SelectedItem;
string test = empsel.GetType().ToString();
MessageBox.Show(test);
}
当我 select 之后的程序 运行 时,我一直收到错误。我试着查找错误,但我迷失在这片广阔的荒野中。我想我在概念上理解发生了什么,但我不知道如何解决它。我对 C#/Visual Studio.
还是很陌生这里是错误:
An exception of type 'System.InvalidCastException' occurred in 6DegreesGateway.exe but was not handled in user code
Additional information: Unable to cast object of type '<>f__AnonymousType103[System.String,System.String,System.Decimal]' to type 'System.Windows.Controls.ListViewItem'.
我的 XAML 看起来像这样:
<ListView x:Name="lb_active_employee_names" HorizontalAlignment="Left" Height="135" Margin="10,148,0,0" VerticalAlignment="Top" Width="365" Grid.Column="1" Grid.Row="1" Loaded="listBox_Loaded" SelectionChanged="lb_active_employee_names_SelectionChanged" SelectedItem="{Binding Path=empid}">
<ListView.View>
<GridView>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" />
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" />
<GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding empid}" />
</GridView>
</ListView.View>
</ListView>
我尝试回顾其他人所做的事情,绑定或创建 class 等。但我越来越困惑。
我在将项目添加到列表框时使用的代码将类型更改为匿名。
foreach (var item in q)
{
lb_active_employee_names.Items.Add(new { LastName = item.LastName, FirstName = item.FirstName, empid = item.empid });
}
我改为这样:
foreach (var item in q)
{
lb_active_employee_names.Items.Add(item);
}
这样一来,列表中的所有项目,包括 empid 都保留了它们的类型(至少我认为是这样)。无论如何,我可以通过这行代码从中获取我需要的数据(empid):
var empsel = (classes.Employeeselected)lb_active_employee_names.SelectedItem;
empIdSelected = empsel.empid;
然后我可以 运行 在其他表上使用该代码进一步查询。
这不是很好,我相信有更好的方法来做到这一点。