dolbs
asked on
DataGrid Cell Formatting and ASP.Net
Hi
I am using a datagrid to display the contents of a table. I need to be able to change the color of a cell called GlobalRisk if a number is greater than 10. Can any one suggest if and how I can format just the cell of the datagrid and not the entire grid? Can I use an if/then statement to format a datagrid cell? Here is the relevant code.
Sub Page_Load
If Not isPostBack Then
BindDataGrid( "ARN" )
End If
End Sub
Sub BindDataGrid( strSortField As String )
Dim conPubs As OLeDBConnection
Dim cmdSelect As OLeDBCommand
Dim dadTitles As OLeDBDataAdapter
Dim dstTitles As DataSet
dim sXMLFile as string
dim sMDBFile as string
dim sConn as string
dim sSQL as string
sMDBFile = MapPath("~/Audit/Audit.mdb ")
SConn = "Provider=Microsoft.Jet.OL EDB.4.0;"
Sconn = Sconn & "Data Source=" & sMDBFile
dim cnn as OLeDbConnection
dim cmd as OLeDbCommand
dim objDA as OLeDBDataAdapter
dim objDS as Dataset
cnn = New OLeDbConnection(sConn)
cnn.Open()
cmdSelect = New OLeDBCommand( "Select * From tblEntity Order By " & strSortField, cnn )
dgrdTitles.DataSource = cmdSelect.ExecuteReader()
dgrdTitles.DataBind()
cnn.Close()
End Sub
Sub dgrdTitles_SortCommand( s As Object, e As DataGridSortCommandEventAr gs )
BindDataGrid( e.SortExpression )
End Sub
**AND THIS IS THE COLUMN AND CELL I WISH TO FORMAT IF THE VALUE IS GREATER THAN 10 ***
<asp:DataGrid
ID="dgrdTitles"
AllowSorting="True"
OnSortCommand="dgrdTitles_ SortComman d"
AutoGenerateColumns="False "
CellPadding="10"
HeaderStyle-Font-Name="Ari al"
HeaderStyle-Font-Size="11p t"
HeaderStyle-Font-Bold="Tru e"
HeaderStyle-BackColor="Lig htBlue"
ShowFooter="True"
FooterStyle-Font-Name="Ari al"
FooterStyle-Font-Size="10p t"
FooterStyle-Font-Bold="Tru e"
FooterStyle-BackColor="Lig htCyan"
BorderColor="Gray"
ItemStyle-Font-Name="Arial "
ItemStyle-Font-Size="9pt"
ItemStyle-Font-Bold="False "
ItemStyle-BackColor="Alice Blue"
Runat="Server">
***THE RELEVANT COLUMN/CELL **
<asp:BoundColumn
HeaderText="Global Risk Score"
DataField="GlobalRisk"
SortExpression="GlobalRisk "
FooterText="Global Risk Score"/>
I am using a datagrid to display the contents of a table. I need to be able to change the color of a cell called GlobalRisk if a number is greater than 10. Can any one suggest if and how I can format just the cell of the datagrid and not the entire grid? Can I use an if/then statement to format a datagrid cell? Here is the relevant code.
Sub Page_Load
If Not isPostBack Then
BindDataGrid( "ARN" )
End If
End Sub
Sub BindDataGrid( strSortField As String )
Dim conPubs As OLeDBConnection
Dim cmdSelect As OLeDBCommand
Dim dadTitles As OLeDBDataAdapter
Dim dstTitles As DataSet
dim sXMLFile as string
dim sMDBFile as string
dim sConn as string
dim sSQL as string
sMDBFile = MapPath("~/Audit/Audit.mdb
SConn = "Provider=Microsoft.Jet.OL
Sconn = Sconn & "Data Source=" & sMDBFile
dim cnn as OLeDbConnection
dim cmd as OLeDbCommand
dim objDA as OLeDBDataAdapter
dim objDS as Dataset
cnn = New OLeDbConnection(sConn)
cnn.Open()
cmdSelect = New OLeDBCommand( "Select * From tblEntity Order By " & strSortField, cnn )
dgrdTitles.DataSource = cmdSelect.ExecuteReader()
dgrdTitles.DataBind()
cnn.Close()
End Sub
Sub dgrdTitles_SortCommand( s As Object, e As DataGridSortCommandEventAr
BindDataGrid( e.SortExpression )
End Sub
**AND THIS IS THE COLUMN AND CELL I WISH TO FORMAT IF THE VALUE IS GREATER THAN 10 ***
<asp:DataGrid
ID="dgrdTitles"
AllowSorting="True"
OnSortCommand="dgrdTitles_
AutoGenerateColumns="False
CellPadding="10"
HeaderStyle-Font-Name="Ari
HeaderStyle-Font-Size="11p
HeaderStyle-Font-Bold="Tru
HeaderStyle-BackColor="Lig
ShowFooter="True"
FooterStyle-Font-Name="Ari
FooterStyle-Font-Size="10p
FooterStyle-Font-Bold="Tru
FooterStyle-BackColor="Lig
BorderColor="Gray"
ItemStyle-Font-Name="Arial
ItemStyle-Font-Size="9pt"
ItemStyle-Font-Bold="False
ItemStyle-BackColor="Alice
Runat="Server">
***THE RELEVANT COLUMN/CELL **
<asp:BoundColumn
HeaderText="Global Risk Score"
DataField="GlobalRisk"
SortExpression="GlobalRisk
FooterText="Global Risk Score"/>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
Where is the itemdatbound placed?
and the page will not compliel because of an error in this line e.Item.Cells(4).BackColor = Color.Red
It says the Color.Red is undeclared so it does not recognise the color function.
Where is the itemdatbound placed?
and the page will not compliel because of an error in this line e.Item.Cells(4).BackColor = Color.Red
It says the Color.Red is undeclared so it does not recognise the color function.
you have to import System.Drawing namespace to work with color property
ASKER
Okay. Sorry for the dumb questions. New to asp.net Color problem sorted. Figured that out myself. thanks to "toocrazy007" anyway, but the code will still not compile.
I keep getting an " invalid cast exception" See the commented section below.
I am using the OnItemDataBound = "GridDataBound" to call the sub. Id this correct?
Here is the entire code (important parts) as it now stands
Sub BindDataGrid( strSortField As String )
Dim conPubs As OLeDBConnection
Dim cmdSelect As OLeDBCommand
Dim dadTitles As OLeDBDataAdapter
Dim dstTitles As DataSet
dim sXMLFile as string
dim sMDBFile as string
dim sConn as string
dim sSQL as string
sMDBFile = MapPath("~/Audit/Audit.mdb ")
SConn = "Provider=Microsoft.Jet.OL EDB.4.0;"
Sconn = Sconn & "Data Source=" & sMDBFile
dim cnn as OLeDbConnection
dim cmd as OLeDbCommand
dim objDA as OLeDBDataAdapter
dim objDS as Dataset
cnn = New OLeDbConnection(sConn)
cnn.Open()
cmdSelect = New OLeDBCommand( "Select * From tblEntity Order By " & strSortField, cnn )
dgrdTitles.DataSource = cmdSelect.ExecuteReader()
dgrdTitles.DataBind()
cnn.Close()
End Sub
Private Sub GridDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt em Then
Dim rv As DataRowView = CType(e.Item.DataItem, DataRowView) '///// invalid cast exception
'assume that your required column is in 4th row of array
Dim nUnitsInStock As Int32 = Convert.ToInt32(rv.Row.Ite mArray(3))
If nUnitsInStock = 10 Then
e.Item.Cells(3).BackColor = Color.Red
End If
End If
End Sub
Sub dgrdTitles_SortCommand( s As Object, e As DataGridSortCommandEventAr gs )
BindDataGrid( e.SortExpression )
End Sub
'and the datagrid layout
<asp:DataGrid
ID="dgrdTitles"
AllowSorting="True"
OnSortCommand="dgrdTitles_ SortComman d"
OnItemDataBound = "GridDataBound" ' //// Is this correct?
AutoGenerateColumns="False "
CellPadding="10"
HeaderStyle-Font-Name="Ari al"
HeaderStyle-Font-Size="11p t"
HeaderStyle-Font-Bold="Tru e"
HeaderStyle-BackColor="Lig htBlue"
ShowFooter="True"
FooterStyle-Font-Name="Ari al"
FooterStyle-Font-Size="10p t"
FooterStyle-Font-Bold="Tru e"
FooterStyle-BackColor="Lig htCyan"
BorderColor="Gray"
ItemStyle-Font-Name="Arial "
ItemStyle-Font-Size="9pt"
ItemStyle-Font-Bold="False "
ItemStyle-BackColor="Alice Blue"
Runat="Server">
<Columns>
<asp:BoundColumn
HeaderText="ID"
DataField="ID"
SortExpression="ID"
FooterText="ID"/>
<asp:BoundColumn
HeaderText="ARN"
DataField="ARN"
SortExpression="ARN"
FooterText="ARN"/>
<asp:BoundColumn
HeaderText="Legal Entity"
DataField="Entity"
SortExpression="Entity"
FooterText="Legal Entity"/>
<asp:BoundColumn
HeaderText="Global Risk Score"
DataField="GlobalRisk"
SortExpression="GlobalRisk "
FooterText="Global Risk Score"/>
<asp:BoundColumn
HeaderText="Level"
DataField="GlobalColor"
SortExpression="GlobalColo r"
FooterText="Level"/>
</Columns>
</asp:DataGrid>
I keep getting an " invalid cast exception" See the commented section below.
I am using the OnItemDataBound = "GridDataBound" to call the sub. Id this correct?
Here is the entire code (important parts) as it now stands
Sub BindDataGrid( strSortField As String )
Dim conPubs As OLeDBConnection
Dim cmdSelect As OLeDBCommand
Dim dadTitles As OLeDBDataAdapter
Dim dstTitles As DataSet
dim sXMLFile as string
dim sMDBFile as string
dim sConn as string
dim sSQL as string
sMDBFile = MapPath("~/Audit/Audit.mdb
SConn = "Provider=Microsoft.Jet.OL
Sconn = Sconn & "Data Source=" & sMDBFile
dim cnn as OLeDbConnection
dim cmd as OLeDbCommand
dim objDA as OLeDBDataAdapter
dim objDS as Dataset
cnn = New OLeDbConnection(sConn)
cnn.Open()
cmdSelect = New OLeDBCommand( "Select * From tblEntity Order By " & strSortField, cnn )
dgrdTitles.DataSource = cmdSelect.ExecuteReader()
dgrdTitles.DataBind()
cnn.Close()
End Sub
Private Sub GridDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
Dim rv As DataRowView = CType(e.Item.DataItem, DataRowView) '///// invalid cast exception
'assume that your required column is in 4th row of array
Dim nUnitsInStock As Int32 = Convert.ToInt32(rv.Row.Ite
If nUnitsInStock = 10 Then
e.Item.Cells(3).BackColor = Color.Red
End If
End If
End Sub
Sub dgrdTitles_SortCommand( s As Object, e As DataGridSortCommandEventAr
BindDataGrid( e.SortExpression )
End Sub
'and the datagrid layout
<asp:DataGrid
ID="dgrdTitles"
AllowSorting="True"
OnSortCommand="dgrdTitles_
OnItemDataBound = "GridDataBound" ' //// Is this correct?
AutoGenerateColumns="False
CellPadding="10"
HeaderStyle-Font-Name="Ari
HeaderStyle-Font-Size="11p
HeaderStyle-Font-Bold="Tru
HeaderStyle-BackColor="Lig
ShowFooter="True"
FooterStyle-Font-Name="Ari
FooterStyle-Font-Size="10p
FooterStyle-Font-Bold="Tru
FooterStyle-BackColor="Lig
BorderColor="Gray"
ItemStyle-Font-Name="Arial
ItemStyle-Font-Size="9pt"
ItemStyle-Font-Bold="False
ItemStyle-BackColor="Alice
Runat="Server">
<Columns>
<asp:BoundColumn
HeaderText="ID"
DataField="ID"
SortExpression="ID"
FooterText="ID"/>
<asp:BoundColumn
HeaderText="ARN"
DataField="ARN"
SortExpression="ARN"
FooterText="ARN"/>
<asp:BoundColumn
HeaderText="Legal Entity"
DataField="Entity"
SortExpression="Entity"
FooterText="Legal Entity"/>
<asp:BoundColumn
HeaderText="Global Risk Score"
DataField="GlobalRisk"
SortExpression="GlobalRisk
FooterText="Global Risk Score"/>
<asp:BoundColumn
HeaderText="Level"
DataField="GlobalColor"
SortExpression="GlobalColo
FooterText="Level"/>
</Columns>
</asp:DataGrid>
Both the things, assigning method to onItemDataBound, and the type casting is also right, i didn't found any error in that please debug the code and see weather you are getting data correctly / not.
ASKER
When I removed the datagrid sort command and replaced it with an edit command the page complied but the cell did not change colour. If the 4th column in the database holds the value then the (rv.Row.ItemArray(3)) is 3 as shown with the first column being row zero. Is this correct.
ASKER
Thank you all for your help. Unfortunately none of the suggested answers worked properly. I have been experimenting and solved the problem by using this code:
And by changing the datagrid from OnDataBound to "OnItemDataBound"
Private Sub GridDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt em Then
Dim nUnits As Integer
nUnits = CInt(e.Item.Cells(2).Text)
If nUnits >= 12 Then
e.Item.Cells(2).BackColor = Color.Red
e.Item.Cells(2).Font.Bold = True
Else If nUnits < 12 and nUnits >= 10 Then
e.Item.Cells(2).BackColor = Color.Yellow
e.Item.Cells(2).Font.Bold = True
Else If nUnits < 10 and nUnits >= 7 Then
e.Item.Cells(2).BackColor = Color.LightGreen
Else
e.Item.BackColor = Color.White
End If
End If
End Sub
And by changing the datagrid from OnDataBound to "OnItemDataBound"
Private Sub GridDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
Dim nUnits As Integer
nUnits = CInt(e.Item.Cells(2).Text)
If nUnits >= 12 Then
e.Item.Cells(2).BackColor = Color.Red
e.Item.Cells(2).Font.Bold = True
Else If nUnits < 12 and nUnits >= 10 Then
e.Item.Cells(2).BackColor = Color.Yellow
e.Item.Cells(2).Font.Bold = True
Else If nUnits < 10 and nUnits >= 7 Then
e.Item.Cells(2).BackColor = Color.LightGreen
Else
e.Item.BackColor = Color.White
End If
End If
End Sub
http://www.codeproject.com/aspnet/ItemCreated.asp
and here
http://carlosag.net/Tools/CodeTranslator/Default.aspx
to convert the code to VB.net