如何找到GridView单元格并改变它的颜色

本文关键字:改变 颜色 单元格 何找 GridView | 更新日期: 2023-09-27 18:06:48

我想找到GridView单元格的高度日期和改变它的背景颜色

GridView标签=>

<div id="divgrdItemReport" runat="server" class="table-responsive col-lg-12 col-md-12 col-sm-12 col-xs-12 mt5 overflow-auto">
     <asp:GridView ID="grdItemTrackingReport" runat="server" AutoGenerateColumns="false" CssClass="table table-primary table-condensed table-responsive rowloader" OnRowDataBound="grdItemTrackingReport_OnRowDataBound"
                                    AllowPaging="false">
                                    <Columns>
                                        <asp:BoundField DataField="Date1" HeaderText="Date1" />
                                        <asp:BoundField DataField="Date2" HeaderText="Date2" />
                                        <asp:BoundField DataField="Date3" HeaderText="Date3" />
                                        <asp:BoundField DataField="Date4" HeaderText="Date4" />
                                        <asp:BoundField DataField="Date5" HeaderText="Date5" />
                                        <asp:BoundField DataField="Date6" HeaderText="Date6" />
                                    </Columns>
                                </asp:GridView>
                            </div>

我的数据绑定()

private void LoadItemTrackingGrid()
{
    var dtItemTrackingReport = PP.getItemTrackingReportDt(new Query()
    {
        IdCodeWithYear = txtBarcodeNo.Text,
        SupplierId = ddlAccountGroup.zIsSelect() ? ddlAccountGroup.zToInt() : null,
        ProductId = ddlProduct.zIsSelect() ? ddlProduct.zToInt() : null,
        eStatus = (int)eStatus.Active,
        OrganizationUniqueId = OrganizationUtilities.GetOrganizationUniqueId(),
        CompanyUniqueId = CompanyUniqueId,
    });
    grdItemTrackingReport.Columns[CU.GetColumnIndexByName(grdItemTrackingReport, CS.StockProductId)].Visible = true;
    grdItemTrackingReport.DataSource = dtItemTrackingReport;
    grdItemTrackingReport.DataBind();
    grdItemTrackingReport.Columns[CU.GetColumnIndexByName(grdItemTrackingReport, CS.StockProductId)].Visible = false;
}

当前在OnRowDataBound =>

中没有任何内容
protected void grdItemTrackingReport_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
}

现在,我必须改变我的特定单元格背景,它在行中具有最大的值。我该怎么做?

如何找到GridView单元格并改变它的颜色

protected void grdItemTrackingReport_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    DateTime? maxDate = DateTime.MinValue;
    int CellIndex = 0;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataTable DataTable = new DataTable();
        DataTable.Columns.Add("Date", typeof(DateTime));
        DataTable.Columns.Add("CellIndex", typeof(int));
        for (int i = 0; i < e.Row.Cells.Count; i++)//Store only Date and It's Cell index in Data Table
        {
            if (e.Row.Cells[i].Text.zIsDate())
                DataTable.Rows.Add(e.Row.Cells[i].Text, i);//i is CellIndex
        }
        for (int i = 0; i < DataTable.Rows.Count; i++)//Find MAX Date
        {
            DateTime? date = DataTable.Rows[i]["Date"].ToString().zToDate();
            if (date > maxDate)
            {
                maxDate = date;
                CellIndex = int.Parse(DataTable.Rows[i]["CellIndex"].ToString());//largest Date's CellIndex
            }
        }
        if (CellIndex > 0)//Give Color To Index
            e.Row.Cells[CellIndex].BackColor = Color.DarkGray;
    }
}