在ASP.NET中将Outlook日历项目获取到网格视图

时间:2019-01-02 16:36:02

标签: c# asp.net gridview datatable outlook

我正在尝试使用Microsoft.Office.Interop.Outlook将Outlook日历项目获取到ASP.NET中的gridview表。将数据存储到列表框没有问题,但是我需要使用gridview表格式。

运行代码时我没有收到任何错误,只是没有显示任何数据。

        public void GetAllCalendarItems()
    {
        DataTable calendardata = new DataTable();
        calendardata.Columns.Add("Subject", typeof(string));
        calendardata.Columns.Add("StartDate", typeof(DateTime));

        Microsoft.Office.Interop.Outlook.Application oApp = null;
        Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null;
        Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder = null;
        Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = null;

        oApp = new Microsoft.Office.Interop.Outlook.Application();
        mapiNamespace = oApp.GetNamespace("MAPI");
        Recipient outlookAccount = oApp.Session.CreateRecipient("outofoffice");
        CalendarFolder = mapiNamespace.GetSharedDefaultFolder(outlookAccount, OlDefaultFolders.olFolderCalendar);
        outlookCalendarItems = CalendarFolder.Items;
        outlookCalendarItems.IncludeRecurrences = true;

        foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems)
        {

                DataRow row = calendardata.NewRow();
                row["Subject"] = item.Subject;
                row["StartDate"] = item.Start.Date;

                foreach (DataRow dr in calendardata.Rows)
                {
                    tblCalendar.DataSource = calendardata;
                    tblCalendar.DataBind();
                }
        }
    }

这是asp.net代码:

        <asp:GridView ID="tblCalendar" runat="server" AutoGenerateColumns="false" CssClass="table table-bordered table-striped" Visible="true">
        <RowStyle CssClass="myrow" />
        <AlternatingRowStyle CssClass="myrow" />
        <Columns>
            <asp:BoundField DataField="Subject" HeaderText="Subject" />
            <asp:BoundField DataField="StartDate" HeaderText="Start Date" />

        </Columns>
    </asp:GridView>

2 个答案:

答案 0 :(得分:0)

代码应如下所示:

foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems)
{
    DataRow row = calendardata.NewRow();
    row["Subject"] = item.Subject;
    row["StartDate"] = item.Start.Date;
}
tblCalendar.DataSource = calendardata;

答案 1 :(得分:0)

这解决了我的问题:

dt.Rows.Add(new Object[] { item.Subject, item.Start.Date }); 
tblCalendar.DataBind();`