GridView 於rowdatabound事件中,指定特定[欄]的格式

摘要:GridView 於rowdatabound事件中,指定特定[欄]的格式

C# 

指定第二欄 小於等零 背景呈現紅色 ,大於零 背景呈現綠色


if (e.Row.RowType == DataControlRowType.DataRow)
{
    if (Convert.ToInt16(e.Row.Cells[1].Text) <= 0)
    {
        e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
    }
    else if (Convert.ToInt16(e.Row.Cells[1].Text) > 0)
    {
        e.Row.Cells[1].BackColor = System.Drawing.Color.Green;
    }
}

VB

指定第8欄 小於零 呈現紅色 ,大於等於零 呈現藍色

 


 


        If e.Row.RowType = DataControlRowType.DataRow Then

            If Convert.ToInt32(e.Row.Cells(7).Text < 0) Then

                e.Row.Cells(7).ForeColor = Drawing.Color.Red

            Else

                e.Row.Cells(7).ForeColor = Drawing.Color.Blue

            End If

        End If
 

在指定ForeColor時,目前在IE8(含)以上 時,該格格線也會變色

參考文件:

1.Format a cell during rowdatabound in a gridview  http://forums.asp.net/p/1589807/4025155.aspx

2 .GridView.RowDataBound 事件

   .http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx