您如何从Datatable中选择特定值 [英] How do you pick particular value from the Datatable
问题描述
大家好,
我在my.vb应用程序中有一个DataTable m_dtDelayedDeliveryDays。这个数据表我加载了我的组合框。
数据表格的值如下:
ID DelayDays
6 0
8 3 >
9 7
10 15
11 20
12 30
现在我的组合框包含DelayDays。根据用户选择说3我需要ID为8.
如何在vb.net中执行此操作..请帮助。
我尝试了这个,但它没有用:
Dim selectedDataRow As Integer = DirectCast(cmbDelayedDeliveryDays.SelectedItem,DataRowView).Row
Dim delayId As Integer = Convert.ToInt32(selectedDataRow(DeliveryDelayDaysID"))
这样会抛出错误无法将类型''System.Int32'的对象强制转换为类型''System.Data.DataRowView''。
请帮忙。
谢谢。
Hi All,
I have a DataTable m_dtDelayedDeliveryDays in my.vb application.With this Data table i loaded my combo box .
The Data table has values like this:
ID DelayDays
6 0
8 3
9 7
10 15
11 20
12 30
Now my combo box contains DelayDays. Based on the user selection say 3 i need ID as 8.
How do i do this in vb.net..Please help.
I tried this but it didnt work:
Dim selectedDataRow As Integer = DirectCast(cmbDelayedDeliveryDays.SelectedItem, DataRowView).Row
Dim delayId As Integer = Convert.ToInt32(selectedDataRow(DeliveryDelayDaysID"))
This is throwing an error"Unable to cast object of type ''System.Int32'' to type ''System.Data.DataRowView''."
Please help.
Thanks.
推荐答案
使用以下代码行
use the following line of code
int cboIndex = cboCountry.SelectedIndex;
string strId = this.cboCountry.Items[cboIndex].ToString();
您可以使用DataSource绑定方法轻松访问它。
You can use the DataSource binding method to easily access this.
comboBox.DisplayMember ="DelayDays";//where DelayDays is the column name
comboBox.ValueMember = "ID"; //where ID is the column name
comboBox.DataSource = m_dtDelayedDeliveryDays ;
现在获取所选值。
Now to get the selected value.
string id = comboBox.SelectedValue.ToString();//this will give you the id 8 for the display text 3
这篇关于您如何从Datatable中选择特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!