繁体   English   中英

如何根据gridview行数据绑定中的当前HeaderName日期将“缺席”文本保留在“空”单元格的位置

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

如果单元格为空,则尝试单元格值应为“不存在”,如果单元格基于标题文本日期小于等于今天该单元格应为空的日期,低于 12,13 日期与以下条件匹配,但如何根据标题文本日期不存在单元格字段,标题条件与下面的 if 条件匹配,但不知道如何保持缺席。

 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 ,这可能不是最好的办法,但我怀疑,因为没有办法直接获取CellIndex基于Cell的HeaderRow价值,它更容易使列表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";
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM