0

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
  • What language? What DB? Which library? Feb 26, 2009 at 11:10

2 Answers 2

1

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).

0

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

MSDN on DataRowView

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.