Because I want have checkbox column, I use this way create checkbox in my 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>
I use this way to bind 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
If I refresh the data (by running dtFillDatagrid()
again), the datagrid's columns are displayed 2 times. Why ???
I tried to detect step by step, and found that dgv.Itemssource = Nothing is just clear gridview data, and there is no clear field.
I want to ask, how can I use DTFILLDATAGRID() repeatedly and solve the problem of rduplicate columns, or is it more convenient for DataTable Fill to gridView?
Since you specify dgv.AutoGenerateColumns = False
, the columns will be specified manually which you are doing in:
dtFillDatagrid
where add new columns using your DataGridText
function.You only need to define the columns once. In your current code, you are adding DataGridTextColumns every time dtFillDatagrid
executes:
dgv.Columns.Add(myUI.DataGridText(name, name))
You should adjust the code to create the columns once in a separate subroutine when your Window initializes.
If the columns in your DataTable (dt.Columns
) potentially change every time you run dtFillDatagrid
, then you can remove all the columns except the DataGridCheckBoxColumn
(by checking the column's Header value) and then add the new columns like you are doing now. Just add this code to dtFillDatagrid
, before adding columns again:
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