简体   繁体   English

如何将复杂对象绑定到Telerik的RadGrid

[英]How to bind complex objects to Telerik's RadGrid

I have to create a table to monitor a list of streams. 我必须创建一个表来监视流列表。 The table, as you can see from the attached, must have the following features: 从附件中可以看到,该表必须具有以下功能:

  • The stream description (or name) to be monitored 要监视的流描述(或名称)
  • The type (input, output or both) 类型(输入,输出或两者)
  • The results of a particular day ( and here is the problem ) 某一天的结果( 这就是问题所在

This is the desidered result: 这是令人讨厌的结果: 在此处输入图片说明

The data streams are provided by Web Service, via JSON. 数据流由Web服务通过JSON提供。

I planned on doing everything with the RadGrid, but I have some difficulties in its implementation. 我计划使用RadGrid进行所有操作,但是在实现过程中遇到了一些困难。

This is the model to pass: 这是要传递的模型

[DataContractFormat]
public class StreamOutputDto : BaseDto
{
    public string Name { get; set; }
    public string Type { get; set; }
    public List<DetailStream> Details { get; set; }
}

Where " DetailStream " is: 其中“ DetailStream ”是:

public class DetailStream
{
    public string Id { get; set; }
    public DateTime Date { get; set; }
    public int CountInfo { get; set; }
    public StateStream StateInput { get; set; }
    public StateStream StateOutput { get; set; }
    //...
}

public enum StateStream
{
    InProgress,
    Received,
    Declined,
    Inexistent
}

So DetailStream is the result of a specific stream in a specific day. 因此, DetailStream是在特定日期的特定流的结果。 Details is a list of DetailStream, ie the results of a specific stream in a specific week. 详细信息是DetailStream的列表,即特定周中特定流的结果。

With Name and Type there are no problems, but I do not know how to manage the list of DetailStream. 使用Name和Type没有问题,但是我不知道如何管理DetailStream的列表。 Someone can help me? 有人可以帮我吗?

This is my current implementation: 这是我当前的实现:

My Web Service : 我的网络服务

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        ResponseFormat = WebMessageFormat.Json)]
    StandardResponse<StreamOutputDto> GetStream(string request);
}


[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[MessageLoggingBehavior]
public class MyService : IMyService
{

    public StandardResponse<StreamOutputDto> GetStream(string request)
    {
        // TEST CASE:
        StandardResponse<StreamOutputDto> response = new StandardResponse<StreamOutputDto>();
        response.Output = new StreamOutputDto();
        response.Output.Name = "Hi!";
        response.Output.Type = "Input";
        response.Output.Details = new List<DetailStream>();

        response.Output.Details.Add(new DetailStream(){
                Id = "1",
                CountInfo = 100,
                Date = DateTime.Today });

            response.Output.Details.Add(new DetailStream(){
                Id = "2",
                CountInfo = 200 });

        return response;
    }

}

My WebForm.aspx : 我的WebForm.aspx

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="RadGridParser.js"></script>
</head>

<body>

    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true"/>

    <telerik:RadGrid ID="RadGrid1" RenderMode="Lightweight" ClientDataSourceID="RadClientDataSource1" 
             AllowPaging="false" AllowSorting="false" AllowFilteringByColumn="false" PageSize="5" runat="server">

        <MasterTableView DataKeyNames="Name" ClientDataKeyNames="Name">
            <Columns>
                <telerik:GridBoundColumn DataField="Name" HeaderText="" DataType="System.String" >
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Type" HeaderText="Tipologia flusso" DataType="System.String">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Day1" HeaderText="Lunedì">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Day2" HeaderText="Martedì">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Day3" HeaderText="Mercoledì">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Day4" HeaderText="Giovedì">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Day5" HeaderText="Venerdì">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>

    </telerik:RadGrid>

    <telerik:RadClientDataSource ID="RadClientDataSource1" runat="server" AllowBatchOperations="true">
        <ClientEvents OnCustomParameter="ParameterMap" OnDataParse="Parse" />
        <DataSource>
            <WebServiceDataSourceSettings>
                <Select Url="http://soldev/Axa.Sol.Web/ws/Ivass/IvassService.svc/GetStream" DataType="JSON" RequestType="Post" />
            </WebServiceDataSourceSettings>
        </DataSource>
        <Schema ResponseType="JSON">
            <Model ID="StreamModel">
                <telerik:ClientDataSourceModelField FieldName="Name" DataType="String" />
                <telerik:ClientDataSourceModelField FieldName="Type" DataType="String" />
                <telerik:ClientDataSourceModelField FieldName="Day1" DataType="String" />
                <telerik:ClientDataSourceModelField FieldName="Day2" DataType="String" />
                <telerik:ClientDataSourceModelField FieldName="Day3" DataType="String" />
                <telerik:ClientDataSourceModelField FieldName="Day4" DataType="String" />
                <telerik:ClientDataSourceModelField FieldName="Day5" DataType="String" />
            </Model>
        </Schema>
    </telerik:RadClientDataSource>

</body>

My RadGridParser.js : 我的RadGridParser.js

//<![CDATA[
function ParameterMap(sender, args) {
    //If you want to send a parameter to the select call you can modify the if 
    //statement to check whether the request type is 'read':
    //if (args.get_type() == "read" && args.get_data()) {
    if (args.get_type() != "read" && args.get_data()) {
        args.set_parameterFormat({ request: kendo.stringify(args.get_data().models) });
    }
}

function Parse(sender, args) {
    var response = args.get_response();
    if (response) {
        args.set_parsedData(response.Output);
    }
}

function UserAction(sender, args) {
    if (sender.get_batchEditingManager().hasChanges(sender.get_masterTableView()) &&
                !confirm("Any changes will be cleared. Are you sure you want to perform this action?")) {
        args.set_cancel(true);
    }
}

//]]>

RadGrid can't handle this out of the box. RadGrid无法开箱即用。 The only thing you can do I think is to use a TemplateColumn. 我认为您唯一可以做的就是使用TemplateColumn。 The template column has a "ItemTemplate", "InsertTemplate", and an "EditTemplate". 模板列具有“ ItemTemplate”,“ InsertTemplate”和“ EditTemplate”。

You can put any controls and html elements inside this container. 您可以将任何控件和html元素放入此容器中。 Also you can access this controls (and your data item) inside the "ItemDataBound" event of the grid. 您也可以在网格的“ ItemDataBound”事件内访问此控件(和您的数据项)。 This should help you to achive your scenario. 这应该可以帮助您实现方案。

References: Template Columns 参考: 模板列

Item-DataBound Event: Item-DataBound事件:

protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
  GridDataItem gridItem = e.Item as GridDataItem;
  dynamic dataItem = (YourType)(gridItem.DataItem);
  gridItem.ToolTip = dataItem.ID + " - " + dataItem.UUID;
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM