我正在查询SharePoint 2010中的夹子,并填充相应的网格视图:

<asp:DataGrid runat="server" ID="gridView" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="ActiveBorder" Width="95%" GridLines="Vertical" AllowPaging="true" PageSize="30" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" >
    <Columns>
<asp:BoundColumn DataField="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="Title"></asp:BoundColumn>
<asp:HyperLinkColumn DataTextField="Name" ></asp:HyperLinkColumn>
<asp:BoundColumn DataField="Surname"></asp:BoundColumn>
<asp:BoundColumn DataField="email"></asp:BoundColumn>
</columns>
</gridview>
.

如果我使用这行代码,它将意味着在使用此操作时,它会允许我查看夹子中找到的所有项目。但我需要只能从列表中显示特定数据,因为这个原因我正在使用spquery来过滤包含以下代码的列表:

            Guid webId = web.ID;
            SPListItemCollection items = list.Items;

            SPList currentTestList = web.Lists[listGuid];

            // query 
            SPQuery oQuery = new SPQuery();
            oQuery.ExpandRecurrence = true;

            oQuery.Query = @"
                <Where>
                    <Eq>
                        <FieldRef Name='Name' />
                        <Value Type='Text'>Adrian</Value>
                    </Eq>
                </Where>
                ";
            gridView.DataSource = list.GetItems(oQuery);
            gridView.DataBind();
.

仍然在执行时,它会引发以下问题“在所选数据源上找不到”姓氏“名称的字段或属性”。有人可以突出我的代码有什么问题吗?还是我错过了一些东西?从使用时,它使用“getDataTable()”它工作正常,但另一方面在过滤列表时,提出列出的错误。

更新


按照指示更新了视野,但同样的错误仍然存在 当前代码:

SPQuery oQuery = new SPQuery();
                oQuery.ExpandRecurrence = true;

                oQuery.Query = @"
                    <Where>
                        <Eq>
                            <FieldRef Name='Name' />
                            <Value Type='Text'>Adrian</Value>
                        </Eq>
                    </Where>
                    ";

                oQuery.ViewFields = @"<FieldRef Name='Name' /><FieldRef Name='Surname' /><FieldRef Name='email' /><FieldRef Name='ID' /><FieldRef Name='Title' />";
                oQuery.ViewFieldsOnly = true;
                //binding it to grid view

                SPListItemCollection filteredList = list.GetItems(oQuery);
                gridView.DataSource = filteredList;
                //gridView.DataSource = list.GetItems(oQuery);
                gridView.DataBind();
.

有帮助吗?

解决方案

Try using SPListItemCollection.GetDataTable like this:

var dt = list.GetItems(oQuery).GetDataTable();
gridView.DataSource = dt;

When you just use list.GetItems(oQuery) as the DataSource what the GridView gets is a SPListItemCollection which it doesn't understand, but it's an IEnumerable which it understands. So now the GridView can enumerate the datasource and get its items back, these are of type SPListItem. The GridView then tries to get each column value as either a field or property of the item it processes. The is OK for ID, Title and Name as these all exists as properties on SPListItem, but there is no property SPListItem.SurName you have to use SPListItem["SurName"] but the GridView doesn't know that.

When you use list.GetItems(oQuery).GetDataTable() as the DataSource what the GridView gets is a DataTable and the GridView knows how to access the Rows and Columns of that.

其他提示

Are you certain that 'Surname' is the proper internal name of the field? The internal name is what needs to be used during queries.

To quickly find out, you can easily inspect the list using powershell:

$url = "http://www.yoursharepointsite.com/mysite"
$listname = "YourListName"

$site= SPSite ($url) 
$web=$site.OpenWeb() 
$list=$web.Lists[$listname] 
$list.Fields |select ID, title, internalname
许可以下: CC-BY-SA归因
scroll top