Geeks With Blogs
Jim Lahman's Blog Fortitude|Endurance|Faith|Teamwork

I created a listview with two columns as part of a text C# solution to test the use of structures. This list was created with the IDE. When creating a list where you want to show multiple columns, set the listview's view property to Details. I use the IDE to define the columns by selecting columns from the properties list.

To fill in the listview before displaying it, I programmatically add the items in the form's LOAD event.

BTW, I'm moving from VB.Net to C# and adding events to the form is different in C# from VB .Net. Rather than selecting the event from a drop-down list (as in VB), the event is pro grammatically defined as shown here:

this.Load += new EventHandler(frmL1Cmn_Load);
this.Activated += new EventHandler(frmL1Cmn_Activated);

Here's the generated callback

void frmL1Cmn_Activated(object sender, EventArgs e)
{
     throw new NotImplementedException();
}

But, to add this eventhandler is much different than VB. After enter +=, let the IDE instruct you to enter the TAB. It is at this point that the TAB key is depressed. Then, let the IDE prompt you one more time to hit the TAB key to generate the callback function. Select the TAB key a 2nd time and the callback function is generated.

Coming back from this tangent, here's the code in the form's load callback function to populate the listview.

void frmL1Cmn_Load(object sender, EventArgs e)
{
   try
   {
      // Populate lstReadL1Ints with lngVals
      lstReadL1Ints.BeginUpdate();
      lstReadL1Ints.Items.Clear();
      for (i = 0; i < 50; i++)
     {
           ListViewItem row = new ListViewItem(i.ToString());
           int j = i * 2;
           row.SubItems.Add(l1CmnRec[i].lngVal[j].ToString());
           lstReadL1Ints.Items.Add(row);
     }
     lstReadL1Ints.EndUpdate();
     lstReadL1Ints.Refresh();
  }
  catch (Exception ex)
   {
      StringBuilder sb = new StringBuilder();
      sb.Append("Cannot fill in Int listview. ex = ").Append(ex.Message);
     Console.WriteLine(sb.ToString());
   }
}

Posted on Sunday, April 5, 2009 4:50 PM | Back to top


Comments on this post: Adding items to a listview in C#

# re: Adding items to a listview in C#
Requesting Gravatar...
i think it is better way for understanding c# code thank you so much
Left by vipin on Oct 12, 2009 4:12 AM

Your comment:
 (will show your gravatar)


Copyright © Jim Lahman | Powered by: GeeksWithBlogs.net