Hi all. I'm going crazy here, i've been going through every example i could get my hands on to background color a row or cell in my datagrid. i have inherited this code from a collegaue, and i have been able to update the page no problem, but then i wanted to do something which i thought was simple; color the background of the row, depending on the cells value. i know there are a good few samples out there, but i have tried to integrate each one into mine, and none work, i am totally spent trying to make it work.
i have a database with a certain column containing the status value. it can either be "OPEN", "CLOSED","SENT" etc. If the cell contains "OPEN", i want the row to be background colored red. simple as that. the code already exists to display the grid, it works great, but integrating this new code is killing me. i am using the 'code behind' approach. here are my files, recomm1.aspx, and recomm1.vb
I'm sorry i'm putting in all of the file text, but it is probably best in order to illustrate everything involved in this datagrid. I welcome any help. Thanks for your time.
recomm1.aspx
-----------------------
<%@ Page Language="VB" Debug="true" codebehind="recomm1.vb" Src="recomm1.vb" Inherits="SortingTemplateColumns" %>
<html>
<form runat="server">
<aspataGrid id="DataGrid1" runat="server" Width="100%" ForeColor="Black" GridLines="Vertical" CellPadding="3" BackColor="White" _
BorderColor="#999999" BorderWidth="1px" BorderStyle="Solid" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" _
OnItemCommand="DataGrid1_CommandItem" OnItemDataBound="DataGrid1_ItemDataBound">
<FooterStyle backcolor="#CCCCCC"></FooterStyle>
<HeaderStyle font-names="Arial" font-bold="True" horizontalalign="Center" forecolor="White" backcolor="Black"></HeaderStyle>
<PagerStyle horizontalalign="Center" forecolor="Black" backcolor="#999999" mode="NumericPages"></PagerStyle>
<SelectedItemStyle font-bold="True" forecolor="White" backcolor="#000099"></SelectedItemStyle>
<Columns>
<asp:TemplateColumn>
<HeaderStyle width="10%"></HeaderStyle>
<HeaderTemplate>
<asp:LinkButton id="LinkButton2" runat="server" forecolor="white" CommandName="sort" CommandArgument="Date">Date</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="lblDate" text= '<%# DataBinder.Eval(Container.DataItem,"Date","{0:dd/MMM/yyyy HH:mm}") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle width="5%"></HeaderStyle>
<HeaderTemplate>
<asp:LinkButton id="LinkButton5" runat="server" forecolor="white" CommandName="sort" CommandArgument="Status">Status</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="lblStatus" text='<%# DataBinder.Eval(Container.DataItem,"Status") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
</p>
<p>
<asp:Label id="Label1" runat="server" visible="False"> DESC</asp:Label>
</p>
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
recomm1.vb
-----------------------
Imports System.Data
Imports System.Data.OleDB
Public Class SortingTemplateColumns
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents DropDownList1 AS System.Web.UI.WebControls.DropDownList
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Select Case CType(e.Item.Cells(7).Text, String)
Case Is ="OPEN"
e.Item.BackColor = Drawing.Color.Red
Case Is ="CLOSED"
e.Item.BackColor = Drawing.Color.Orange
End Select
End If
End Sub
Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
' Databind the data grid on the first request only
' (on postback, rebind only in paging and sorting commands)
BindGrid()
End If
End Sub
Sub DataGrid1_Page(ByVal Sender As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub
Sub DataGrid1_Sort(ByVal Sender As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
DataGrid1.CurrentPageIndex = 0
SortField = e.SortExpression
if label1.text=" DESC" then
label1.text=" ASC"
else
label1.text= " DESC"
end if
BindGrid()
End Sub
'---------------------------------------------------------
'
' Helpers
'
' use a property to keep track of the sort field, and
' save it in viewstate between postbacks
Property SortField() As String
Get
Dim o As Object = ViewState("SortField")
If o Is Nothing Then
Return String.Empty
End If
Return CStr(o)
End Get
Set(ByVal Value As String)
ViewState("SortField") = Value
End Set
End Property
Sub BindGrid()
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;"
ConnectionString += "Data Source=C:\Cryosat\database\recommendations.mdb"
Dim CommandText As String
CommandText = "SELECT * FROM recommendations order by recommendations.id DESC;"
Else
CommandText = "SELECT * FROM recommendations order by " & SortField & label1.text
End If
Dim myConnection As New OLEDBConnection(ConnectionString)
Dim myCommand As New OLEdbDataAdapter(CommandText, myConnection)
Dim objDataSet as new DataSet()
myCommand.Fill(objDataSet, "recommendations")
DataGrid1.DataSource=objDataSet.Tables("recommendations").defaultview
DataGrid1.DataBind()
DataGrid1.Columns(1).Visible=False
End Sub
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e as System.EventArgs) Handles DropDownList1.SelectedIndexChanged
End sub
Public Sub DataGrid1_CommandItem (ByVal source as System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
dim lblpage as New System.Web.UI.WebControls.Label
lblpage=e.Item.FindControl("lblpassID")
if e.CommandName = "View" then
Response.Redirect ("viewrecomm.aspx?id=" & lblpage.Text)
end if
if e.CommandName = "Edit" then
Response.Redirect ("Editrecomm.aspx?id=" & lblpage.Text)
end if
End sub
End Class
i have a database with a certain column containing the status value. it can either be "OPEN", "CLOSED","SENT" etc. If the cell contains "OPEN", i want the row to be background colored red. simple as that. the code already exists to display the grid, it works great, but integrating this new code is killing me. i am using the 'code behind' approach. here are my files, recomm1.aspx, and recomm1.vb
I'm sorry i'm putting in all of the file text, but it is probably best in order to illustrate everything involved in this datagrid. I welcome any help. Thanks for your time.
recomm1.aspx
-----------------------
<%@ Page Language="VB" Debug="true" codebehind="recomm1.vb" Src="recomm1.vb" Inherits="SortingTemplateColumns" %>
<html>
<form runat="server">
<aspataGrid id="DataGrid1" runat="server" Width="100%" ForeColor="Black" GridLines="Vertical" CellPadding="3" BackColor="White" _
BorderColor="#999999" BorderWidth="1px" BorderStyle="Solid" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" _
OnItemCommand="DataGrid1_CommandItem" OnItemDataBound="DataGrid1_ItemDataBound">
<FooterStyle backcolor="#CCCCCC"></FooterStyle>
<HeaderStyle font-names="Arial" font-bold="True" horizontalalign="Center" forecolor="White" backcolor="Black"></HeaderStyle>
<PagerStyle horizontalalign="Center" forecolor="Black" backcolor="#999999" mode="NumericPages"></PagerStyle>
<SelectedItemStyle font-bold="True" forecolor="White" backcolor="#000099"></SelectedItemStyle>
<Columns>
<asp:TemplateColumn>
<HeaderStyle width="10%"></HeaderStyle>
<HeaderTemplate>
<asp:LinkButton id="LinkButton2" runat="server" forecolor="white" CommandName="sort" CommandArgument="Date">Date</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="lblDate" text= '<%# DataBinder.Eval(Container.DataItem,"Date","{0:dd/MMM/yyyy HH:mm}") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle width="5%"></HeaderStyle>
<HeaderTemplate>
<asp:LinkButton id="LinkButton5" runat="server" forecolor="white" CommandName="sort" CommandArgument="Status">Status</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="lblStatus" text='<%# DataBinder.Eval(Container.DataItem,"Status") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
</p>
<p>
<asp:Label id="Label1" runat="server" visible="False"> DESC</asp:Label>
</p>
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
recomm1.vb
-----------------------
Imports System.Data
Imports System.Data.OleDB
Public Class SortingTemplateColumns
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents DropDownList1 AS System.Web.UI.WebControls.DropDownList
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Select Case CType(e.Item.Cells(7).Text, String)
Case Is ="OPEN"
e.Item.BackColor = Drawing.Color.Red
Case Is ="CLOSED"
e.Item.BackColor = Drawing.Color.Orange
End Select
End If
End Sub
Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
' Databind the data grid on the first request only
' (on postback, rebind only in paging and sorting commands)
BindGrid()
End If
End Sub
Sub DataGrid1_Page(ByVal Sender As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub
Sub DataGrid1_Sort(ByVal Sender As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
DataGrid1.CurrentPageIndex = 0
SortField = e.SortExpression
if label1.text=" DESC" then
label1.text=" ASC"
else
label1.text= " DESC"
end if
BindGrid()
End Sub
'---------------------------------------------------------
'
' Helpers
'
' use a property to keep track of the sort field, and
' save it in viewstate between postbacks
Property SortField() As String
Get
Dim o As Object = ViewState("SortField")
If o Is Nothing Then
Return String.Empty
End If
Return CStr(o)
End Get
Set(ByVal Value As String)
ViewState("SortField") = Value
End Set
End Property
Sub BindGrid()
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;"
ConnectionString += "Data Source=C:\Cryosat\database\recommendations.mdb"
Dim CommandText As String
CommandText = "SELECT * FROM recommendations order by recommendations.id DESC;"
Else
CommandText = "SELECT * FROM recommendations order by " & SortField & label1.text
End If
Dim myConnection As New OLEDBConnection(ConnectionString)
Dim myCommand As New OLEdbDataAdapter(CommandText, myConnection)
Dim objDataSet as new DataSet()
myCommand.Fill(objDataSet, "recommendations")
DataGrid1.DataSource=objDataSet.Tables("recommendations").defaultview
DataGrid1.DataBind()
DataGrid1.Columns(1).Visible=False
End Sub
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e as System.EventArgs) Handles DropDownList1.SelectedIndexChanged
End sub
Public Sub DataGrid1_CommandItem (ByVal source as System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
dim lblpage as New System.Web.UI.WebControls.Label
lblpage=e.Item.FindControl("lblpassID")
if e.CommandName = "View" then
Response.Redirect ("viewrecomm.aspx?id=" & lblpage.Text)
end if
if e.CommandName = "Edit" then
Response.Redirect ("Editrecomm.aspx?id=" & lblpage.Text)
end if
End sub
End Class