如何根据 gridview rowdatabound 中的当前标题名称日期将 'Absent' 文本保留在 'Empty' 单元格的位置

How to keep 'Absent' text in the place of 'Empty' cell base on Current HeaderName Date in gridview row data bound

如果单元格为空,则尝试单元格值应为 'Absent' header文本日期小于等于今天单元格应为空的日期,低于 12,13 的日期符合以下条件但是如何根据 header 文本日期将单元格字段缺席,header 条件与以下条件匹配但没有得到如何保持缺席。

 protected void gridview_trainees_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView gridView = (GridView)sender;
            var colCount = gridview_trainees.Columns.Count;
            for (int i = 3; i < colCount; i++)
            {
                BoundField columField = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;
                DateTime CurrentColumndate = DateTime.Parse(columField.HeaderText);


                if (CurrentColumndate.Date <=DateTime.Now.Date)
                {
                    //trying cell value should be absent if cell is empty base on header
                }

            }


        }
    }

我现在的gridview如下图

我想要这样的输出,如果 header text date <= today date 那列空单元格应该是 'Absent'

我通常不使用默认的 GridView,这可能不是最好的方法,但我怀疑由于无法根据单元格的 HeaderRow 值直接检索 CellIndex,列出符合日期条件的 CellIndex 值会更容易。

然后遍历该列表以有条件地更改 Cell.Text 值与空条件匹配的单元格中的文本。

添加页面属性:

private List<int> ColIndexes = new List<int>();

这是我基于类似情况提出的解决方案:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    var row = e.Row;
    if(row.RowType == DataControlRowType.Header)
    {
        foreach(TableCell cell in row.Cells)
        {
            if(DateTime.TryParse(cell.Text, out var date))
            {
                if(date <= DateTime.Now.Date)
                    ColIndexes.Add(row.Cells.GetCellIndex(cell));
            }
        }
    }

    if(row.RowType == DataControlRowType.DataRow)
    {
        foreach (var index in ColIndexes)
        {
            var cell = row.Cells[index];
            if (string.IsNullOrWhiteSpace(cell.Text))
                cell.Text = "Absent";
        }
    }
}