Hi,
Here are two ways you can accomplish this.
1)
In the HTML template tag, you normally write something like this:
<%# DataBinder.Eval (Container.Data Item, "au_id") %>
or
<%# Container.DataI tem("au_id") %>
You could instead call into a function as follows:
In the item template:
<%# CheckLength(Con tainer.DataItem ("ProductName") ) %>
In the code-behind:
Protected Function CheckUnit(Units OnOrder As Object) As String
If Integer.Parse(U nitsOnOrder) = 0 Then
Return "Out Of Stock"
Else
Return UnitsOnOrder.To String()
End If
End Function
With this method, you are calling into a code-behind function that can
manipulate the output.
For more on this method, please see:
http://authors.aspalliance.com/Colt/...rFunction.html
2)
You can also use the repeater's ItemDataBound
event. This event fires for each row in the repeater (one by one).
Here's a small sample that demonstrates a little of what you can do in this
*** in the HTML
<asp:datagrid id="DataGrid1" runat="server"
ShowFooter="Tru e"></asp:datagrid>
*** in the code-behind
Dim Counter As Integer = 0
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
If Not IsPostBack Then
Bind()
End If
End Sub
Private Sub Bind()
Dim Qry1 As System.Data.Sql Client.SqlDataR eader
Dim connectionStrin g As String = "server='localh ost';
trusted_connect ion=true; Database='pubs' "
Dim sqlConnection As System.Data.Sql Client.SqlConne ction = New
System.Data.Sql Client.SqlConne ction(connectio nString)
Dim queryString As String = "SELECT au_id, au_lname, au_fname FROM
authors"
Dim sqlCommand As System.Data.Sql Client.SqlComma nd = New
System.Data.Sql Client.SqlComma nd(queryString, sqlConnection)
sqlConnection.O pen()
Qry1 =
sqlCommand.Exec uteReader(Syste m.Data.CommandB ehavior.CloseCo nnection)
DataGrid1.DataS ource = Qry1
DataGrid1.DataB ind()
Qry1.Close()
sqlCommand.Disp ose()
sqlConnection.C lose()
sqlConnection.D ispose()
End Sub
Private Sub DataGrid1_ItemD ataBound(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles
DataGrid1.ItemD ataBound
'This will count the number of items & alternating items.
'It will not include rows that are either selected or
'being edited. If this data grid included selecting or
'editing, then we would add those types to the if
'statement as well.
If e.Item.ItemType = ListItemType.It em Or e.Item.ItemType =
ListItemType.Al ternatingItem Then
If e.Item.Cells(1) .Text = "Green" Then
Dim l As WebControls.Lin kButton = New WebControls.Lin kButton
l.Text = e.Item.Cells(1) .Text
e.Item.Cells(1) .Text = ""
l.ForeColor = System.Drawing. Color.Green()
e.Item.Cells(1) .Controls.Add(l )
End If
Counter += 1
End If
'This will display the total in the footer.
If e.Item.ItemType = ListItemType.Fo oter Then
e.Item.Cells(0) .Text = Counter.ToStrin g & " People"
End If
End Sub
Thank you, Mike
Microsoft, ASP.NET Support Professional
Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.
This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "reiks" <an*******@disc ussions.microso ft.com>
Sender: "reiks" <an*******@disc ussions.microso ft.com>
Subject: datagrid-binding
Date: Thu, 18 Dec 2003 04:24:42 -0800
Lines: 10
Message-ID: <06************ *************** *@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
thread-index: AcPFYeoCQOaNqlI ERHW8vf7FY0pQbQ ==
Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
Path: cpmsftngxa07.ph x.gbl
Xref: cpmsftngxa07.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1971 31
NNTP-Posting-Host: tk2msftngxa12.p hx.gbl 10.40.1.164
X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
Do we any computed column for the datagrid?
Do we any expression property for the datagrid columns?
My requirement is I ought to bind the following expression
at design time
if(au_lname=="" ,' ',"hi"+au_lname )
Is it possible to do so?