Domanda

Sto interrogando uno splist in SharePoint 2010 e popolando la corrispondente vista della griglia:

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

Se utilizzo questa riga di codice, implicherebbe che quando si utilizza questo insieme a una Grid View, mi consentirebbe di visualizzare tutti gli elementi trovati nella split.Ma devo essere in grado di mostrare solo dati specifici dall'elenco, per questo motivo sto usando una SpQuery per filtrare l'elenco con il seguente codice:

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

Ancora al momento dell'esecuzione solleva il seguente problema "Un campo o proprietà con il nome 'cognome' non è stato trovato sull'origine dati selezionata".Qualcuno può evidenziare ciò che è sbagliato nel mio codice?o mi manca qualcosa?Dal momento che quando uso "GetDatagable ()" Funziona bene, ma d'altra parte dopo aver filtrato l'elenco viene sollevato l'errore elencato.

AGGIORNAMENTO


.

Aggiornato i campi di vista come indicato ma lo stesso errore persiste ancora Codice corrente:

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

È stato utile?

Soluzione

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.

Altri suggerimenti

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top