filling a listview in

Hi,

I have an application that registers how many hours I'm spending on my project. I want to display the hours a user spend on each project per day in a listview. I defined the listview (in the design screen) to have 3 columns (Date, Project and Time).

I use the following code to fill the listview when a user is selected in a combobox:

if (cmbViewUser.SelectedIndex > -1){
SqlCommand cmdLoadLstTimes = conTimeKeeper.CreateCommand();
SqlDataReader drUserRegistrations;

cmdLoadLstTimes.CommandType = CommandType.StoredProcedure;
cmdLoadLstTimes.CommandText =
"stp_tik_GetUserRegistrationsPerDay";
cmdLoadLstTimes.Parameters.AddWithValue(
"@UserDisplayName", cmbViewUser.SelectedItem.ToString());

drUserRegistrations = cmdLoadLstTimes.ExecuteReader();

lstTimes.Clear();

while (drUserRegistrations.Read()){
//Create a ListViewItem
ListViewItem lviRegistration = new ListViewItem("PARENT item");

//Create the ListViewSubItems
ListViewItem.ListViewSubItem lvsDate = new ListViewItem.ListViewSubItem(lviRegistration, drUserRegistrations[0].ToString());
ListViewItem.ListViewSubItem lvsProject = new ListViewItem.ListViewSubItem(lviRegistration, drUserRegistrations[1].ToString());
ListViewItem.ListViewSubItem lvsTime = new ListViewItem.ListViewSubItem(lviRegistration, drUserRegistrations[2].ToString());

//Add the ListViewSubItems to the ListViewItem
lviRegistration.SubItems.Add(lvsDate);
lviRegistration.SubItems.Add(lvsProject);
lviRegistration.SubItems.Add(lvsTime);

//Add the ListViewItem to the ListView
lstTimes.Items.Add(lviRegistration);
}

drUserRegistrations.Dispose();
}

The problem is, the listview is always empty but the stp is returning a result... What am I missing here

El Loco



Answer this question

filling a listview in

  • Mule

    The following line of your code might have caused the problem you mentioned:

    lstTimes.Clear();

    After calling Clear() method on the list view control caused it to clear all the things including the header columns !! Thus loses all the iformation associated with the sub items !!! Upon changes on the combo box index, instead of calling as above, use the following code to clear the list itmes:

    lstTimes.Items.Clear();

    Hope this may help.

    Cheers


  • Michael Pritchard

    I already figured out that it was the lstTimes.Clear() statement, but I solved it by adding the collumns every time the listview is cleared. Using the lstTimes.Items.Clear() statement is of course a better solution, so I'm going to implement this.

    Thanks for the help!
    El Loco


  • filling a listview in