Dynamic WPF DataGrid
-
I need to automatically generate a WPF DataGrid at runtime. Some of the columns will be known, and some will be added based off user selections. In all cases, I'll know the data types. The column definitions will all be rows in some table, allowing me to create any number of columns. The problem that I'm going to run into is binding. Since there will be an unknown number of columns, I can't create an "concrete" entity to bind to. So, the real question here is how to represent the data when I don't know all the columns. I came across some examples of ExpandoObject usage and put together this sample:
static void Main(string[] args)
{
// Assigning properties and their values at runtime
var x = new ExpandoObject() as IDictionary;
x.Add("NewProp", "Hello World!");IDictionary propertyValues = x; foreach (var property in propertyValues.Keys) { Console.WriteLine(String.Format("{0} : {1}", property, propertyValues\[property\])); } // Creating properties in code and assigning data at runtime dynamic expando = new ExpandoObject(); expando.SomeStringProp = "C"; expando.SomeNumberProp = 12; expando.MyMethod = new Func(() => { return 55; }); Console.WriteLine(expando.SomeStringProp); Console.WriteLine(expando.SomeNumberProp); Console.WriteLine(expando.MyMethod()); Console.ReadLine();
}
I'm concerned about performance here. If anyone has a better way I'd like to hear about it. Thanks!
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I need to automatically generate a WPF DataGrid at runtime. Some of the columns will be known, and some will be added based off user selections. In all cases, I'll know the data types. The column definitions will all be rows in some table, allowing me to create any number of columns. The problem that I'm going to run into is binding. Since there will be an unknown number of columns, I can't create an "concrete" entity to bind to. So, the real question here is how to represent the data when I don't know all the columns. I came across some examples of ExpandoObject usage and put together this sample:
static void Main(string[] args)
{
// Assigning properties and their values at runtime
var x = new ExpandoObject() as IDictionary;
x.Add("NewProp", "Hello World!");IDictionary propertyValues = x; foreach (var property in propertyValues.Keys) { Console.WriteLine(String.Format("{0} : {1}", property, propertyValues\[property\])); } // Creating properties in code and assigning data at runtime dynamic expando = new ExpandoObject(); expando.SomeStringProp = "C"; expando.SomeNumberProp = 12; expando.MyMethod = new Func(() => { return 55; }); Console.WriteLine(expando.SomeStringProp); Console.WriteLine(expando.SomeNumberProp); Console.WriteLine(expando.MyMethod()); Console.ReadLine();
}
I'm concerned about performance here. If anyone has a better way I'd like to hear about it. Thanks!
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
How about loading the data into a
DataTable
and binding the grid to the table's.DefaultView
property?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
How about loading the data into a
DataTable
and binding the grid to the table's.DefaultView
property?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Ya, a table would be easier to build. I'll look into it. Thanks!
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
How about loading the data into a
DataTable
and binding the grid to the table's.DefaultView
property?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Ok, I'm stuck here. I can see rows - no data just the highlight - and no columns. What am i doing wrong?? XAML
ViewModel
private DataTable _BidBudgetData;
public DataTable BidBudgetData
{
get { return _BidBudgetData; }
set
{
if (_BidBudgetData != value)
{
_BidBudgetData = value;
RaisePropertyChanged("BidBudgetData");
}
}
}private void CreateBidBudgetTable()
{
var bidBudgetData = new DataTable();
bidBudgetData.Columns.Add("Plan Type", typeof(string));
bidBudgetData.Columns.Add("Living Sq Ft", typeof(decimal));foreach (var planType in ProjectPlanTypeSummaries.OrderBy(x =>x.Plan).ThenBy(x => x.Elevation)) { DataRow row = bidBudgetData.NewRow(); row\["Plan Type"\] = $"{planType.Plan}{planType.Elevation}"; row\["Living Sq Ft"\] = planType.LivingSqFt; bidBudgetData.Rows.Add(row); } BidBudgetData = bidBudgetData; RaisePropertyChanged("BidBudgetData");
}
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
Ok, I'm stuck here. I can see rows - no data just the highlight - and no columns. What am i doing wrong?? XAML
ViewModel
private DataTable _BidBudgetData;
public DataTable BidBudgetData
{
get { return _BidBudgetData; }
set
{
if (_BidBudgetData != value)
{
_BidBudgetData = value;
RaisePropertyChanged("BidBudgetData");
}
}
}private void CreateBidBudgetTable()
{
var bidBudgetData = new DataTable();
bidBudgetData.Columns.Add("Plan Type", typeof(string));
bidBudgetData.Columns.Add("Living Sq Ft", typeof(decimal));foreach (var planType in ProjectPlanTypeSummaries.OrderBy(x =>x.Plan).ThenBy(x => x.Elevation)) { DataRow row = bidBudgetData.NewRow(); row\["Plan Type"\] = $"{planType.Plan}{planType.Elevation}"; row\["Living Sq Ft"\] = planType.LivingSqFt; bidBudgetData.Rows.Add(row); } BidBudgetData = bidBudgetData; RaisePropertyChanged("BidBudgetData");
}
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
You've set
AutoGenerateColumns
tofalse
, but you haven't manually defined any columns in theDataGrid
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You've set
AutoGenerateColumns
tofalse
, but you haven't manually defined any columns in theDataGrid
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Yup. I'm an idiot. I spent hours trying to figure out why I'm not seeing anyting. Thanks!
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.