Monday, June 14, 2010

DataGridView with Typed DataSet

I was recently working on an assignment creating a Windows Forms application with a DataGridView being fed data from some object collection, and NOT (as is the case with virtually all samples that can be found on the Internet) using the SqlDataAdapter.

Even though this is a pretty simple task once you've found out how to do it, I did find it a bit tricky to get to work. So here's how it can be done.

  1. Start Visual Studio and create a new Windows Forms application.
  2. Add a DataSet to the project by right-clicking on the project and selecting Add -> New Item and selecting the DataSet template naming it ContactDataSet. This should bring up the DataSet designer.
  3. Add a new DataTable to the designer area using the Toolbox. Name it Contact and add some columns to table (see details such as column data types in the sample code)
  4. Add a new class to the project and name it Address:

    public class Address
    {
    public long Id { get; set; }
    public string Street { get; set; }
    public int HouseNumber { get; set; }
    public string PostalCode { get; set; }
    public string PostalDistrict { get; set; }
    }

  5. Add another new class to the project and name it Contact:

    public class Contact
    {
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? Age { get; set; }
    public Address Address { get; set; }
    }


  6. In Form1.cs add this private member:

    private ContactDataSet _contactDataSet = new ContactDataSet();


  7. Also in Form1.cs add this method for generating a collection of test data:

    private IList<Contact> GenerateData()
    {
    IList<Contact> contactList = new List<Contact>();
    contactList.Add(new Contact
    {
    Id = 1,
    FirstName = "Peter",
    LastName = "Jackson",
    Age = 53,
    Address = new Address
    {
    Street = "Long Road",
    HouseNumber = 173,
    PostalCode = "12345",
    PostalDistrict = "Fake Town"
    }
    });
    contactList.Add(new Contact
    {
    Id = 2,
    FirstName = "Pavlov",
    LastName = "Ivanowich",
    Address = new Address
    {
    Street = "Park Avenue",
    HouseNumber = 1011,
    PostalCode = "98765",
    PostalDistrict = "Imaginaryville"
    }
    });
    contactList.Add(new Contact
    {
    Id = 3,
    FirstName = "Onslow",
    LastName = "Bucket",
    });
    return contactList;
    }

  8. Furthermore, add this method for filling the DataSet with data from a collection:

    private void FillDataSet(IList<Contact> contactList)
    {
    foreach (Contact contact in contactList)
    {
    ContactDataSet.ContactRow row = _contactDataSet.Contact.NewContactRow();
    row.FirstName = contact.FirstName;
    row.LastName = contact.LastName;
    if (contact.Age.HasValue)
    {
    row.Age = contact.Age.Value;
    }
    if (contact.Address != null)
    {
    row.Street = contact.Address.Street;
    row.HouseNumber = contact.Address.HouseNumber;
    row.PostalCode = contact.Address.PostalCode;
    row.PostalDistrict = contact.Address.PostalDistrict;
    }
    _contactDataSet.Contact.AddContactRow(row);
    }

    // OK, so we have filled in the data we want to start up with.
    // We must now call AcceptChanges so that this data isn't change-tracked.
    _contactDataSet.AcceptChanges();
    }

  9. Go to the Form Designer and add a BindingSource and name it contactBindingSource. Set the ContactDataSet as DataSource and Contact as DataMember.
  10. In the Form Designer also add a DataGridView and set contactBindingSource as DataSource.
  11. In the Form1.cs add this code in the Load event handler:

    IList<Contact> contactList = GenerateData();
    FillDataSet(contactList);

    contactBindingSource.DataSource = _contactDataSet;

  12. Run the application. The DataGridView should now display the test data.