private void FillList()
    {
       listView.Items.Clear();
     
       DataTable dtPeople = GetPeople();
     
       listView.BeginUpdate();
       foreach (DataRow dr in dtPeople.Rows)
    {

       ListViewItem listItem = new ListViewItem(dr["id"].ToString());
       listItem.ImageIndex = 0;
    // Get child rows from a parent one.
    // person_name is a name of relation between two
    // tables.
    DataRow[] r = dr.GetChildRows("person_name");
    if (r.Length != 0)
    {
       listItem.SubItems.Add(r[0][0].ToString());
       listView.Items.Add(listItem);
    }
     
    if (listView.Columns.Count == 0)
    {
       listView.Columns.Add("ID", 100, HorizontalAlignment.Left);
       listView.Columns.Add("Name", 100, HorizontalAlignment.Left);
    }
    listView.EndUpdate();
    }
    }

I am having problem understanding foreach (DataRow dr in dtPeople.Rows)

What is dr

When I retype the code into my VS It shows me error, that he can't find "dr"....

Thanks in advance...

dr is the temporary reference to the current DataRow in dtPeople.Rows . In a foreach statement, since you are not necessarily accessing an array like you would with a regular for loop there needs to be some way to reference the current item. In this case 'dr' does just that.

Okay but where I declare that its going to be dr not bt ?

Okay but where I declare that its going to be dr not bt ?

You declare it IN the foreach statement. This is why it is temporary - after each iteration of the for-each dr goes out of scope, becoming null. It is then reasigned to reference the next object in the collection.

This is the exact line that it is declared on: foreach (DataRow dr in dtPeople.Rows)

commented: helped me fast and clear +2

A foreach loop is just an easier way to access each item in a list or array.

foreach (DataRow dr in dtPeople.Rows)
{
	//do something with dr
}

is similar to

for (int i = 0; i < dtPeople.Rows.Count; i++)
{
	DataRow dr = dtPeople.Rows[i];
	//do something with dr
}

The dr variable is used as a place holder for the currently indexed item.
Using foreach also means you do not need to worry about the length of the list or array.

Off the original question but the listView.EndUpdate(); is inside the foreach loop.
I would expect it to be after the closing } of the loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.