将 DataTable 绑定到 ComboBox,返回所选项目的主键 WPF c#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14692835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding DataTable to ComboBox, Returning Primary Key of Selected Item WPF c#
提问by JeffG
This may seem a really simple question but I just cannot figure this out (I am new to XAML). I am populating a comboBox by binding it to a datatable. The primary key in my table is S_ID, I need to retrieve this value for the item selected on the SelectionChanged event so I can then load more details based on this value. I have tried int i = cboSites.SelectedItem, but get a type mismatch even though S_ID is an int. I can view the value in a DatarRowView in debug mode if I set i to type object, but cannot access the data any other way. Please let me know what I am doing wrong. I have done lots of research and from what I read the code looks to be ok, but obviously isn't! By the way the combo box populates with the data absolutely fine. Thanks very much.
这似乎是一个非常简单的问题,但我就是想不通(我是 XAML 的新手)。我通过将组合框绑定到数据表来填充它。我的表中的主键是 S_ID,我需要为在 SelectionChanged 事件上选择的项目检索此值,以便我可以根据此值加载更多详细信息。我尝试过 int i = cboSites.SelectedItem,但即使 S_ID 是一个 int,也会出现类型不匹配。如果我将 i 设置为类型对象,我可以在调试模式下查看 DatarRowView 中的值,但不能以任何其他方式访问数据。请让我知道我做错了什么。我做了很多研究,从我读到的代码来看,看起来还可以,但显然不是!顺便说一下,组合框填充的数据绝对没问题。非常感谢。
C#
C#
try
{
SqlDataAdapter sqlAd = new SqlDataAdapter();
SqlCommand sqlComm = new SqlCommand("SP_SelectAllSiteNamesForCust", sqlConn);
sqlComm.CommandType = System.Data.CommandType.StoredProcedure;
DataSet dSet = new DataSet();
sqlAd.SelectCommand = sqlComm;
sqlAd.Fill(dSet);
DataTable sitesTable = dSet.Tables[0];
cboSites.ItemsSource = sitesTable.DefaultView;
}
XAML
XAML
<ComboBox Height="28" IsEditable="True" Margin="630,15,28,10"
Name="cboSites" TabIndex="6"
ItemsSource="{Binding}"
DisplayMemberPath="S_Name"
SelectedItem="{Binding Path=S_ID}"
SelectionChanged="cboSites_SelectionChanged" />
SQL Stored Proc
SQL 存储过程
SELECT
[S_ID],
[S_Name]
FROM
[dbo].[tbl_Site]
WHERE [C_ID] = @CUSTID
回答by Federico Berasategui
Use SelectedValue instead of SelectedItem:
使用 SelectedValue 而不是 SelectedItem:
<ComboBox ItemsSource="{Binding}" DisplayMemberPath="S_Name"
SelectedValuePath="S_ID" SelectedValue="{Binding Path=Int_Property_In_The_ViewModel}"/>
回答by Blablablaster
You are getting a type mismatch because every item in your collection is like a row in a table from DataView you are binding to. So in your cboSites_SelectionChanged you can get S_ID something like this:
您遇到类型不匹配的问题,因为您的集合中的每个项目就像您绑定到的 DataView 表中的一行。因此,在您的 cboSites_SelectionChanged 中,您可以获得如下所示的 S_ID:
var s_id = int.Parse(((DataRowView)cboSites.SelectedItem)["S_ID"].ToString());
Here you are casting cboxSites.SelectedItemto DataRowViewtype, then using indexer to get S_ID value. Then you just convert that value to string and parsing it into integer variable.
在这里,您要cboxSites.SelectedItem进行DataRowView类型转换,然后使用索引器获取 S_ID 值。然后您只需将该值转换为字符串并将其解析为整数变量。

