Question

How does the Key preoperty get set on an UltraGrid when calling SetDataBinding(object DataSource, string DataMember, bool hideNewColumns)? And How can I set it to something besides List'1?

I have the following form and class:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.ultraGrid2.SetDataBinding(new List<row>(), string.Empty, true);
        this.ultraGrid2.DisplayLayout.ColumnChooserEnabled = DefaultableBoolean.True; 
        // breakpoint on the above line and run the below immediate window code
    }
}

public class row
{
    public string Name { get; set; }
    public string Address { get; set; }
}

After setting the data binding, the below code always gives me List'1 in the immediate window:

this.ultraGrid2.DisplayLayout.Bands[0].Key
Was it helpful?

Solution

On the UltraGridBand object there is a private method that gets called during data binding called InitBandKey and it is within this method that the key is being set.

The logic for this is similar to the following:

CurrencyManager cm = (CurrencyManager)this.bindingManager;
if (cm.List is ITypedList) 
{
    newKey = ((ITypedList)cm.List).GetListName(null);
} 
else 
{
  newKey = cm.List.GetType().Name;
}     

In your example, you are getting the result of (new List<row>()).GetType().Name

You could define a class that derives from List<row> and then the name of that class would be the key of the band. For example:

public class CustomList:List<row>
{

}

And then the updated call to SetDataBinding for this example is:

this.ultraGrid1.SetDataBinding(new CustomList(), string.Empty, true);

OTHER TIPS

Try to set the Key of the Band[0] at design time.

Usually the Band[x].Key is the name of the DataTable/DataView that is binded, but in case of List<T> there is no name to use and perhaps this is automatically defined by the control

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top