【发布时间】:2017-06-22 19:16:15
【问题描述】:
我有一个返回对象数组的端点。我想通过将对象的键设为列并使用每个对象对应的值填充行来填充 DevExpress GridControl。问题是gridcontrol只有在我明确定义json对象的键是这样的时候才会填充:
foreach(dynamic item in json){
report.Add(new
{
AccountID = item["AccountID"],
AccountTypeID = item["AccountTypeID"],
PrefixID = item["PrefixID"],
SuffixID = item["SuffixID"],
GenderID = item["GenderID"],
PrimaryContactID = item["PrimaryContactID"]
});
}
我不想对此进行硬编码,因为从我的端点返回的数组返回了不同的对象结构。 这是我到目前为止所拥有的,它获取我的动态对象的属性并将它们设为列名。它可以创建列名,但行都是空的。
public GridView()
{
InitializeComponent();
DevExpress.Mobile.Forms.Init();
PopulateGridView();
}
public async void PopulateGridView()
{
dynamic json = await model.GetItemAsync();
var report = new List<object>();
var custom = new Dictionary<string, string>();
foreach(dynamic item in json){
report.Add(item);
}
grid.ItemsSource = report;
foreach (JProperty x in (JToken)json[0])
{
TextColumn current = new TextColumn();
current.FieldName = x.Name;
grid.Columns.Add(current);
}
}
这就是它的样子
【问题讨论】:
-
网格是否有 Bind 或 DataBind 方法..?或者在填充列后尝试移动这个
grid.ItemsSource = report;.. -
@MethodMan 该死,我移动了 grid.ItemsSource,它仍然产生相同的结果
标签: c# xamarin xamarin.forms devexpress