【发布时间】:2018-10-25 13:06:26
【问题描述】:
我在我的数据库中添加了第二个表,其中包含我希望在新列中的数据网格的每一行的组合框中看到的不同选项。每个选项都有一个与之关联的键,它与我的主表中的一列匹配,该值是从 0 到 8 的值。我有 2 个问题,其中一个我已经解决了:
加载网格时,我希望组合框列显示与每一行的 [Status] 键对应的 [Description]。这是有效的。
我希望用户选择 [Description] 单元格并使用下拉菜单打开所有可能的 [Description] 选项,以便他可以更改项目的状态。
例如,如果某个组件处于暂停状态,用户需要在网格中选择该组件并在状态列的下拉菜单中选择“暂停”。
状态正确显示,但下拉列表中未填充 CNCComponentStatus 表中的 8 个选项。我敢打赌,我没有在 SqlDataAdapter 中正确地用第二个表填充数据集。那或者我没有正确使用 SelectedItem/ItemSource/SelectedValuePath。
这是数据网格的 xaml(仅相关部分):
<DataGrid Name="dataGrid1" ItemsSource="{Binding Path=test_3DimensionalDB}" AutoGenerateColumns="False">
<DataGridTemplateColumn x:Name="StatusColumn" Header="Status" Width="*" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="cboStatus" Text="{Binding Path=Description, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="StatusCombo" IsEditable="True" SelectedValuePath="{Binding Status}" DisplayMemberPath="{Binding Description}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
这是获取数据的方法:
Private Sub SelectQuery(ByVal company As String, ByVal status As String, ByVal component As String)
Dim com As String = "SELECT tmfCNCComponent_threed.[CNCComponentKey]
,tmfCNCComponent_threed.[CompanyID]
,tmfCNCComponent_threed.[JobNumber]
,tmfCNCComponent_threed.[LogNumber]
,tmfCNCComponent_threed.[Customer]
,tmfCNCComponent_threed.[DueDate]
,tmfCNCComponent_threed.[JobLeader]
,tmfCNCComponent_threed.[CADProgrammer]
,tmfCNCComponent_threed.[Salesperson]
,tmfCNCComponent_threed.[CNCProgrammer]
,tmfCNCComponent_threed.[ComponentDescription]
,tmfCNCComponent_threed.[ComponentFilePath]
,tmfCNCComponent_threed.[Material]
,tmfCNCComponent_threed.[ComponentSizeX]
,tmfCNCComponent_threed.[ComponentSizeY]
,tmfCNCComponent_threed.[ComponentSizeZ]
,tmfCNCComponent_threed.[QuantityShown]
,tmfCNCComponent_threed.[QuantityMirror]
,tmfCNCComponent_threed.[UpdateTime]
,tmfCNCComponent_threed.[Status]
,tmfCNCComponent_threed.[ProgStarted]
,tmfCNCComponentStatus_threed.[Description]
FROM [test_3DimensionalDB].[dbo].[tmfCNCComponent_threed]
INNER JOIN tmfCNCComponentStatus_threed
ON tmfCNCComponent_threed.Status = tmfCNCComponentStatus_threed.CNCComponentStatusKey
WHERE [ComponentDescription] " & component & " 'trode%' AND [CompanyID]='" & company & "' AND [Status]" & status & "ORDER BY [UpdateTime] DESC"
Dim Adpt As New SqlDataAdapter(com, con)
con.Open()
Dim ds As New DataSet()
Adpt.Fill(ds, "dbo.tmfCNCComponent_threed")
dataGrid1.ItemsSource = ds.Tables("dbo.tmfCNCComponent_threed").DefaultView
con.Close()
RowCount()
searchBox.Clear()
End Sub
感谢您的宝贵时间,非常抱歉。
编辑: 这是我尝试填充组合框的方式,因为我没有正确绑定。虽然我不太明白如何只显示一列而不是 DefaultView。另外,“StatusCombo”由于其保护级别而无法访问?
Dim com2 As String = "SELECT * FROM tmfCNCComponentStatus_threed"
Dim AdptStatus As New SqlDataAdapter(com2, con)
AdptStatus.Fill(ds, "dbo.tmfCNCComponentStatus_threed")
StatusCombo.ItemsSource = ds.Tables("dbo.tmfCNCComponentStatus_threed").DefaultView
【问题讨论】:
-
你能展示你用来填充 ComboBox 的代码吗?
-
我没有,我希望如果我在 xaml 中正确使用数据绑定,我将不需要任何代码。我尝试制作另一个数据适配器并再次填充数据集,但没有奏效
-
请告诉我您为填充组合框所做的一切。绝对任何地方。我不介意它是不是你不认为是“代码”的东西。
-
我在帖子底部添加了一个编辑,显示了我如何尝试在代码中填充组合框,但据我了解,我应该在 xaml 中进行数据绑定
-
好的,所以您有多个
StatusCombo,因为它位于一个模板中,该模板为表中的每一行创建了一个实例。因此,其名称的范围仅限于创建它的 DataTemplate。那是没得商量的。但这没关系;明智的是,无论如何您都不想在代码中执行此操作——您想使用数据绑定。关键问题:您有视图模型吗?
标签: c# sql wpf data-binding datagrid