【发布时间】: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