首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据从列表中获取到windows窗体中的网格视图中。

将数据从列表中获取到windows窗体中的网格视图中。
EN

Stack Overflow用户
提问于 2017-02-14 21:41:56
回答 1查看 552关注 0票数 0

我正在尝试将事件日志文件解析为dataGridView中的Windows Forms。我需要将EventLogEntries public static List<EventLogEntry> _LogEntries { get; private set; }的列表放到网格视图中。我相信dataGridView会起作用,但listBox也可能。

我需要将_LogEntries列表中的数据输入到windows窗体中的网格视图中。我该怎么做?

下面是MainForm.cs for Windows Forms的代码

代码语言:javascript
复制
    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);
        }
    }

解析器类来自下面的EventLogParser.cs

代码语言:javascript
复制
    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;
        }
    }
}

我能够修复一些错误,但是现在当我调用ReadEventLog()时,抛出的‘System.ArgumentException类型的异常’发生在System.dll中,但是没有在用户代码中处理

EN

回答 1

Stack Overflow用户

发布于 2017-02-14 22:09:36

您应该以正确的方式初始化_LogEntries类的静态parser变量:

代码语言:javascript
复制
public static List<EventLogEntry> _LogEntries = new List<EventLogEntry>();

可以在静态构造函数中初始化静态属性,以便使其在使用之前已经初始化。

代码语言:javascript
复制
public static List<EventLogEntry> _LogEntries { get; private set; }

static parser()
{ 
     _LogEntries = new List<EventLogEntry>();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42236771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档