tags:

views:

93

answers:

2

I would like to display items from a Queue in a Gridview in Windows Forms. I can set the datasource attribute of the Gridview to the Queue, but it won't be automatically updated. I know I can use the BindingList class, but then I lose my Queue functionality.

Is there any way to combine the two classes, or do I have to implement one of the behaviours in a derived class?

What I'm doing is processing a list of items, I want to show the remaining ones in a grid. The data should not be changed by the user, but I want the GridView to be updated as the contents of the Queue change.

Example:

In form:

Proccessor pro = new Processor();
gridview.DataSource = pro.Items;

In class:

class Proccessor {
    Queue<DataBlock> _queue = new Queue();

    public Queue<DataBlock> Items  { 
        get { 
            return _queue;
        }
    }

    public void AutoProcess() {
        while (_queue.Count > 0) {
            Process(_queue.Dequeue());
        }
    }

    private void Process(DataBlock db) { ... }
}
+2  A: 

The whole purpose of a Queue is that entries can only be added in one place. So the idea of binding this to a UI grid so it can be updated is, uh, interesting - how should the UI look?

You'll definitely have to consider your own custom collection, or as you say, derive from BindingList and handle e.g. CancelNew accordingly. See the MSDN article for details.

Jeremy McGee
I don't want the data to be editable, just display it.
Robert Massa
+1  A: 

I would subclass Queue as QueueForDisplay. The constructor would take a view control. I would override the Enqueue and Dequeue methods. In those overrides, I would update the view control. If you don't like the tight coupling, you could simply subclass Queue as QueueWithEvents and provide OnEnqueue and OnDequeue events.