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.

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();
}
}
}