我有一个数据网格,我已经添加了两个按钮列。加号和减号按钮,如果你愿意的话。我想要做的是当用户点击加号按钮时,另一列值增加1。相反,单击减号按钮会减去一个 - 如果大于0.到目前为止,这就是它的样子:
这是'标准包需要'列,我希望这个数字出现。我对WPF相对较新,因此大多数此类内容都令人困惑。无论如何,这是我的代码:
<DataGrid x:Name="dataGrid" FontSize="15" CanUserAddRows="false" Margin="10,113,10,10" ColumnWidth="*" RowHeight="45" ItemsSource="{Binding Items, Mode=OneWay}" AutoGenerateColumns="False" IsReadOnly="True" AlternatingRowBackground="#FF6E6E6E" Foreground="Black" BorderBrush="#FF595A5B" Background="Transparent" HorizontalGridLinesBrush="Black" VerticalGridLinesBrush="Black" CanUserReorderColumns="False" RowHeaderWidth="0" >
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="#FF575757" />
<Setter Property="Foreground" Value="White" />
<Style.Triggers>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#FF616161" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="14"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DodgerBlue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Order Number" Binding="{Binding compOrderNumber}"></DataGridTextColumn>
<DataGridTextColumn Header="Start Date" Binding="{Binding tstart, StringFormat=\{0:yyyy-MM-dd \}}" ></DataGridTextColumn>
<DataGridTextColumn x:Name="dataGridTextColumn" Header="Tool" Binding="{Binding tool}"></DataGridTextColumn>
<DataGridTextColumn Header="Parent Tool" Binding="{Binding parentTool}" Visibility="Hidden"></DataGridTextColumn>
<DataGridTextColumn Header="Component" Binding="{Binding component}"></DataGridTextColumn>
<DataGridTextColumn Header="Color No." Binding="{Binding colorno}" Visibility="Hidden"></DataGridTextColumn>
<DataGridTextColumn Header="Material No." Binding="{Binding matno}" Visibility="Hidden"></DataGridTextColumn>
<DataGridTextColumn Header="Quantity" Binding="{Binding qty}" Visibility="Hidden"></DataGridTextColumn>
<DataGridTextColumn Header="Seq." Visibility="Hidden" Binding="{Binding seq}"></DataGridTextColumn>
<DataGridTextColumn Header="Press" Binding="{Binding press}" Visibility="Hidden"></DataGridTextColumn>
<DataGridTextColumn Header="LDR" Binding="{Binding LDR}" Visibility="Hidden"></DataGridTextColumn>
<DataGridTextColumn Header="Remaining" Visibility="hidden" Binding="{Binding remaining, StringFormat={}{0:n0}}"></DataGridTextColumn>
<DataGridTextColumn Header="Weight" Binding="{Binding TotalWeight, StringFormat={}{0:n0}}" Visibility="Hidden"></DataGridTextColumn>
<DataGridTemplateColumn Header="" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="myButton4" Background="Red" HorizontalContentAlignment="Center" Content="-" Foreground="White" Click="myButton4_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="myButton4" Background="Green" HorizontalContentAlignment="Center" Content="+" Foreground="White" Click="myButton4_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Standard Pack" Binding="{Binding stdPack}"></DataGridTextColumn>
<DataGridTextColumn Header="Standard Packs Needed" IsReadOnly="False" Binding="{Binding needed}"></DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Padding" Value="-5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
</DataGrid>
我的代码背后:
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim CmdString As String = String.Empty
Using con As New SqlConnection("Data Source=10.0.0.54;Initial Catalog=Crib3LV;User ID=sa;Password=Jacob123!;Connect Timeout=200; pooling=true; Max Pool Size=200;")
CmdString = " SELECT *, '1000' as [stdPack], '0' as needed FROM tblCurrentComponent WHERE press = 'D-PLR LH' AND printed = '1' "
'CmdString = "SELECT DISTINCT orderNumber, tstart, tool, colorno, matno, qty FROM tblSchedule WHERE press = '" & dataClass.press & "' ORDER BY tstart;"
Dim cmd As New SqlCommand(CmdString, con)
Dim sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable("Orders")
sda.Fill(dt)
dataGrid.ItemsSource = dt.DefaultView
End Using
dataGrid.SelectedItem = Nothing
dataGrid.Columns(9).Visibility = Windows.Visibility.Hidden
dataGrid.UnselectAll()
End Sub
Private Sub myButton4_Click(sender As Object, e As RoutedEventArgs)
'MsgBox("i fired")
For Each rowView As DataRowView In dataGrid.SelectedItems
If rowView IsNot Nothing Then
Dim row As DataRow = rowView.Row
gridind = dataGrid.SelectedIndex
MsgBox(gridind & " " & row.ItemArray(4).ToString)
dataGrid.Columns(4).IsReadOnly = False
Dim row2 As DataRowView = DirectCast(Content.DataContext, DataRowView)
End If
Next
End Sub
正如您在按钮中看到的那样,我知道如何获取某些值,但我不知道如何设置它们。我计划做的只是循环遍历所有行并将它们插入到SQL表中。那个时候我可能会回到这里。无论如何,感谢您提供给我的任何帮助,并提前感谢您的耐心等待。
答案 0 :(得分:0)
要做的第一件事就是让您的点击处理程序可以访问DataTable
,方法是将其设为私有字段:
Private myDataTable As DataTable
然后,您可以将Dim dt As New DataTable("Orders")
替换为myDataTable = New DataTable("Orders")
你给了两个按钮相同的名字,所以我建议你修复它并将你的点击处理程序重命名为更有意义的东西,然后做这样的事情:
Private Sub AddButton_Click(sender As Object, e As RoutedEventArgs)
If dataGrid.SelectedIndex >= 0 Then
Dim currentCount As Integer = CInt(myDataTable.Rows(dataGrid.SelectedIndex)("needed"))
myDataTable.Rows(dataGrid.SelectedIndex)("needed") = currentCount + 1
End If
End Sub
然后相似的减法:
Private Sub SubtractButton_Click(sender As Object, e As RoutedEventArgs)
If dataGrid.SelectedIndex >= 0 Then
Dim currentCount As Integer = CInt(myDataTable.Rows(dataGrid.SelectedIndex)("needed"))
If currentCount > 0 Then
myDataTable.Rows(dataGrid.SelectedIndex)("needed") = currentCount - 1
End If
End If
End Sub
当您的DataGrid
绑定到DataTable
时,您对基础表所做的任何更改都会更新网格。