我填充一個ListView
從LINQ到SQL查詢列表與此:獲得從使用一個LINQ填充到SQL查詢一個ListView值
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 });
}
}
的問題是,我只是想選擇的值成爲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);
}
我一直在運行程序時後,我選擇它得到一個錯誤。我試圖查找錯誤,但我迷失在這片廣袤的荒野中。我想我在概念上理解發生了什麼,但我不知道如何解決它。我對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>
我試圖審查別人怎麼做,綁定或創建一個類等但我越來越困惑。
你是什麼意思'我只想選定的值是empid'?你想用'empid'做什麼? –
看看這個類似的問題。我認爲答案會有所幫助。 http://stackoverflow.com/questions/15091400/get-single-listview-selecteditem – 2016-06-01 02:30:24
@JasonBoyd我將在另一張桌子上運行Empid的另一個查詢。 –