动态创建多层标头DataGrid [英] dynamically create multilayer header DataGrid
问题描述
大家好,
我想问你,有人能告诉我,我怎样才能动态创建多层头DataGrid?
我的数据正在发生变化,因此我无法在xaml代码中设置这些标头。
全部取决于DatePicker的选择 - 我有两个DatePickers,代码是:
DataGrid1.DataContext = Nothing
DataGrid1.Columns.Clear()
' Controls.Add(DataGrid1)
DataGrid1.Visibility = Windows.Visibility.Visible
' DataGrid1.Update()
Dim startDate As DateTime = DateTimePicker1.SelectedDate
Dim endDate As DateTime = DateTimePicker2.SelectedDate
Dim numberOfDays As 整数 =(endDate。日期 - startDate。日期)。天+ 1
对于 dayCount As 整数 = 0 (numberOfDays - 1 )
Dim currentDate As 日期 = Convert.ToDateTime(startDate.AddDays(dayCount))
Dim formatedD 作为 字符串 = currentDate.ToString( dd)
如果 currentDate.DayOfWeek<> DayOfWeek.Saturday AndAlso currentDate.DayOfWeek<> DayOfWeek.Sunday 然后
Dim c1 As 新 DataGridTextColumn
c1.Header = formatedD
DataGrid1.Columns.Add(c1)
结束 如果
下一步 dayCount
工作正常。应用程序正在创建日期列。但是,如何创建额外的标题,以指示与本月相关的日期之上的月份名称。 (天列按顺序从1到... n)
我想要实现的目标(或多或少像这样的视图)
http://www.newfazeindustries.com/picofboard.JPG [ ^ ] [ ]
非常感谢您的任何解决方案。
BR
私人 Sub Button1_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs) Handles Button1.Click
DataGrid1.DataSource = Nothin g
DataGrid1.Columns.Clear()
' Controls.Add(DataGrid1 )
' DataGrid1.Visible = True
' DataGrid1.Update()
Dim startDate As DateTime = DateTimePicker1.Value
Dim endDate 作为 DateTime = DateTimePicker2.Value
Dim numberOfDays As 整数 =(endDate。日期 - startDate。日期)。天+ 1
对于 dayCount As 小号pan> 整数 = 0 到(numberOfDays - 1 )
Dim currentDate As 日期 = Convert.ToDateTime(startDate.AddDays(dayCount))
Dim formatedD < span class =code-keyword> As String = currentDate.ToString( < span class =code-string> dd)
如果 currentDate.DayOfWeek<> DayOfWeek.Saturday AndAlso currentDate.DayOfWeek<> DayOfWeek.Sunday 然后
Dim c1 As 新 DataGridViewColumn
c1.HeaderText =格式(currentDate, MMM)& vbCrLf& formatedD
DataGrid1.Columns.Add(c1)
结束 如果
下一步 dayCount
结束 Sub
Hi All,
I would like to aks you, can someone tell me, how can I dynamically create multilayer headers DataGrid?
My data are changing so I cannot set those headers in xaml code.
All depends on DatePicker selections - I have Two DatePickers, and the code is:
DataGrid1.DataContext = Nothing
DataGrid1.Columns.Clear()
'Controls.Add(DataGrid1)
DataGrid1.Visibility = Windows.Visibility.Visible
'DataGrid1.Update()
Dim startDate As DateTime = DateTimePicker1.SelectedDate
Dim endDate As DateTime = DateTimePicker2.SelectedDate
Dim numberOfDays As Integer = (endDate.Date - startDate.Date).Days + 1
For dayCount As Integer = 0 To (numberOfDays - 1)
Dim currentDate As Date = Convert.ToDateTime(startDate.AddDays(dayCount))
Dim formatedD As String = currentDate.ToString("dd")
If currentDate.DayOfWeek <> DayOfWeek.Saturday AndAlso currentDate.DayOfWeek <> DayOfWeek.Sunday Then
Dim c1 As New DataGridTextColumn
c1.Header = formatedD
DataGrid1.Columns.Add(c1)
End If
Next dayCount
It works fine. App is creating day column. But, how to create additional header that would indicate name of the Month above the days related to this month. (days columns are in order from 1 to ...n)
What I would like to achieve (more or less view like this one)
http://www.newfazeindustries.com/picofboard.JPG[^][]
Thank you very much for any solution.
BR
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DataGrid1.DataSource = Nothing DataGrid1.Columns.Clear() 'Controls.Add(DataGrid1) 'DataGrid1.Visible = True 'DataGrid1.Update() Dim startDate As DateTime = DateTimePicker1.Value Dim endDate As DateTime = DateTimePicker2.Value Dim numberOfDays As Integer = (endDate.Date - startDate.Date).Days + 1 For dayCount As Integer = 0 To (numberOfDays - 1) Dim currentDate As Date = Convert.ToDateTime(startDate.AddDays(dayCount)) Dim formatedD As String = currentDate.ToString("dd") If currentDate.DayOfWeek <> DayOfWeek.Saturday AndAlso currentDate.DayOfWeek <> DayOfWeek.Sunday Then Dim c1 As New DataGridViewColumn c1.HeaderText = Format(currentDate, "MMM") & vbCrLf & formatedD DataGrid1.Columns.Add(c1) End If Next dayCount End Sub
这篇关于动态创建多层标头DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!