gpt4 book ai didi

C# GridView 垂直和水平交替背景颜色

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:02 27 4
gpt4 key购买 nike

我正在开发一个 ASP.NET Web 应用程序,我想像国际象棋一样交替使用 gridview 单元格的颜色(垂直和水平)。颜色将是黄色和黑色。

最佳答案

您可能想使用 GridView 的 RowDataBound 事件,如下所示:

   <asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False" 
DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;

for (int i = 0; i < e.Row.Cells.Count; i++)
{
TableCell Cell = e.Row.Cells[i];

// if both row and column are odd, color then black
// if both row and column are even, color then yellow
if (((e.Row.RowIndex % 2 == 1) && (i % 2 == 1)) ||
((e.Row.RowIndex % 2 == 0) && (i % 2 == 0)))
Cell.BackColor = Color.Black;
else
Cell.BackColor = Color.Yellow;
}
}

关于C# GridView 垂直和水平交替背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15030823/

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