【问题标题】:Getting the data from a List into a grid view in windows forms将列表中的数据获取到 Windows 窗体中的网格视图中
【发布时间】:2017-02-14 21:41:56
【问题描述】:

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

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

下面是MainForm.csWindows 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);
        }
    }

来自下面 EventLogParser.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;
        }
    }
}

我能够修复一些错误,但现在当我调用 ReadEventLog() 时,它会抛出“System.ArgumentException 类型的异常”发生在 System.dll 中,但未在用户代码中处理

【问题讨论】:

  • 你运行过这个吗?你有错误吗?如果有,它们是什么?
  • 我已经运行它,它在bs.DataSource = parser._LogEntries; 行中断,但类型为“System.TypeInitializationException”
  • 你是新来的,所以这不是问题,但将来你会希望在你的问题中包含这些错误,它可以帮助人们回答:)无论如何,我从来没有看到parserget在任何地方定义......这是问题的一部分吗?
  • 不,我很抱歉它在不同的 .cs 文件中。
  • 我把它放在其他代码sn-p下面

标签: c# winforms datagridview event-log


【解决方案1】:

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

public static List<EventLogEntry> _LogEntries = new List<EventLogEntry>();

您可以在静态构造函数中初始化您的静态属性,使其在使用前已被初始化。

public static List<EventLogEntry> _LogEntries { get; private set; }

static parser()
{ 
     _LogEntries = new List<EventLogEntry>();
}

【讨论】:

  • 我看不出你到底是在哪里初始化的。
  • 解析器类之首
  • 啊,你刚刚更新了你的代码。还在抛出那个错误吗?
  • 更新了在静态构造函数中初始化静态属性的答案。请用静态初始化器测试!!!
  • 您遇到了另一个异常,即您正在使用的变量中有空值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 1970-01-01
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多