我正在开发一个Web应用程序(.net framework 4.0),我的应用程序角色是在Active Directory中搜索并在SearchResultCollection中返回结果。 我的问题是如何将我的GridView作为数据源提供给SearchResultCollection?
答案 0 :(得分:0)
首先检查SearchResultsCollection是否继承自IEnumerable:
确实如此,现在您可以使用以下方法(取自http://www.telerik.com/help/aspnet-ajax/grid-data-binding-basics.html):
RadGrid控件的高级数据绑定的关键是处理NeedDataSource事件。每次需要绑定到数据源时,RadGrid都会触发NeedDataSource事件。如果在事件发生时,DataSource属性未设置为有效的数据源对象,则网格将无法正常运行。
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
' Ensure that SearchResultsCollection is accessible in your code-behind class or <script runat="server"> code block
RadGrid1.DataSource = SearchResultsCollection
End Sub
当您不需要网格执行复杂操作时,可以在简单情况下使用简单数据绑定,例如:通过自定义编辑表单(WebUserControl或FormTemplate)插入,删除和更新记录,分组,层次结构关系,过滤,排序,分页
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RadGrid1.DataSource = SearchResultsCollection
End Sub
编辑:
操纵结果以获取所需信息:
Private Sub rgItems_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgItems.ItemDataBound
Dim result As SearchResult = TryCast(e.Item.DataItem, SearchResult)
If searchResult IsNot Nothing Then
' Get info you need here, perhaps play with result.GetDirectoryEntry()
' Assign this data to variables, name, department , etc
' Use e.Item.FindControl("ltlDepartment").Text = department
End If
End Sub
确保定义这些控件中的每一个:
<telerik:RadGrid runat="server">
<telerik:GridTemplateColumn HeaderText="Department" UniqueName="Department">
<ItemTemplate>
<asp:Literal id="ltlDepartment" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</telerik:radGrid>