-3

尝试将 datagridview 中的周数显示为列。要选择两个日期范围,单击按钮,然后将数据网格中的周数显示为列。

我正在尝试按 ID 和周数创建时间线图

这是我到目前为止所做的事情,我想从日期中选择并获得几周。

var daysCount = DateTime.DaysInMonth(DateTime.Now.Year, 1);

for (int i = 1; i <= daysCount; i++)
{
    grid1.Columns.Add(new DataGridViewColumn() 
    { 
        HeaderText = i.ToString(), 
        CellTemplate = new DataGridViewTextBoxCell() 
    });
}
4

1 回答 1

0

检查: https ://stackoverflow.com/a/3738781/1043068

var start = new DateTime(2013, 9, 23);
var end = new DateTime(2013, 10, 5);

double daysBefore = (start - new DateTime(start.Year, 1, 1)).TotalDays;

int
    weekNumber = (int)Math.Ceiling(daysBefore / 7), // Start Day week number
    currentYear = start.Year; // used to check for a new year

for (var dt = start; dt <= end; dt = dt.AddDays(7))
{
    if (dt.Year > currentYear)
    {
        currentYear = dt.Year;
        weekNumber = 1;
    }

    dataGridView1.Columns.Add(new DataGridViewColumn
    {
        HeaderText = (weekNumber++).ToString(),
        CellTemplate = new DataGridViewTextBoxCell()
    });
}
于 2013-10-03T13:35:23.813 回答