清除WPF中的DataGrid数据

ewm0tg9j  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(155)

因为我想有复选框列,我用这种方式在我的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

如果我刷新数据(再次运行dtFillDatagrid()),数据网格的列将显示2次。为什么?
我试着一步一步地检测,发现dgv.Itemssource = Nothing只是清除gridview数据,没有清除字段。
我想问一下,如何重复使用DTFILLDATAGRID()并解决重复列的问题,还是DataTable Fill到gridView更方便?
第一次加载gridview:

第二次加载gridview:

wnavrhmk

wnavrhmk1#

由于您指定了dgv.AutoGenerateColumns = False,因此将手动指定列,您正在执行:

  1. XAML:DataGrid.Columns。在这里创建了第一个列,DataGridCheckBoxColumn。
    1.在dtFillDatagrid中,使用DataGridText函数添加新列。
    您只需要定义一次列。在您当前的代码中,每次执行dtFillDatagrid时都要添加DataGridTextColumns:
    dgv.Columns.Add(myUI.DataGridText(name, name))
    应调整代码,以便在初始化Window时在单独的子例程中创建一次列。
    如果DataTable(dt.Columns)中的列可能在每次运行dtFillDatagrid更改,则可以删除除DataGridCheckBoxColumn以外的所有列(通过检查列的Header值),然后像现在这样添加新列。在再次添加列之前,只需将此代码添加到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

相关问题