CRM 2011 fetchxml查询数据并绑定到GridView

    //最近一周完成的任务
    protected void loadCompTaskInfo(IOrganizationService server)
    {
        string untaskXml = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                            <entity name='task'>
                                            <attribute name='activityid' />
                                            <attribute name='subject' />
                                            <attribute name='new_taskresult' />
                                            <attribute name='new_prioritylevel' />
                                            <attribute name='regardingobjectid' />
                                            <attribute name='scheduledstart' />
                                            <attribute name='scheduledend' />
                                            <attribute name='actualend' />
                                            <attribute name='ownerid' />
                                            <order attribute='createdon' descending='true' />
                                            <filter type='and'>
                                                <condition attribute='statecode' operator='eq' value='1' />
                                                <condition attribute='actualend' operator='last-seven-days' />
                                            </filter>
                                            <link-entity name='systemuser' from='systemuserid' to='owninguser' visible='false' link-type='outer' alias='users'>
                                                <attribute name='businessunitid' />
                                            </link-entity>
                                            </entity>
                                        </fetch>");
        FetchExpression fetchUntask = new FetchExpression(untaskXml);
        EntityCollection untaskRetrieved = server.RetrieveMultiple(fetchUntask);

        DataTable dtUntask = new DataTable();
        DataColumn dc = null;
        dc = dtUntask.Columns.Add("activityid", Type.GetType("System.String"));
        dc = dtUntask.Columns.Add("subject", Type.GetType("System.String"));
        dc = dtUntask.Columns.Add("taskresult", Type.GetType("System.String"));
        dc = dtUntask.Columns.Add("prioritylevel", Type.GetType("System.String"));
        dc = dtUntask.Columns.Add("regardingobjectidname", Type.GetType("System.String"));
        dc = dtUntask.Columns.Add("scheduledstart", Type.GetType("System.String"));
        dc = dtUntask.Columns.Add("scheduledend", Type.GetType("System.String"));
        dc = dtUntask.Columns.Add("actualend", Type.GetType("System.String"));
        dc = dtUntask.Columns.Add("owneridname", Type.GetType("System.String"));

        DataRow newRow;
        foreach (Entity untask in untaskRetrieved.Entities)
        {
            newRow = dtUntask.NewRow();
            newRow["activityid"] = (untask.Contains("activityid")) ? untask.Attributes["activityid"].ToString() : "";
            newRow["subject"] = (untask.Contains("subject")) ? untask.Attributes["subject"].ToString() : "";
            newRow["taskresult"] = (untask.Contains("new_taskresult")) ? untask.Attributes["new_taskresult"].ToString() : "";
            if (((OptionSetValue)(untask["new_prioritylevel"])).Value.ToString() == "100000000")
            {
                newRow["prioritylevel"] = "正常";
            }
            else if (((OptionSetValue)(untask["new_prioritylevel"])).Value.ToString() == "100000005")
            {
                newRow["prioritylevel"] = "重要";
            }
            else
            {
                newRow["prioritylevel"] = "紧急";
            }
            newRow["regardingobjectidname"] = untask.Attributes.Contains("regardingobjectid") ? ((EntityReference)(untask.Attributes["regardingobjectid"])).Name : "";
            newRow["scheduledstart"] = (untask.Contains("scheduledstart")) ? ((DateTime)(untask["scheduledstart"])).AddHours(8).ToString() : "";
            newRow["scheduledend"] = (untask.Contains("scheduledend")) ? ((DateTime)(untask["scheduledend"])).AddHours(8).ToString() : "";
            newRow["actualend"] = (untask.Contains("actualend")) ? ((DateTime)(untask["actualend"])).AddHours(8).ToString() : "";
            newRow["owneridname"] = untask.Attributes.Contains("ownerid") ? ((EntityReference)(untask.Attributes["ownerid"])).Name : "";

            dtUntask.Rows.Add(newRow);
        }


        this.comptaskData.DataSource = dtUntask;
        this.comptaskData.DataBind();

    }

页面:

   <asp:GridView ID="comptaskData" runat="server"  Height="6px" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" OnPageIndexChanging="comptaskData_PageIndexChanging" OnRowDataBound="comptask_RowDataBound" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Width="100%">
                            <FooterStyle BackColor="White" ForeColor="#000066" />
                            <RowStyle Wrap="false" Font-Size="9pt" ForeColor="#000066" />
                            <Columns>
                                <asp:TemplateField HeaderText="主题">
                                    <ItemTemplate>
                                        <span>
                                            <asp:Label ID="lblcomptaskid" runat="server" Text='<%# Eval("subject") %>'></asp:Label>
                                            <asp:HiddenField ID="comptaskid" runat="server" Value='<%# Eval("activityid") %>' />
                                        </span>
                                    </ItemTemplate>
                                    <ItemStyle Width="17%" />
                                </asp:TemplateField>
                                <asp:BoundField HeaderText="执行情况" DataField="taskresult" ItemStyle-Width="21%" />
                                <asp:BoundField HeaderText="任务相关" DataField="regardingobjectidname" ItemStyle-Width="17%" />
                                <asp:BoundField HeaderText="优先级" DataField="prioritylevel" ItemStyle-Width="7%" />
                                <asp:BoundField HeaderText="开始日期" DataField="scheduledstart" ItemStyle-Width="10%" />
                                <asp:BoundField HeaderText="截止日期" DataField="scheduledend" ItemStyle-Width="10%" />
                                <asp:BoundField HeaderText="完成日期" DataField="actualend" ItemStyle-Width="10%" />
                                <asp:BoundField HeaderText="负责人" DataField="owneridname" ItemStyle-Width="7%" />
                            </Columns>
                            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" Wrap="false" />
                            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" Wrap="false" Font-Size="9pt" CssClass="DataGridFixedHeader" />
                            <SortedAscendingCellStyle BackColor="#F1F1F1" />
                            <SortedAscendingHeaderStyle BackColor="#007DBB" />
                            <SortedDescendingCellStyle BackColor="#CAC9C9" />
                            <SortedDescendingHeaderStyle BackColor="#00547E" />
                        </asp:GridView>

 

 

posted @ 2013-06-28 17:06  流浪De云  阅读(457)  评论(0编辑  收藏  举报