0

I am querying a SPList in sharepoint 2010 and populating the corresponding grid view:

<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>

gridView.DataSource = items.GetDataTable();

If i use this line of code it would imply that when using this along with a grid view it would allow me to view all items found in the SPlist . But I need to be able to show only specific data from the List, for this reason i am using an SPQuery to filter the list with the following code :

                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();

yet upon execution it raises the following issue "A field or property with the name 'Surname' was not found on the selected data source". Can someone highlight what is wrong with my code? or am i missing something out? since when I use "getDataTable()" it works fine but on the other hand upon filtering the list the listed error is raised.

1 Answer 1

0

Try the following:

SPListItemCollection objCollection =list.GetItems(oQuery);
gridView.DataSource = objCollection.GetDataTable(); 
gridView.DataBind();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.