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;
    }
  • 相关阅读:
    网络性能测试工具iperf详细使用图文教程zz
    linux时间和定时器zz
    sleep
    linux调度器的配置参数zz
    zz升级Mininet自带的OpenvSwitch & 编译OpenvSwitch
    minnet sample
    电信新势力,TIP/CORD能颠覆电信设备商吗?
    【TS】534- TypeScript 可辨识联合
    【拓展】一张图看懂字节跳动8年创业史,太励志了吧
    【CSS】533- CSS 渲染原理以及优化策略
  • 原文地址:https://www.cnblogs.com/springsnow/p/14113803.html
Copyright © 2011-2022 走看看