Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I made some helper classes so I could use the DataGrid as a kind of DataTable. In other words, I wanted the formatting, sorting, and polished look of the DataGrid without having to pre-fab some classes beforehand. The main reason I wanted this was for a testing suite, I wanted to be able to create an arbitrary number of columns and at runtime. Here's what I got</p> <pre><code>public class DataRow { internal List&lt;object&gt; Items = new List&lt;object&gt;(); public object this[string value] { get { return Items[Convert.ToInt32(value)]; } } public string GetString(int index) { return Items[index].ToString(); } public object GetObject(int index) { return Items[index]; } public DataRow(params object[] values) { if (values == null || values.Length &lt; 1) throw new Exception("You must pass in some values"); Items.AddRange(values); } } public class GridConstructor { public List&lt;DataRow&gt; Rows = new List&lt;DataRow&gt;(); private DataRow headers; public GridConstructor(DataRow head) { headers = head; } public void BuildInto(DataGrid grid) { grid.AutoGenerateColumns = false; grid.Columns.Clear(); int totalCols = 0; Type headType = headers.GetType(); for (int i = 0; i &lt; headers.Items.Count; i++) { grid.Columns.Add(GetCol(headers.GetString(i), String.Concat("[", i.ToString(),"]"))); totalCols++; } int finalWidth = totalCols * (int)grid.ColumnWidth.Value + 15; grid.Width = finalWidth; grid.ItemsSource = Rows; } private DataGridTextColumn GetCol(string header, string binding) { DataGridTextColumn col = new DataGridTextColumn(); col.IsReadOnly = true; col.Header = header; col.Binding = new Binding(binding); return col; } public DataGrid Create(int colSize) { DataGrid grid = new DataGrid(); grid.ColumnWidth = colSize; grid.CanUserAddRows = false; grid.AlternationCount = 2; BuildInto(grid); return grid; } } </code></pre> <p>Putting this together, this is a sample use:</p> <pre><code>void SimpleTest_Loaded(object sender, RoutedEventArgs e) { DataRow headers = new DataRow("Level", "Weapon Type", "vs None", "vs Leather", "vs Studded", "vs Brigandine"); GridConstructor gridConstructor = new GridConstructor(headers); var weaponType = "Slash"; for (int level = 1; level &lt; 10; level++) { int damage = DiceCup.RollMulti(8, level); int damCloth = damage - DiceCup.RollMulti(2, level); int damLeather = damage - DiceCup.RollMulti(3, level); int damStudded = damage - DiceCup.RollMulti(4, level); int damBrigandine = damage - DiceCup.RollMulti(5, level); DataRow row = new DataRow(level, weaponType, damage, damCloth, damLeather, damStudded, damBrigandine); gridConstructor.Rows.Add(row); } //Create the grid. var grid = gridConstructor.Create(100); //Create a chart. Chart chart = new Chart(); chart.Height = 200; chart.LegendTitle = "Legend"; chart.Title = "Slash vs Armor Types"; chart.DataContext = gridConstructor.Rows; //Create our series, or lines. LineSeries slashVsNone = new LineSeries(); slashVsNone.Title = "vs None"; slashVsNone.DependentValueBinding = new Binding("[2]"); slashVsNone.IndependentValueBinding = new Binding("[0]"); slashVsNone.ItemsSource = gridConstructor.Rows; chart.Series.Add(slashVsNone); //Presentation is a stackpanel on the page. presentation.Children.Add(grid); presentation.Children.Add(chart); } </code></pre> <p>And the output:</p> <p><a href="http://quiteabnormal.com/images/codeSample.jpg" rel="nofollow noreferrer" title="Output">alt text http://quiteabnormal.com/images/codeSample.jpg</a></p> <p>Please note that the grid coloring is from universal styles set on the page. If you use the GridConstructor.BuildInto() method you can specify a grid you've pre-formatted yourself in Blend or somesuch.</p> <p>Just one thing, the GridConstructor makes some assumptions about the column's initial settings. You can change the class to make it more customizable if you like, but this is what I needed so I wanted to be able to make it without fuss.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload