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 (a.k.a 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 (a.k.a 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
DataSourceAppenderclass, I can’t tell for sure what you’re trying to do.I see you’ve assigned
consoleSourceas your datasource, so the fact that you’re not seeing any newly added items in the grid tells me you haven’t correctly implementedIListto wrap_events;But you’re making life harder than necessary by trying to implement
IListanyway. Just assign_eventsas your datasource. If you like, you can expose it as a property ofDataSourceAppender:then:
Also you don’t need this line:
Basically this is all there is to it:
then add items like:
or simply :