Thursday 22 March 2007

GridView : DIsplaying results of function using DataItem as parameter

Instead of just displaying the actual value of a field in a column (<%# Bind("Hours") %>) you can also call a function that uses the value to get another value.
e.g.
If we have ClientID but we want to display ClientName (another tabel in the db) we can assign:
Text='<%# GetClientName(DataBinder.Eval(Container.DataItem, "ClientID")) %>'

We then have a function in the page code:

protected string GetClientName(object iD)
{
return GetClientNameById(iD);
}

Note. the parameter must be defined as object because the actual type is unknown until binding.