views:

36

answers:

1

I am doing a tutorial where you use a templatefield in the gridview control to call a function.

I don't understand the code for the function. What is the object Northwind.EmployeesRow?

This is the tutorial I am doing.

Tutorial 12: Using TemplateFields in the GridView Control

And this is the code for the function.

Protected Function DisplayDaysOnJob(ByVal employee As Northwind.EmployeesRow) As String

    If employee.IsHireDateNull() Then
        Return "Unknown"
    Else
        ' Returns the number of days between the current
        ' date/time and HireDate
        Dim ts As TimeSpan = DateTime.Now.Subtract(employee.HireDate)
        Return ts.Days.ToString("#,##0")
    End If

End Function
+1  A: 

About 3/4 of the way down the tutorial there's the text:

Container.DataItem returns a DataRowView object that corresponds to the DataSource record bound to the GridViewRow. Its Row property returns the strongly typed Northwind.EmployeesRow

Which indicates to me that Northwind.EmployeesRow is a strongly typed DataRow. You might want to take a read through "Data Points: Efficient Coding With Strongly Typed DataSets" to get a feel for what strongly typed DataSets are and how they work.

In a nuthshell, when using a "normal" dataset, you'd write code like:

foreach(DataRow record in myDataSet.Tables[0].Rows)
{
  var employeeId = Convert.ToInt32(record["employeeId"]);
  GivePayriseTo(employeeId);
}

With a strongly typed DataRow/DataSet you can do:

foreach(Northwind.EmployeesRow employee in Employees.Rows)
{
  GivePayriseTo(employee.EmployeeId);
}
Rob
There's also a pretty good explanation from 4guysfromrolla.com: http://www.4guysfromrolla.com/articles/020806-1.aspx
Rob