datagrid-binding

  • Thread starter Thread starter reiks
  • Start date Start date
R

reiks

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?
 
Hi,

Here are two ways you can accomplish this.

1)
In the HTML template tag, you normally write something like this:
<%# DataBinder.Eval(Container.DataItem, "au_id") %>
or
<%# Container.DataItem("au_id") %>

You could instead call into a function as follows:
In the item template:
<%# CheckLength(Container.DataItem("ProductName")) %>

In the code-behind:
Protected Function CheckUnit(UnitsOnOrder As Object) As String
If Integer.Parse(UnitsOnOrder) = 0 Then
Return "Out Of Stock"
Else
Return UnitsOnOrder.ToString()
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/SourceCode/HelperFunction.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="True"></asp:datagrid>

*** in the code-behind

Dim Counter As Integer = 0

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Bind()
End If
End Sub

Private Sub Bind()
Dim Qry1 As System.Data.SqlClient.SqlDataReader
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='pubs'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT au_id, au_lname, au_fname FROM
authors"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlConnection.Open()
Qry1 =
sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
DataGrid1.DataSource = Qry1
DataGrid1.DataBind()
Qry1.Close()
sqlCommand.Dispose()
sqlConnection.Close()
sqlConnection.Dispose()
End Sub

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
'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.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
If e.Item.Cells(1).Text = "Green" Then
Dim l As WebControls.LinkButton = New WebControls.LinkButton
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.Footer Then
e.Item.Cells(0).Text = Counter.ToString & " 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.


--------------------
 
Back
Top