我正在尝试在1列上使用行跨度(如果下一行的值相同) 并根据上述情况交替行颜色。
我设法以我想要的方式运行行间距,但不能交替行。 BatchNo:3694217行应该是蓝色的。
这是当前的输出:
的Asp
<asp:GridView ID="visualisation" runat="server"
AutoGenerateColumns="false" OnDataBound="OnDataBound" OnItemDataBound="Item_Bound" CellPadding="15" CellSpacing="15" HeaderStyle-BackColor="DarkOliveGreen" GridLines="Both">
<Columns>
<asp:BoundField DataField="BatchNo" HeaderText="BatchNo" HeaderStyle-Width="15%" />
<asp:BoundField DataField="Type" HeaderText="Type" HeaderStyle-Width="15%" />
<asp:ImageField DataImageUrlField="DataLoaded" HeaderText="DataLoaded" HeaderStyle-Width="15%" />
<asp:ImageField DataImageUrlField="Errors" HeaderText="Errors" HeaderStyle-Width="15%" />
<asp:ImageField DataImageUrlField="ProcessingRun" HeaderText="ProcessingRun" HeaderStyle-Width="15%" />
</Columns>
</asp:GridView>
C#
protected void Page_Load(object sender, EventArgs e)
{
dataT();
visualisation.DataBind();
}
public void dataT()
{
DataTable dtVisu = new DataTable();
dtVisu.Columns.Add(new DataColumn("BatchNo", typeof(System.String)));
dtVisu.Columns.Add(new DataColumn("Type", typeof(System.String)));
dtVisu.Columns.Add(new DataColumn("DataLoaded", typeof(System.String)));
dtVisu.Columns.Add(new DataColumn("Errors", typeof(System.String)));
dtVisu.Columns.Add(new DataColumn("ProcessingRun", typeof(System.String)));
//dtVisu.Columns.Add(new DataColumn("alignRow", typeof(System.String)));
DataRow dr = dtVisu.NewRow();
dr["BatchNo"] = "3704500";
dr["Type"] = "Calibration";
dr["DataLoaded"] = "images/g4-12.png";
dr["Errors"] = "images/g4-12.png";
dr["ProcessingRun"] = "images/g4-12.png";
//dr["alignRow"] = "1";
dtVisu.Rows.Add(dr);
dr = dtVisu.NewRow();
dr["BatchNo"] = "3704542";
dr["Type"] = "Range Settings";
dr["DataLoaded"] = "images/r4-12.png";
dr["Errors"] = "images/r4-12.png";
dr["ProcessingRun"] = "images/g4-12.png";
dtVisu.Rows.Add(dr);
dr = dtVisu.NewRow();
dr["BatchNo"] = "3704542";
dr["Type"] = "Range Settings";
dr["DataLoaded"] = "images/r4-12.png";
dr["Errors"] = "images/r4-12.png";
dr["ProcessingRun"] = "images/g4-12.png";
dtVisu.Rows.Add(dr);
dr = dtVisu.NewRow();
dr["BatchNo"] = "3687345";
dr["Type"] = "Calibration";
dr["DataLoaded"] = "images/g4-12.png";
dr["Errors"] = "images/g4-12.png";
dr["ProcessingRun"] = "images/g4-12.png";
dtVisu.Rows.Add(dr);
dr = dtVisu.NewRow();
dr["BatchNo"] = "3694217";
dr["Type"] = "Calibration";
dr["DataLoaded"] = "images/g4-12.png";
dr["Errors"] = "images/g4-12.png";
dr["ProcessingRun"] = "images/g4-12.png";
dtVisu.Rows.Add(dr);
visualisation.DataSource = dtVisu;
}
protected void OnDataBound(object sender, EventArgs e)
{
int RowSpan = 2;
for (int i = visualisation.Rows.Count - 2; i >= 0; i--)
{
GridViewRow currRow = visualisation.Rows[i];
GridViewRow prevRow = visualisation.Rows[i + 1];
if (currRow.Cells[0].Text == prevRow.Cells[0].Text)
{
currRow.Cells[0].RowSpan = RowSpan;
prevRow.Cells[0].Visible = false;
RowSpan += 1;
currRow.BackColor = Color.FromName("#7AA5D6");
prevRow.BackColor = Color.FromName("#7AA5D6");
}
else
{
RowSpan = 2;
}
}
}
答案 0 :(得分:3)
您需要为行保留一个单独的计数器以指定交替的颜色。请注意,在当前代码中,颜色仅在跨越行时才会交替,因此单行不会被突出显示。围绕这个应该做的事情:
protected void OnDataBound(object sender, EventArgs e)
{
int RowSpan = 2;
// actual row counter, spanned rows count as one
int rowCount = 0;
for (int i = visualisation.Rows.Count - 2; i >= 0; i--)
{
GridViewRow currRow = visualisation.Rows[i];
GridViewRow prevRow = visualisation.Rows[i + 1];
if (currRow.Cells[0].Text == prevRow.Cells[0].Text)
{
currRow.Cells[0].RowSpan = RowSpan;
prevRow.Cells[0].Visible = false;
RowSpan += 1;
}
else
{
RowSpan = 2;
//it was a new row
rowCount++;
}
if (rowCount % 2 == 0)
{
currRow.BackColor = Color.FromName("#7AA5D6");
}
}
}