DataCollection | ComponentOne
In This Topic
    Quick Start
    In This Topic

    This quick start will guide you through the steps of adding a DataGridView control to your application and add data to it using the C1DataCollection class.

    Complete the steps given below to see how the DataGridView control appears after data binding:

    1. Create a data source
    2. Bind DataGridView to the data source

    The following image shows how the DataGridView control appears after completing the steps above.

     The image shows a grid control.

    Step 1: Create a data source

    1. Add a new class file, Customer, to the application.
    2. Add the following code to the Customer file. In this example, we are using Customer class to represent data in the DataGridView control.
      Public Class Customer
          Private _id, _countryId As Integer
          Private _name, _email, _city As String
          Private _OrderDate As DateTime
          Private _orderTotal As Double
          Shared _rnd As Random = New Random()
          Shared _firstNames As String() = "Andy|Ben|Charlie|Dan|Ed|Fred|Herb|Jack|Mark|Ted".Split("|"c)
          Shared _lastNames As String() = "Ambers|Bishop|Cole|Danson|Evers|Frommer|Heath|Myers|Richards|Stevens".Split("|"c)
          Shared _emailServers As String() = "gmail|yahoo|outlook|aol".Split("|"c)
          Shared countries As String = "China-Beijing,Shanghai|India-Delhi,Kolkata|United States-Washington,New York|Russia-Moscow,Saint Petersburg|Japan-Tokio,Yokohama"
          Shared _countries As KeyValuePair(Of String, String())() = countries.Split("|"c).[Select](Function(str) New KeyValuePair(Of String, String())(str.Split("-"c).First(), str.Split("-"c).Skip(1).First().Split(","c))).ToArray()
          Public Sub New()
      
          End Sub
      
          Public Sub New(ByVal id As Integer)
      
              id = id
              Name = GetName()
              Email = String.Format("{0}@{1}.com", (Name.Substring(0, 3)).ToLower(), GetString(_emailServers))
              CountryId = _rnd.[Next]() Mod _countries.Length
              Dim cities = _countries(CountryId).Value
              City = GetString(cities)
              OrderDate = DateTime.Today.AddDays(-_rnd.[Next](1, 365)).AddHours(_rnd.[Next](0, 24)).AddMinutes(_rnd.[Next](0, 60))
              OrderTotal = Math.Round(_rnd.NextDouble() * 10000.0, 2)
      
          End Sub
      
          Public Property ID As Integer
      
              Get
                  Return _id
              End Get
      
              Set(ByVal value As Integer)
                  If value <> _id Then
                      _id = value
                  End If
              End Set
          End Property
      
          Public Property Name As String
              Get
                  Return _name
              End Get
              Set(ByVal value As String)
                  If value <> _name Then
      
                      _name = value
      
                  End If
              End Set
          End Property
      
          Public Property Email As String
              Get
                  Return _email
              End Get
      
              Set(ByVal value As String)
                  If value <> _email Then
                      _email = value
                  End If
              End Set
          End Property
      
          Public Property City As String
              Get
                  Return _city
              End Get
      
              Set(ByVal value As String)
                  If value <> _city Then
                      _city = value
                  End If
              End Set
          End Property
      
          Public Property CountryId As Integer
              Get
                  Return _countryId
              End Get
              Set(ByVal value As Integer)
      
                  If value <> _countryId AndAlso value > -1 AndAlso value < _countries.Length Then
      
                      _countryId = value
                  End If
              End Set
          End Property
      
          Public Property OrderDate As DateTime
      
              Get
      
                  Return _OrderDate
      
              End Get
      
              Set(ByVal value As DateTime)
      
                  If value <> _OrderDate Then
      
                      _OrderDate = value
      
                  End If
      
              End Set
      
          End Property
      
          Public Property OrderTotal As Double
      
              Get
      
                  Return _orderTotal
      
              End Get
      
              Set(ByVal value As Double)
      
                  If value <> _orderTotal Then
      
                      _orderTotal = value
      
                  End If
      
              End Set
      
          End Property
      
          Private Shared Function GetString(ByVal arr As String()) As String
      
              Return arr(_rnd.[Next](arr.Length))
      
          End Function
      
          Private Shared Function GetName() As String
      
              Return String.Format("{0} {1}", GetString(_firstNames), GetString(_lastNames))
      
          End Function
      
          Public ReadOnly Property Country As String
      
              Get
      
                  Return _countries(_countryId).Key
      
              End Get
      
          End Property
      
          Public Shared Function GetCustomerList(ByVal count As Integer) As ObservableCollection(Of Customer)
      
              Dim list = New ObservableCollection(Of Customer)()
      
              For i As Integer = 0 To count - 1
      
                  list.Add(New Customer(i))
      
              Next
      
              Return list
      
          End Function
      
      End Class
      
      public class Customer
      {   
          int _id, _countryId;
          string _name, _email, _city;
          DateTime _OrderDate;
          double _orderTotal;
      
          static Random _rnd = new Random();
          static string[] _firstNames =
             "Andy|Ben|Charlie|Dan|Ed|Fred|Herb|Jack|Mark|Ted".Split('|');
          static string[] _lastNames =
             "Ambers|Bishop|Cole|Danson|Evers|Frommer|Heath|Myers|Richards|Stevens".Split('|');
          static string[] _emailServers = "gmail|yahoo|outlook|aol".Split('|');
          static string countries =
             "China-Beijing,Shanghai|India-Delhi,Kolkata|United States-Washington,New York|Russia-Moscow,Saint Petersburg|Japan-Tokio,Yokohama";
          static KeyValuePair<string, string[]>[] _countries =
             countries.Split('|').Select(str => new KeyValuePair<string, string[]>(str.Split('-').First(),
                 str.Split('-').Skip(1).First().Split(','))).ToArray();
          
          public Customer()
          {
          }
      
          public Customer(int id)
          {
              ID = id;
              Name = GetName();
              Email = string.Format("{0}@{1}.com", (Name.Substring(0, 3)).ToLower(), GetString(_emailServers));
              CountryId = _rnd.Next() % _countries.Length;
              var cities = _countries[CountryId].Value;
              City = GetString(cities);
              OrderDate = DateTime.Today.AddDays(-_rnd.Next(1, 365)).AddHours(_rnd.Next(0, 24)).AddMinutes(_rnd.Next(0, 60));
              OrderTotal = Math.Round(_rnd.NextDouble() * 10000.00, 2);
          }
          
          public int ID
          {
              get { return _id; }
              set
              {
                  if (value != _id)
                  {
                      _id = value;
                  }
              }
          }
          public string Name
          {
              get { return _name; }
              set
              {
                  if (value != _name)
                  {
                      _name = value;
                  }
              }
          }
          public string Email
          {
              get { return _email; }
              set
              {
                  if (value != _email)
                  {
                      _email = value;
                  }
              }
          }
          public string City
          {
              get { return _city; }
              set
              {
                  if (value != _city)
                  {
                      _city = value;
                  }
              }
          }
      
          public int CountryId
          {
              get { return _countryId; }
              set
              {
                  if (value != _countryId && value > -1 && value < _countries.Length)
                  {
                      _countryId = value;
                  }
              }
          }
          public DateTime OrderDate
          {
              get { return _OrderDate; }
              set
              {
                  if (value != _OrderDate)
                  {
                      _OrderDate = value;
                  }
              }
          }
      
          public double OrderTotal
          {
              get { return _orderTotal; }
              set
              {
                  if (value != _orderTotal)
                  {
                      _orderTotal = value;
                  }
              }
          }
        
          static string GetString(string[] arr)
          {
              return arr[_rnd.Next(arr.Length)];
          }
          static string GetName()
          {
              return string.Format("{0} {1}", GetString(_firstNames), GetString(_lastNames));
          }
          public string Country
          {
              get { return _countries[_countryId].Key; }
          }
      
          // ** static list provider
          public static ObservableCollection<Customer> GetCustomerList(int count)
          {
              var list = new ObservableCollection<Customer>();
              for (int i = 0; i < count; i++)
              {
                  list.Add(new Customer(i));
              }
              return list;
          }  
      }
      
    Back to Top

    Step 2: Bind DataGridView to the data source

    1. Add the following dlls to your application to work with DataCollection:
      • C1.DataCollection.dll
      • C1.Win.DataCollection.dll

      You can also use the available DataCollection NuGet packages from the following locations:

      For information on how to add NuGet packages to your application, see Adding NuGet Packages to your App.

    2. Drag and drop the DataGridView control from the Toolbox onto your form.
    3. Switch to the Code view and add the following code to bind DataGridView to the data source.
      Dim cv As C1DataCollection(Of Customer) = New C1DataCollection(Of Customer)(Customer.GetCustomerList(100))
      grid.ItemsSource = New C1DataCollectionBindingList(cv)
      
      cv = new C1DataCollection<Customer>(Customer.GetCustomerList(100));
      grid.DataSource  = new C1DataCollectionBindingList(cv);
      

    Run the application and observe that the grid displays a Customers table.

    Back to Top