torsdag 8. oktober 2015

Simple Objectlistview Example



























Objectlistview is a list view built upon the original Listview in Visual Studio, It is easy to use and it can do so much more than the original. Take a look at some of the things it can do, and download it at their  Website

After you have downloaded and extracted it you need to add it to your project by right-clicking Solution in the solution explorer then go to Add > Existing Project then you navigate to the 2012 version in the folder you just downloaded.
Now you just need to reference it in your project by right-clicking References then clicking Add Reference. In the window that pops up you should see a Projects tab on the left side, click it and check the box beside ObjectListView2012 and finally click ok. Now you should be ready to start coding

Here is some simple code using the Objectlistview to get you started

Person.cs
namespace ObjListViewTestApp
{
class Person
{
//Create the variables with getters and setters so that the Objectlistview can access them
public int age { get; set; }
public string name { get; set; }
public string surname { get; set; }
public string city { get; set; }
public Person(int age, string name, string surname, string city)
{
this.age = age;
this.name = name;
this.surname = surname;
this.city = city;
}
}
}
view raw Person.cs hosted with ❤ by GitHub
Form1.cs
using System.Collections.Generic;
using System.Windows.Forms;
namespace ObjListViewTestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Creating the columns
BrightIdeasSoftware.OLVColumn ageColumn = new BrightIdeasSoftware.OLVColumn();
BrightIdeasSoftware.OLVColumn nameColumn = new BrightIdeasSoftware.OLVColumn();
BrightIdeasSoftware.OLVColumn surnameColumn = new BrightIdeasSoftware.OLVColumn();
BrightIdeasSoftware.OLVColumn cityColumn = new BrightIdeasSoftware.OLVColumn();
//Adding the columns to the Objectlistview
objectList.AllColumns.Add(ageColumn);
objectList.AllColumns.Add(nameColumn);
objectList.AllColumns.Add(surnameColumn);
objectList.AllColumns.Add(cityColumn);
//Directing the columns to the correct variable names.
ageColumn.AspectName = "age";// <-- these names need to be the same as the ones in your class
nameColumn.AspectName = "name";
surnameColumn.AspectName = "surname";
cityColumn.AspectName = "city";
//Setting the header name on each column
ageColumn.Text = "Age";
nameColumn.Text = "Name";
surnameColumn.Text = "Surname";
cityColumn.Text = "City";
//Set the columns to fill the width of the objectlistview
ageColumn.FillsFreeSpace = true;
nameColumn.FillsFreeSpace = true;
surnameColumn.FillsFreeSpace = true;
cityColumn.FillsFreeSpace = true;
//Creating the header
objectList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {ageColumn,
nameColumn,
surnameColumn,
cityColumn });
//Creating a list of objects that are to be added to the Objectlistview
List<Person> masterList = new List<Person>();
masterList.Add(new Person(20, "Potato", "Tomato", "Tomato City"));
masterList.Add(new Person(20, "Criss", "Cross", "Cross City"));
masterList.Add(new Person(41, "blue", "cow", "Cow City"));
masterList.Add(new Person(12, "green", "cow", "Cow City"));
masterList.Add(new Person(20, "brown", "chicken", "Cow City"));
masterList.Add(new Person(55, "yellow", "chicken", "Tomato City"));
//Adding the object list to the Objectlistview
objectList.SetObjects(masterList);
}
}
}
view raw Form1.cs hosted with ❤ by GitHub

3 kommentarer:

  1. I can't see where you set the grouping by city ?

    SvarSlett
    Svar
    1. Line 22 in from1.cs. Unless you ment the sorting, thats a feature that is included with objectlistview

      Slett
  2. Do you have the sample projects in vb.n?

    SvarSlett