【问题标题】:How To Find GridView Cell And Change It's Color如何找到 GridView 单元格并更改其颜色
【发布时间】:2016-09-12 09:09:14
【问题描述】:

我想找到带有高度日期的 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)
{
}

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

【问题讨论】:

    标签: c# css gridview


    【解决方案1】:
    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多