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.
Try the following:
SPListItemCollection objCollection =list.GetItems(oQuery);
gridView.DataSource = objCollection.GetDataTable();
gridView.DataBind();