我一直在寻找一整天的感觉,但似乎无法解决这个问题所以我想我会问自己。
我有一个像这样构建的DataTable:
Dim dtAttendanceTimes As New DataTable
Dim dcEmployee As New DataColumn With {
.ColumnName = "Employee",
.DataType = GetType(String)}
dtAttendanceTimes.Columns.Add(dcEmployee)
Dim dcDate As New DataColumn With {
.ColumnName = "Date",
.DataType = GetType(String)}
dtAttendanceTimes.Columns.Add(dcDate)
Dim dcClockIn As New DataColumn With {
.ColumnName = "Clock In",
.DataType = GetType(String)}
dtAttendanceTimes.Columns.Add(dcClockIn)
Dim dcClockType As New DataColumn With {
.ColumnName = "Type",
.DataType = GetType(String)}
dtAttendanceTimes.Columns.Add(dcClockType)
Dim dcApproved As New DataColumn With {
.ColumnName = "Approved",
.DataType = GetType(String)}
dtAttendanceTimes.Columns.Add(dcApproved)
For Each drAttendaceTime As DataRow In AttendancetimesAdaptor.GetDataByEmployeeID(intEmployeeID).Rows
dtAttendanceTimes.Rows.Add(drAttendaceTime("fkEmployee"), drAttendaceTime("strDate"), drAttendaceTime("strClockIn"), drAttendaceTime("strType"), drAttendaceTime("strApproved"))
Next
dgAttendanceTimes.ItemsSource = dtAttendanceTimes.DefaultView
在XAML中我构建了我的DataGrid,如下所示:
<DataGrid x:Name="dgAttendanceTimes" AutoGenerateColumns="False" Style="{StaticResource MyDataGridStyle}" Grid.Column="1" Grid.Row="1" Grid.RowSpan="3">
<DataGrid.Columns>
<DataGridTextColumn Header="Employee" x:Name="Employee" Width="*" Binding="{Binding Path=Employee}"/>
<DataGridTextColumn Header="Date" x:Name="Date" Width="*" Binding="{Binding Path=Date}"/>
<DataGridTextColumn Header="Clock In" x:Name="ClockIn" Binding="{Binding Path=ClockIn}"/>
<DataGridTextColumn Header="Type" x:Name="Type" Visibility="Hidden" Binding="{Binding Path=Type}"/>
<DataGridTextColumn Header="Approved" x:Name="Approved" Binding="{Binding Path=Approved}"/>
</DataGrid.Columns>
</DataGrid>
然而,当我运行它时,我只得到空行,我得到的行数恰好是空白而没有列?
感谢任何帮助。
答案 0 :(得分:0)
您可以在DataTable中添加列,如下所示:
With dt.Columns
.Add("SrNo", GetType(Integer))
.Add("Name", GetType(String))
End With
并在XAML中添加
ItemsSource="{Binding}"
现在在将行添加到数据表之前添加以下行:
dg.ItemSource= dt.DefaultView()
现在向datatable添加行,它将自动反映到您的数据网格。