tags:

views:

1298

answers:

2

Here's the code that's disturbibg me a bit..

DataTable dt=GetDataTable();
for(int i=0<dt.Rows.Count;i++)
{
   string Name=dt.DefaultView[i].Row["FirstName"].ToString()
}

I need explanation of this, what actually dt.DefaultView[i] means here ?

+1  A: 

Actually, that code is redundant. A "view" maps the actual DataTable to something with virtual properties that can be used for data-binding scenarios. The default view is just the "all columns, in that order, no initial sort, no initial filter" layout.

With your code, the line:

string Name = dt.Rows[i]["FirstName"].ToString();

should be fine.

As an aside - the for usage and string-indexer can be tidied a bit:

    DataColumn col = dt.Columns["FirstName"];
    foreach (DataRow row in dt.Rows)
    {
        string name = (string)row[col];
        //.. do something with name
    }

This is bother easier to read and more efficient (the DataColumn indexer is the optimal one).

Marc Gravell
A: 

dt.DefaultView[i] returns the DataRowView instance of the row at index i in your DefaultView.

MSDN on DataRowView

Andy Rose