2

How do you create a numeric column (ideally integer) in an Eto forms GridView?

Below is my code for two string columns

stats.Columns.Add(new GridColumn
{
    DataCell = new TextBoxCell { Binding = Binding.Property<RulesDocAdapter, string>(r => r.Name) },
    HeaderText = "Name"
});

stats.Columns.Add(new GridColumn
{
     DataCell = new TextBoxCell { Binding = Binding.Property<RulesDocAdapter, string>(r => r.Abbreviation) },
     HeaderText = "Abbreviation"
});

1 Answers1

1

To use different columns, you can convert the integer property to a string using the Convert() extension on the property binding, like so:

Binding.Property((RulesDocAdapter r) => r.IntProperty).Convert(v => v.ToString(), s => { int i = 0; return int.TryParse(s, out i) ? i : -1; })
Curtis
  • 1,552
  • 15
  • 20
  • Thanks! The code is kind of obscure so I was having trouble working that magic out1 – Jeffrey Kesselman Nov 04 '16 at 16:00
  • This works for display, but update fails. I assume its a conversion exception thats getting causht. If I want the int property to be editable, what do I need to do? – Jeffrey Kesselman Nov 04 '16 at 16:07
  • Current Code: ` stats.Columns.Add(new GridColumn { DataCell = new TextBoxCell {Binding = Binding.Property((RulesDocAdapter r) => r.Cost).Convert(v => v.ToString()) }, HeaderText = "Cost" , Editable = true }); ` – Jeffrey Kesselman Nov 04 '16 at 16:17
  • Figured this out. Did string conversion in my adapter object. I dunno if thas canonically right but it works.. – Jeffrey Kesselman Nov 06 '16 at 16:30
  • @JeffreyKesselman yes you can do that (and it can sometimes make it easier), however you can use the 2nd parameter to the Convert() extension to define how it converts the string back to an int. I've updated the example to show two way binding there. Hope it helps! – Curtis Nov 08 '16 at 06:53
  • Oh cool! Thats a good trick to know whether I use it right now or not! Thanks! – Jeffrey Kesselman Nov 09 '16 at 00:17