【问题标题】:Getting Outlook Calendar Items to Grid view in ASP.NET在 ASP.NET 中将 Outlook 日历项目添加到网格视图
【发布时间】:2019-05-29 07:30:41
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

  • 日历可能没有重新绘制。尝试将 DataSource 设置为 null : tblCalendar.DataSource = null;tblCalendar.DataSource = dt;
  • 还是不行。
  • DataTable dt 是否包含数据?数据入表后设置断点。然后将鼠标悬停在 dt 变量上,单击向下箭头,然后选择 Data Table Visualizer。
  • 不,不是。 row["Subject"] = item.Subject 中有数据- 但不在 DataRow 博士中。我不确定如何在那里获取数据。
  • 您已经使用 DataRow row = calendardata.NewRow(); 将行放入数据表中;所以你只需要一个 foreach 循环。然后在 foreach 之后你只需要一个语句: tblCalendar.DataSource = calendardata;

标签: c# asp.net gridview datatable outlook


【解决方案1】:

这解决了我的问题:

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

【讨论】:

  • 我认为第一行没有任何作用。除非您的意思是表 tblCalendar,否则代码中没有表 dt。
【解决方案2】:

代码应如下所示:

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;

【讨论】:

  • 我能够通过使用以下方法使其工作:dt.Rows.Add(new Object[] { item.Subject, item.Start.Date });然后 - tblCalendar.DataBind();感谢您的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 2014-05-03
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多