zoukankan      html  css  js  c++  java
  • DevExpress、XtraGrid绑定到JSON

    Bind to JSON Data | WinForms Controls | DevExpress Documentation

    JsonDataSource Class | Cross-Platform Class Library | DevExpress Documentation

    要绑定到Code中的JSON格式的数据,请创建一个新的DevExpress.DataAccess.Json.JsonDataSource对象。

    private void Form1_Load(object sender, EventArgs e)
    {
        gridControl1.DataMember = "Customers";
        gridControl1.DataSource = CreateDataSourceFromWeb();
    }
    
    private JsonDataSource CreateDataSourceFromWeb()
    {
        var jsonDataSource = new JsonDataSource();
        //Specify the data source location 
        jsonDataSource.JsonSource = new UriJsonSource(new Uri("http://northwind.servicestack.net/customers.json"));
    
        jsonDataSource.Fill();
        //jsonDataSource.FillAsync();
        return jsonDataSource;
    }

    如果您需要排除特定的数据字段,请手动构建JSON Schema。

    private JsonDataSource CreateDataSourceFromWeb()
    {
        var jsonDataSource = new JsonDataSource();
        UriJsonSource uriJsonSource = new UriJsonSource();
        jsonDataSource.JsonSource = new UriJsonSource(new Uri("http://northwind.servicestack.net/customers.json"));
    
        JsonSchemaNode root = new JsonSchemaNode("root", null);
        JsonSchemaNode nodeCustomers = new JsonSchemaNode("Customers", true, JsonNodeType.Array);
        JsonSchemaNode nodeCompanyName = new JsonSchemaNode("CompanyName", true, JsonNodeType.Property, typeof(string));
        JsonSchemaNode nodeContactName = new JsonSchemaNode("ContactName", true, JsonNodeType.Property, typeof(string));
        JsonSchemaNode nodeContactTitle = new JsonSchemaNode("ContactTitle", true, JsonNodeType.Property, typeof(string));
        JsonSchemaNode nodeAddress = new JsonSchemaNode("Address", true, JsonNodeType.Property, typeof(string));
        JsonSchemaNode nodeCity = new JsonSchemaNode("City", true, JsonNodeType.Property, typeof(string));
    
        nodeCustomers.Nodes.AddRange(new DevExpress.DataAccess.Node<JsonNode>[] {
            nodeCompanyName,
            nodeContactName,
            nodeContactTitle,
            nodeAddress,
            nodeCity,
        });
        root.Nodes.Add(nodeCustomers);
        jsonDataSource.Schema = root;
    
        jsonDataSource.Fill();
        //jsonDataSource.FillAsync();
        return jsonDataSource;
    }

    下面的代码示例说明了如何从Web检索JSON数据。

    using DevExpress.DataAccess.Json;
    // ...
    public static JsonDataSource CreateDataSourceFromWeb() {
        var jsonDataSource = new JsonDataSource();
        // Specify the endpoint.
        jsonDataSource.JsonSource = new UriJsonSource(new Uri("http://northwind.servicestack.net/customers.json"));
        // Populate the data source with data.
        jsonDataSource.Fill();
        return jsonDataSource;
    }
    下面的代码示例说明了如何使用文件中的JSON数据。
    using DevExpress.DataAccess.Json;
    // ...
    public static JsonDataSource CreateDataSourceFromFile() {
        var jsonDataSource = new JsonDataSource();
        // Specify the JSON file name.
        Uri fileUri = new Uri("customers.json", UriKind.RelativeOrAbsolute);
        jsonDataSource.JsonSource = new UriJsonSource(fileUri);
        // Populate the data source with data.
        jsonDataSource.Fill();
        return jsonDataSource;
    }
    下面的代码示例说明了如何使用字符串变量中的JSON数据。
    using DevExpress.DataAccess.Json;
    // ...
    public static JsonDataSource CreateDataSourceFromText() {
        var jsonDataSource = new JsonDataSource();
    
        // Specify a string with JSON data.
        string json = "{"Customers":[{"Id":"ALFKI","CompanyName":"Alfreds Futterkiste","ContactName":"Maria Anders","ContactTitle":"Sales Representative","Address":"Obere Str. 57","City":"Berlin","PostalCode":"12209","Country":"Germany","Phone":"030-0074321","Fax":"030-0076545"}],"ResponseStatus":{}}";
    
        // Specify the object that retrieves JSON data.
        jsonDataSource.JsonSource = new CustomJsonSource(json);
        // Populate the data source with data.
        jsonDataSource.Fill();
        return jsonDataSource;
    }
  • 相关阅读:
    优化你的手机游戏
    vuforia 结合 unity3d 开发 AR 的 androidAPP 总结
    self._raiseerror(v) File "D:GameDevelopmentPython27libxmletreeElementTree.py", line 1506, in _raiseerror
    自定义TexturePacker插件导出自己的plist文件
    还原TexturePacker plist 文件以及图片的方法 (切开各小图片)
    no module named firefly.master.master
    c#比较器 排序
    python ——面向对象进阶(反射,双下线方法,静态方法,类方法)
    python——模块和包 需要注意的地方
    举例详解Python中的split()函数的使用方法
  • 原文地址:https://www.cnblogs.com/springsnow/p/14113803.html
Copyright © 2011-2022 走看看