wpf - 将数据网格绑定到组合框 WPF
问题描述
请帮我 !在 SQL Server 中,我创建表 temp:
CREATE TABLE temp(
user_temp nvarchar(5),
year_temp int)
表温度中的值:
user_temp year_temp
A 5
B 6
C 5
在窗口中,我创建了一个 Datagrid 和一个 ComboBox,我写道:
Private Sub Windowtemp_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim s as string="select * from temp"
Dim cmd as new New SqlClient.SqlCommand(s, connect)
Dim adaptertemp As New SqlClient.SqlDataAdapter
adaptertemp.SelectCommand = cmd
Dim tabletemp As New DataTable
adaptertemp.Fill(tabletemp)
datagridtemp.ItemsSource = tabletemp.DefaultView
cmd.Dispose()
adaptertemp.Dispose()
tabletemp.Dispose()
Dim i as interger
for i= 10 to 20
cbo.Items.Add(i)
Next
End Sub
在数据网格 selection_changed 中:
Private Sub gridtemp_SelectionChanged(sender As Object, e As SelectionChangeEventArgs) Handles gridtemp.SelectionChanged
cbo.Text = gridtemp.SelectedItem.row(1).ToString
End Sub
但是 Combobox 不显示 gridtemp 的值。我应该怎么做才能做到这一点?提前致谢!
解决方案
SelectedItem
返回一个DataRowView
。尝试这个:
Private Sub gridtemp_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles gridtemp.SelectionChanged
If Not gridtemp.SelectedItem Is Nothing Then
Dim selectedItem = CType(gridtemp.SelectedItem, DataRowView)
cbo.SelectedItem = Convert.ToInt32(selectedItem(1))
End If
End Sub
推荐阅读
- .net - 将自动代码分析添加到遗留或已启动的代码库
- javascript - 在下载 PNG 之前将 SVG 标题添加到绘图图表
- vbscript - UFT - 随机日期
- python - Does it make any difference if we change the position of operands in a logical statement?
- ios - How pass data from one ViewController to another one Xcode?
- c++ - Can concepts be used with CRTP idiom?
- azure-devops - Copy-item command is throwing Access denied error
- java - Can't add OAuth 2.0 scopes LinkedIn
- php - How to call different Ajax Calls within the same form
- asp.net - Kashflow .NET - An existing connection was forcibly closed by the remote host