Mathias Brandewinder on .NET, F#, VSTO and Excel development, and quantitative analysis / machine learning.
by Mathias 20. February 2011 17:27

While browsing the ListObject documentation today, I realized that, while all the examples given focused on binding to a DataSet, it also supports databinding to “any component that implements the IList interface”. This is something I wasn’t aware of, so I figured I would give it a try.

I quickly created a Excel 2007 Template project in VS2010, and added a simple Product class as follow:

public class Product
{
   public string Name { get; set; }
   public double Price { get; set; }
}

I then added the following code behind Sheet1, creating a straightforward list of Product, as well as a ListObject, setting the DataSource to the list:

public partial class Sheet1
{
   private List<Product> products;
   private ListObject listObject;

   private void Sheet1_Startup(object sender, System.EventArgs e)
   {
      this.listObject = this.Controls.AddListObject(this.Range["B2"], "Products");

      this.products = new List<Product>();
      this.products.Add(new Product() { Name = "Alpha", Price = 10d });
      this.products.Add(new Product() { Name = "Bravo", Price = 20d });
      this.products.Add(new Product() { Name = "Charlie", Price = 30d });

      this.listObject.DataSource = products;
      this.listObject.AutoSetDataBoundColumnHeaders = true;
   }
   // auto-generated code omitted
}

Hit F5, and watch:

image

Out of the box, we get a nicely formatted list, with filters in the headers. If anything, that’s a convenient way to display a list of items on a Worksheet. I didn’t have time to dig deeper into it, but I am now very curious about how much more can be done with this mechanism. Can I add data validation? Can I control what properties to display?

Comments

2/21/2011 12:15:46 PM #

trackback

Codebix.com - Your post is on Codebix.com

This post has been featured on Codebix.com. The place to find latest articles on programming. Click on the url to reach your post's page.

Codebix.com | Reply

3/20/2011 9:27:24 PM #

Mustafa

Hi Mathias,

Could the concept in your article be extended to binding a ListObject to a WPF control?  Specifically, I'd appreciate any guidance you can provide about how to implement the following scenarios in a VSTO Add-in for Excel:

1) Two-way binding between a spreadsheet cell (named range) and a WPF text box hosted in a custom task pane, where the WPF textbox is automatically updated when the bound cell value changes and vice versa;

2) Two-way binding between a ListObject and a WPF DataGrid hosted in  a custom task pane, where the DataGrid is automatically updated when the value of any cell within the ListObject changes or vice versa.

Thanks,

Mustafa.

Mustafa Australia | Reply

12/22/2016 3:34:16 AM #

pingback

Pingback from steroidsforsale.biz

ansomone

steroidsforsale.biz | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Comments

Comment RSS