How to: Respond to the Solver Data Binding Event

Solver Foundation 3.0

You can subscribe to the data binding event that is raised by a solver and respond to the event. The following procedure demonstrates how to add a data binding event and change the VariableUpperBound field in response to the event.

To respond to the solver data binding event

  1. Add the following Imports or using statements to the top of the Program code file.

    using Microsoft.SolverFoundation.Common;
    using Microsoft.SolverFoundation.Services;
  2. Declare a SolverContext object to create a model.

    private SolverContext context;
  3. Add the following line of code to your model to subscribe to the solver data binding event.

    context = SolverContext.GetContext();
    Model model = context.CreateModel();
    context.DataBinding += new EventHandler<DataBindingEventArgs>(context_DataBinding);
  4. Implement the context_DataBinding method. The following code changes the VariableUpperBound field. First, the code enumerates the indexes for every decision in the model by using the Indexes property, which is used as an array because decisions can have multiple indexes. The ComputeUpperBoundForIndex method is business logic that is user-defined.

    static void context_DataBinding(object sender, DataBindingEventArgs e) {
      foreach (object[] indexes in e.Indexes) {
        e[SolverProperties.VariableUpperBound, indexes] =