I received a comment from an individual wishing to set columns at runtime before binding an object and not have any other column information show up when bound. The situation below having a customer object that has a first name, last name, address and phone. The grid columns are set up to show bound columns FirstName, Address, Phone but not LastName. There is also an unbound column added called Phone2. The code that resulted is below.
private void CreateBindButtonClick(object sender, EventArgs e)
{
List<Customer> customers = new List<Customer>();
//Customer has a FirstName, LastName, Address, Phone
customers.Add(new Customer("John", "Smith", "123 race", "555-555-5555"));
customers.Add(new Customer("Bob", "Fanny", "5568 s. block", "555-554-5555"));
customers.Add(new Customer("Ken", "Little", "6552 gohome", "545-555-5555"));
customers.Add(new Customer("Joe", "Flint", "6 left turn", "555-595-5555"));
customers.Add(new Customer("Sue", "Ogden", "871 frank street", "456-555-5555"));
CreateColumns();
SetCustomerDataSource(customers);
}
private void SetCustomerDataSource(List<Customer> customers)
{
//binds to customers without a datamember and hides new columns
this.ultraGrid1.SetDataBinding(customers, null, true);
}
private void CreateColumns()
{
//always create a new band.
//Each time a band is added it looks to replace the old band.
UltraGridBand band = CreateBand();
//these are all Bounds columns
//A Customer contains FirstName, LastName, Phone, Address
//The LastName column is not added and wil not be shown
UltraGridColumn phoneColumn = new UltraGridColumn("Phone");
phoneColumn.Header.VisiblePosition = 2;
UltraGridColumn firstNameColumn = new UltraGridColumn("FirstName");
firstNameColumn.Header.VisiblePosition = 0;
UltraGridColumn addressColumn = new UltraGridColumn("Address");
addressColumn.Header.VisiblePosition = 1;
//this is an unbound column thus the second param in the constructor
UltraGridColumn phone2Column = new UltraGridColumn("Phone2", 3);
band.Columns.AddRange(new object[]
{
firstNameColumn,
addressColumn,
phoneColumn,
phone2Column
});
}
private UltraGridBand CreateBand()
{
UltraGridBand band = new UltraGridBand("NewBand", -1);
this.ultraGrid1.DisplayLayout.BandsSerializer.Add(band);
return band;
}
Hope this helps some of you dealing with the Infragistics controls.