gpt4 book ai didi

c# - 如何找到 GridView 单元格并更改它的颜色

转载 作者:行者123 更新时间:2023-11-28 05:13:05 25 4
gpt4 key购买 nike

我想找到具有高度日期的 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 里面什么都没有 =>

protected void grdItemTrackingReport_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
}

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

最佳答案

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

关于c# - 如何找到 GridView 单元格并更改它的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39446831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com