简体   繁体   中英

Displaying an IList of User Objects in a DataGridView

We are Java developers trying to support a project in C# and can't seem to figure out how to bind a list of objects which is constantly being updated in and by the UI to a DataGridView so they are visible as soon as they are added.

There is a requirement that the UI track its use and display it back to the user in a data grid (for human factors analysis). We are implementing the IList interface and subclassing one of our logging classes:

public class DataSourceAppender : Logger, IList<LogEvent>
{
    private List<LogEvent> _events = new List<LogEvent>();

We add (append) a new LogEvent object to the list each time the user clicks a control or performs some operation of interest, recording various details of the interaction in the order in when they occurred. This list is dumped to a file at the end of the user test session for analysis. Now we need to show this to the user in a data grid as the session is progressing.

Our main method contains:

// create a new logger(appender) which holds all our log events
consoleSource = new DataSourceAppender();
consoleSource.Append("INIT", "Client Initialized");

// add the logger the the console data grid and wire-up the data binding
mainform.consoleDataGrid.AutoGenerateColumns = true;
mainform.consoleBinding.DataSource = typeof(LogEvent);
mainform.consoleBinding.DataSource = consoleSource;
consoleSource.Binding = mainform.consoleBinding;

The append method in the DataSourceAppender (aka consoleSource) contains:

public override void Append(string category, object entry)
{
    long now = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

    if (lastevent == 0) lastevent = now;

    try
    {
        LogEvent logevent = new LogEvent(now, category, (now - lastevent), Log.Interval, entry);

        // add the new log event to this data sorce
        lock (_events)
        {
            _events.Add(logevent);
        }
        if (_binding != null) _binding.ResetBindings(false);
    }
    catch (Exception e)
    {
        System.Console.Error.WriteLine(this.GetType().FullName + " error: " + e + ":" + e.Message);
    }
}

The results are the first "INIT" entry is displayed in the data grid but none of the other events in the DataSourceAppender (aka consoleSource) are displayed. They are all written to disk later so we know the Append method is getting called and otherwise working fine.

A few goals:

  • We are trying to stick to the Designer and not mess with generated code.
  • We would like to wire up the binding in the Main() method.
  • We would like the public properties of the LogEvent class to appear as column headings
  • This must remain a single-threaded application

We have been scouring forums and the MSDN pages and find loads of examples for binding to databases and manually updating the data grid, but precious little information on programmatically adding data to the data grid through its backing data store.

Any help would be greatly appreciated.

Since we can't see your entire DataSourceAppender class, I can't tell for sure what you're trying to do.

I see you've assigned consoleSource as your datasource, so the fact that you're not seeing any newly added items in the grid tells me you haven't correctly implemented IList to wrap _events ;

But you're making life harder than necessary by trying to implement IList anyway. Just assign _events as your datasource. If you like, you can expose it as a property of DataSourceAppender :

public IEnumerable<LogEvent> EventList { get { return _events; } }

then:

mainform.consoleBinding.DataSource = consoleSource.EventList;

Also you don't need this line:

mainform.consoleBinding.DataSource = typeof(LogEvent);

Basically this is all there is to it:

events = new Collection<LogEvent>(); // or List<> if you want
bindingSource.DataSource = events;
dataGridView.DataSource = bindingSource;

then add items like:

events.Add(event);
bindingSource.ResetBindings(false);

or simply :

bindingSource.Add(event);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM