从列表中获取数据到窗体中的网格视图
我想解析事件日志文件到dataGridView
Windows Forms
。 我需要将EventLogEntry public static List<EventLogEntry> _LogEntries { get; private set; }
的列表放入网格视图中。 我相信dataGridView会工作,但列表框也可能。从列表中获取数据到窗体中的网格视图
我需要从我的列表
_LogEntries
获取数据到Windows窗体网格视图。我会怎么做?
下面是从下面MainForm.cs
public static class parser
{
public static string EvlLocation { get; set; }
public static string evlLocationManual = "K:\\Event Log\\Test\\Test.evt";
public static List<EventLogEntry> _LogEntries { get; private set; }
static parser()
{
_LogEntries = new List<EventLogEntry>();
}
public static void ReadEventLog()
{
EventLog eventLog = new EventLog(EvlLocation);
EventLogEntryCollection eventLogEntries = eventLog.Entries;
int eventLogEntryCount = eventLogEntries.Count;
for (int i = 0; i < eventLogEntries.Count; i++)
{
EventLogEntry entry = eventLog.Entries[i];
//Do Some processing on the entry
}
_LogEntries = eventLogEntries.Cast<EventLogEntry>().ToList();
}
public static void ParseTest()
{
evlLocationManual = "K:\\Event Log\\Test\\Test.evt";
ReadEventLog();
}
public static void setLogLocation(string input)
{
EvlLocation = input;
}
}
public static class EventLogEntryCollection_Container
{
public static void testCollection()
{
string myLogName = "_log";
// Create an EventLog instance and assign its source.
EventLog _log = new EventLog();
_log.Source = "%Program Files (x86)%\\EventLogParser\\ImportedEventLogs\\" + varBank.logInput;
// Write an informational entry to the event log.
_log.WriteEntry("Successfully created a new Entry in the Log");
_log.Close();
// Create a new EventLog object.
EventLog myEventLog1 = new EventLog();
myEventLog1.Log = myLogName;
// Obtain the Log Entries of "_log".
EventLogEntryCollection _logCollection = _log.Entries;
_log.Close();
// Copy the EventLog entries to Array of type EventLogEntry.
EventLogEntry[] _logEntryArray = new EventLogEntry[_logCollection.Count];
_logCollection.CopyTo(_logEntryArray, 0);
IEnumerator myEnumerator = _logEntryArray.GetEnumerator();
while (myEnumerator.MoveNext())
{
EventLogEntry myEventLogEntry = (EventLogEntry)myEnumerator.Current;
}
}
}
我能够修正一些错误的代码从EventLogParser.cs
Windows Forms
private List<Foo> ComputerName = new List<Foo>(); private List<Foo> EventId = new List<Foo>(); private List<Foo> EventType = new List<Foo>(); private List<Foo> SourceName = new List<Foo>(); private List<Foo> Message = new List<Foo>(); class Foo : INotifyPropertyChanged { private string bar_; public string Bar { get { return bar_; } set { bar_ = value; NotifyPropertyChanged("Bar"); } } public Foo(string bar) { this.Bar = bar; } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public override string ToString() { return bar_; } } private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { var bs = new BindingSource(ds, "Events"); Foo foo1 = new Foo("TEST PC"); ComputerName.Add(foo1); parser.ReadEventLog(); bs.DataSource = parser._LogEntries; //Bind fooList to the dataGridView dataGridView1.DataSource = bs; //I can see bar1 in the listbox as expected this.Invoke(pbHandler, new object[] { 100, 100 }); } // Open the log file private void OpenFile() { string evlLocation = ""; // Show file open dialog if (openFile.ShowDialog() == DialogResult.OK) { // Create a dataset for binding the data to the grid. ds = new DataSet("EventLog Entries"); ds.Tables.Add("Events"); ds.Tables["Events"].Columns.Add("ComputerName"); ds.Tables["Events"].Columns.Add("EventId"); ds.Tables["Events"].Columns.Add("EventType"); ds.Tables["Events"].Columns.Add("SourceName"); ds.Tables["Events"].Columns.Add("Message"); // Start the processing as a background process evlLocation = openFile.FileName; parser.setLogLocation(openFile.FileName); worker.RunWorkerAsync(openFile.FileName); } }
解析器类,但现在当我调用
ReadEventLog()
它抛出`类型System.ArgumentException异常'在System.dll中发生但public static List<EventLogEntry> _LogEntries = new List<EventLogEntry>();
可以初始化静态构造函数的静态属性,以便它会使它具有:在用户代码
你应该初始化parser
类的静态_LogEntries
变量在适当的方式没有处理在使用之前已经初始化。
public static List<EventLogEntry> _LogEntries { get; private set; }
static parser()
{
_LogEntries = new List<EventLogEntry>();
}
我做了它在底部的代码 –
我没有看到你究竟在哪里初始化。 –
解析器类的顶部 –
你跑了吗?你有错误吗?如果是这样,他们是什么? – Rinktacular
我已经运行它,它在线'bs.DataSource = parser._LogEntries;'中断了'类型'System.TypeInitializationException'的异常 –
你在这里是新的,所以这不是一个问题,但在未来,你会想要在你的问题中包括这些错误,它可以帮助人们回答:) 无论如何,我从来没有看到'parser'在任何地方被定义..是问题的一部分吗? – Rinktacular