Search code examples
c#asp.netlinqgridviewlinqdatasource

LinqDataSource class example


Just starting to look into using LinqDataSource for a GridView and I’m looking for a few more examples on how to set up my data source.

I am hoping later on to use it against a SharePoint List but for now I’m simply after some detailed examples for a class that I can retrieve data from and update.

Here is an example class from MS. I just after more than one example so i have better idea how this work.

A Simple example that is known to work and is a little more complicated would be great

public class MovieLibrary
{
        string[] _availableGenres = { "Comedy", "Drama", "Romance" };

        public MovieLibrary()
        {
        }

        public string[] AvailableGenres
        {
            get
            {
                return _availableGenres;
            }
        }
    }

Cheers


Solution

  • I'm using LinqDataSource with SPGridView here is my code.

          <SharePoint:SPGridView runat="server" ID="spgvUserTrainingLists" AutoGenerateColumns="false"
                                    DataSourceID="linqDsEmployeeTrainingLists" DataKeyNames="RowId" OnRowDataBound="spgvUserTrainingLists_RowDataBound"
                                    AllowSorting="true" AllowPaging="true" PageSize="15" AllowFiltering="true" FilteredDataSourcePropertyName="Where"
                                    FilteredDataSourcePropertyFormat='{1} == "{0}"' FilterDataFields=",TrainingType,,Trainer,Status">
                                    <Columns>
                                        <SharePoint:SPBoundField HeaderText="Ref #" SortExpression="RefNo" DataField="RefNo" />
                                        <SharePoint:SPBoundField HeaderText="Type" SortExpression="TrainingType" DataField="TrainingType" />
                                        <asp:TemplateField HeaderText="Training" SortExpression="TrainingTitle">
                                            <ItemTemplate>
                                                <asp:HyperLink ID="hlTrainingDetail" runat="server" Text='<%# Eval("TrainingTitle") %>'
                                                    NavigateUrl="#" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <SharePoint:SPBoundField HeaderText="Trainer" SortExpression="Trainer" DataField="Trainer" />
                                        <SharePoint:SPBoundField HeaderText="Status" SortExpression="Status" DataField="Status" />
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:ImageButton ID="imgDelete" ImageUrl="~/_layouts/images/DELITEM.GIF" runat="server"
                                                    UseSubmitBehaviour="false" CommandValue='<%# Eval("RowId") %>' OnClientClick="javascript:DeleteTraining(this);" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                    <EmptyDataTemplate>
                                        No training yet.
                                    </EmptyDataTemplate>
                                </SharePoint:SPGridView>
                                <SharePoint:SPGridViewPager ID="SPGridViewPager1" runat="server" GridViewId="spgvUserTrainingLists" />
                                <aspweb:LinqDataSource runat="server" ID="linqDsEmployeeTrainingLists" OnSelecting="linqDs_Selecting" />
    

    In the LinqDataSource Selecting event is where the data binding happened

    protected void linqDs_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            object parameter = null;
    
            if (e.SelectParameters.TryGetValue("employee", out parameter))
            {
                e.Result = DefaultBLL.GetEmployeeTrainingLists(parameter.ToString());
            }
        }
    
    
    Hope this help.