提问人:TWMonk 提问时间:6/13/2023 最后编辑:Étienne LanevilleTWMonk 更新时间:6/13/2023 访问量:39
清除 WPF 中的 DataGrid 数据
Clear DataGrid data in WPF
问:
因为我想要有复选框列,所以我用这种方式创建复选框 在我的 XAML 中:
<DataGrid x:Name="boxNoDetail" IsReadOnly="true" ItemsSource="{Binding}" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" FontSize="12" >
<DataGrid.Columns >
<DataGridCheckBoxColumn Header="Checked" Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" >
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}" >
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="IsHitTestVisible" Value="False"/>
<!--'read only-->
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
我用这种方式来绑定gridview:
Function dtFillDatagrid(ByVal dt As DataTable, ByVal dgv As DataGrid) As Boolean
dgv.ItemsSource = nothing
Dim myUI As userInterface = New userInterface
Try
For Each column As DataColumn In dt.Columns
Dim name As String = column.ColumnName
If name = "IsSelected" Then
ElseIf name = "sn" Then
dgv.Columns.Add(myUI.DataGridText(name, name, True))
Else
dgv.Columns.Add(myUI.DataGridText(name, name))
End If
Next
dgv.ItemsSource = dt.DefaultView
dgv.AutoGenerateColumns = False
dgv.CanUserAddRows = False
Return True
Catch ex As Exception
logUtil.e(ex)
Return False
End Try
End Function
Public Class userInterface
Function DataGridText(ByVal bind_name As String, ByVal headerName As String,
Optional notVisible As Boolean = False,
Optional rightAlignment As Boolean = False) As DataGridTextColumn
Dim col_data As DataGridTextColumn = New DataGridTextColumn
Dim c As New Style
col_data.Binding = New Binding(bind_name)
col_data.Header = headerName
If notVisible Then col_data.Visibility = Visibility.Hidden
If rightAlignment Then
c.Setters.Add(New Setter(TextBox.TextAlignmentProperty, TextAlignment.Right))
col_data.CellStyle = c
End If
Return col_data
End Function
end class
如果我刷新数据(通过再次运行),数据网格的列将显示 2 次。为什么???dtFillDatagrid()
我试着一步一步地检测,发现dgv。Itemssource = Nothing 只是清晰的网格视图数据,并且没有清晰的字段。
我想问一下,如何重复使用DTFILLDATAGRID()并解决rduplicate列的问题,或者DataTable Fill到gridView更方便?
答:
0赞
Étienne Laneville
6/13/2023
#1
由于您指定了 ,因此将手动指定列,您正在执行以下操作:dgv.AutoGenerateColumns = False
- XAML:DataGrid.Columns。在这里,您将创建第一列,即 DataGridCheckBoxColumn。
- 其中,使用函数添加新列。
dtFillDatagrid
DataGridText
您只需定义一次列。在当前代码中,每次执行时都会添加 DataGridTextColumns:dtFillDatagrid
dgv.Columns.Add(myUI.DataGridText(name, name))
您应该调整代码,以便在 Window 初始化时在单独的子例程中创建一次列。
如果 DataTable () 中的列在每次运行时都可能更改,则可以删除除 (通过检查列的 Header 值)之外的所有列,然后像现在一样添加新列。只需将此代码添加到 ,然后再添加列:dt.Columns
dtFillDatagrid
DataGridCheckBoxColumn
dtFillDatagrid
Dim columnsToRemove As New List(Of DataGridColumn)
For Each column As DataGridColumn In boxNoDetail.Columns
If column.Header <> "Checked" Then
columnsToRemove.Add(column)
End If
Next
For Each column As DataGridColumn In columnsToRemove
boxNoDetail.Columns.Remove(column)
Next
评论