Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »
This sample creates a windows form application that retrieves event log entries and displays them in a grid. It demonstrates how to databind, and filter entries retrieved from the Windows Event Log.

Screenshot of Event Log Viewer built using LINQ


sample project code can be Downloaded here.


To create:

1. Start Visual Studio 2008 (download the Express version which is free from Microsoft's website if you don't have a copy of Visual Studio 2008 Professional or above)
2. Choose File-New Project from the menu
3. Choose a C# Windows Forms application
4. Drag a combo box from the toolbar onto the form. Change its name to comboBoxSource
5. Drad a DataGridView onto the form. Change its name to dataGridView
6. Drag an EventLog component from the toolbar (its in the Components group) into the form area. Change its name to eventLog.
7. Add the clause: using System.Diagnostics under the existing using references
8. Change the name of the Form1 to MainForm. Implement the main forms load event, as
        private void MainForm_Load(object sender, EventArgs e)
        {
            // retrieve a list of all event log sources, and populate the drop-down combo box
            eventLog.Log = "Application";
 
            var sources = (from EventLogEntry es in eventLog.Entries
                           orderby es.Source
                           select es.Source).Distinct();
 
            comboBoxSource.DataSource = sources.ToList();
        }

9. Implement the comboBox's SelectedIndexChange event as -
        private void comboBoxSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            // retrieve all the event log entries matching the source selection in the combo-box
            var query = from EventLogEntry el in eventLog.Entries
                        where el.Source == comboBoxSource.Text
                        orderby el.TimeGenerated descending
                        select new
                        {
                            Time = el.TimeGenerated,
                            Source = el.Source,
                            Message = el.Message
                        };
 
            dataGridView.DataSource = query.ToList();
 
        }



The complete code listing for the MainForm is shown here -

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
 
namespace EventLogViewer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
 
        private void MainForm_Load(object sender, EventArgs e)
        {
            // retrieve a list of all event log sources, and populate the drop-down combo box
            eventLog.Log = "Application";
 
            var sources = (from EventLogEntry es in eventLog.Entries
                           orderby es.Source
                           select es.Source).Distinct();
 
            // bind the result to the combo-box
            comboBoxSource.DataSource = sources.ToList();
        }
 
        private void comboBoxSource_SelectedIndexChanged(object sender, EventArgs e)
        {
            // retrieve all the event log entries matching the source selection in the combo-box
            var query = from EventLogEntry el in eventLog.Entries
                        where el.Source == comboBoxSource.Text
                        orderby el.TimeGenerated descending
                        select new
                        {
                            Time = el.TimeGenerated,
                            Source = el.Source,
                            Message = el.Message
                        };
 
            // bind the results to the grid-view 
            dataGridView.DataSource = query.ToList();
 
        }
    }
}

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.