Dynamic DataGrid Column

Can someone tell me how to accomplish this. Basically I have code here that
returns a datagrid that is queried from a database. One column however
contains data as to the state of a computer (error, critical, successful,
etc) and I would like to replace this column with a picture (i.e. a red X for
critical or a check for successful). So, the data in the column would
determine the picture used. For instance if the value = 10 then
successful.gif is shown. How can I accomplish this? My code is below:

<% [at] Page Language="VB" %>
<% [at] Import Namespace = "System.Data" %>
<% [at] Import Namespace = "System.Data.SQLClient" %>
<script language="VB" runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
Dim strSortField As String ' Field to sort by
Dim strSortOrder As String ' "ASC" or "DESC"

' Retrieve sorting parameters:
' Get the field name and make sure the input is one of our field names.
strSortField = Request.QueryString("field")
Select Case LCase(strSortField)
Case "lastheartbeat", "events", "alerts", "highestopenalertstate"
strSortField = strSortField
Case Else
strSortField = "name"
End Select

' Check for descending o/w we default to ascending
Select Case LCase(Request.QueryString("order"))
Case "desc"
strSortOrder = "DESC"
Case Else
strSortOrder = "ASC"
End Select

BindDataToGrid(strSortField, strSortOrder)
End Sub

Sub BindDataToGrid(strSortField As String, strSortOrder As String)
Dim myConnection As SqlConnection
Dim myDataAdapter As SqlDataAdapter
Dim myDataSet As DataSet

Dim strSQL As String

Dim objColumn As DataGridColumn

myConnection = New SQLConnection("Data Source=SERVER;" _
& "Initial Catalog=OnePoint;User ID=user; Password=password;" _
& "Connect Timeout=15;Network Library=dbmssocn;")

strSQL = "SELECT * FROM Ops_ComputerSummaryView ORDER BY [" & strSortField
& "] " & strSortOrder & ";"

myDataAdapter = New SqlDataAdapter(strSQL, myConnection)

myDataSet = new Dataset()
myDataAdapter.Fill(myDataSet, "sample")

' Adjust hyperlink on sort field and add indicator image to header.
For Each objColumn in DBSortDataGrid.Columns
If InStr(objColumn.HeaderText, strSortField) <> 0 Then
If LCase(strSortOrder) = "asc" Then
objColumn.HeaderText = Replace(objColumn.HeaderText, "order=asc",
"order=desc") _
& " <img src=""images/arr_up2.gif"" />"
Else
objColumn.HeaderText = objColumn.HeaderText _
& " <img src=""images/arr_dn2.gif"" />"
End If
End If
Next objColumn

DBSortDataGrid.DataSource = myDataSet
DBSortDataGrid.DataBind()
End Sub

</script>

<html>
<head>
<title>ASP.NET Database Sort (with Arrows) Sample</title>
</head>
<body>

<form runat="server" EnableViewState="false">

<asp:DataGrid id="DBSortDataGrid" runat="server"
Border = 0
CellSpacing = 1
width = "100%"

HeaderStyle-BackColor = "#CCCCCC"
HeaderStyle-ForeColor = "#000000"
HeaderStyle-Font-Bold = "True"
HeaderStyle-Font-Name = "Tahoma"
HeaderStyle-Font-Size = "8.25"

ItemStyle-BackColor = "#FFFFFF"
ItemStyle-Font-Name = "Tahoma"
ItemStyle-Font-Size = "8.25"
AlternatingItemStyle-BackColor = "GhostWhite"

AutoGenerateColumns = "False"
>
<Columns>
<asp:BoundColumn DataField="name" HeaderText="<a
href='?field=name&order=asc'><font color=black>Computer</font></a>" />
<asp:BoundColumn DataField="lastheartbeat" HeaderText="<a
href='?field=lastheartbeat&order=asc'><font color=black>Last
Heartbeat</font></a>" />
<asp:BoundColumn DataField="events" HeaderText="<a
href='?field=events&order=asc'><font color=black>Events</font></a>" />
<asp:BoundColumn DataField="alerts" HeaderText="<a
href='?field=alerts&order=asc'><font color=black>Alerts</font></a>" />
<asp:BoundColumn DataField="highestopenalertstate" HeaderText="<a
href='?field=highestopenalertstate&order=asc'><font
color=black>Status</font></a>" />
</Columns>
</asp:DataGrid>

</form>

</body>
</html>

--
Justin Harter
::BLOG:: http://spaces.msn.com/members/jharter
jharter [ Di, 04 April 2006 16:30 ] [ ID #1259793 ]

Re: Dynamic DataGrid Column

Justin Harter wrote:
> Can someone tell me how to accomplish this. Basically I have code
> here that returns a datagrid that is queried from a database. One
> column however contains data as to the state of a computer (error,
> critical, successful, etc) and I would like to replace this column
> with a picture (i.e. a red X for critical or a check for successful).
> So, the data in the column would determine the picture used. For
> instance if the value = 10 then successful.gif is shown. How can I
> accomplish this? My code is below:
>
There was no way for you to know it, but this is a classic asp newsgroup.
While you may be lucky enough to find a dotnet-savvy person here who can
answer your question, you can eliminate the luck factor by posting your
question to a newsgroup where the dotnet-savvy people hang out. I suggest
microsoft.public.dotnet.framework.aspnet.

There was a good 4Guys... series written by Scott mitchell from which I
learned how to do this. Let's see if i can find it ...
Ah!
http://aspnet.4guysfromrolla.com/articles/040502-1.aspx


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
reb01501 [ Di, 04 April 2006 16:34 ] [ ID #1259794 ]
Webserver » microsoft.public.inetserver.asp.db » Dynamic DataGrid Column

Vorheriges Thema: possible MDAC issue hanging web servers?
Nächstes Thema: ASP -- INNER JOIN