Thursday, June 28, 2012

Quick way to display a grid of information in a WinForms application.

This C# snippet can be used in a WinForms application to quickly display a grid of information.

Note how the column names are defined using LINQ to Objects.
if (EomAppCommon.Settings.DebugEomDatabase)
{
var form = new System.Windows.Forms.Form();
var grid = new System.Windows.Forms.DataGridView {
AutoGenerateColumns = true,
Dock = System.Windows.Forms.DockStyle.Fill,
DataSource =
(from c in new [] {
Tuple.Create("DADatabaseR1ConnectionString", this.DADatabaseR1ConnectionString),
Tuple.Create("StatsYear", this.StatsYear.ToString()),
Tuple.Create("StatsMonth", this.StatsMonth.ToString()),
Tuple.Create("StatsDaysInMonth", this.StatsDaysInMonth.ToString()),
Tuple.Create("PubReportSubjectLine", this.PubReportSubjectLine),
} select new {
Key = c.Item1,
Value = c.Item2,
}).ToList(),
AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill,
};
form.Controls.AddRange(new[] { grid, });
form.ShowDialog();
}
view raw gistfile1.cs hosted with ❤ by GitHub

No comments:

Post a Comment