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

时间:2016-09-12 09:09:15

标签: c# css gridview

我想找到具有高度日期的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>

这是我的数据bind()

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 =&gt;

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

现在,我必须更改行中具有最大值的特定单元格背景。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

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;
    }
}