Solved

How to connect SharePoint 2010 list to Telerik RadGrid

Posted on 2012-12-20
4
2,018 Views
Last Modified: 2013-02-19
I want to populate a RadGrid with SharePoint 2010 list data. I cannot figure out how this is accomplished.

Here is my ASPX page:
<SPWebControls:SPDataSource runat="server" ID="dsclientmatter" DataSourceMode="ListItem" UseInternalName="true">
  <SelectParameters>
      <asp:Parameter Name="WebUrl" DefaultValue="/configuration/" />
      <asp:Parameter Name="ListID" DefaultValue="4056449D-1884-4C79-AAAD-CD0ABEB2F854" />
      <asp:Parameter Name="ListItemID" DefaultValue="1" />
  </SelectParameters>
</SPWebControls:SPDataSource>
 
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" ShowStatusBar="true" PageSize="7" AllowPaging="True" DataSourceID="dsclientmatter" >
    <ClientSettings>
        <Selecting AllowRowSelect="True" />
    </ClientSettings>
<MasterTableView datasourceid="">
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
 
<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
 
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
</MasterTableView>
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
</telerik:RadGrid>

Open in new window


Here is my code-behind:
protected void Page_Load(object sender, EventArgs e)
        {
            SPSite site = new SPSite("http://abcdev");
             
            using (SPWeb web = site.OpenWeb())
            {
                SPList clientlist = web.Lists.TryGetList("clientmatter");
                DataTable dt = new DataTable();
                SPQuery query = new SPQuery();
                query.ViewFields = "<Where><Eq><FieldRef Name='clientid'/><Value Type='Text'>12345</Value></Eq></Where>";
                SPListItemCollection items = clientlist.GetItems(query);
                if(items != null)
                {
                    if(items.Count > 0)
                    {
                        dt = items.GetDataTable();                       
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                         
                    }
                }
            }
        }

Open in new window



I don't know if this is the best way to start, but I do know something is wrong because I cant render any data. Plus, I don't understand why by RadGrid ID will not compile. I get an error stating "The name 'GridView1' does not exist in the current context". Why. That is the ID of my RadGrid.

My end goal is to create a RadGrid that I can filter by individual columns, such as: name, client#, topic, etc. I am not sure if the filtering allows for multiple entries, but I would like to be able to filter on more than one criteria. Also, wildcard searches are very important.

Thank you for the help!
0
Comment
Question by:vpit
  • 3
4 Comments
 
LVL 9

Assisted Solution

by:kukdai
kukdai earned 500 total points
ID: 38714835
First of all, create the columns that you want to display in the rad grid in the .aspx file.

in your code behind
write a function that loads the data into the rad grid.

create a data table and create the columns that are needed to store the values.

create a function that returns the data table.

inside that data table get the data from the list and assign each data to the columns that are created for the data table and return the data table.
now in the page load event
if it is not post back then load the function where you will assign the data source of the current rad grid to function that return the data table.

there is one function called needdatasource event in the rad grid.
if you click on the rad grid in the aspx file and check the event properties of the rad grid you can see needdatasource double click on that and write the code that will bind the data source with the data table.

i don't want to write the code here unless you need it there are bunch of examples about this i hope. you can also search in telerik forums they are of great help.
0
 

Author Comment

by:vpit
ID: 38724730
Telerik support replied via email and I wanted to post it here:

RadGrid, by itself, does not support binding to SPLists directly, neither can it handle an SPDataSource control.

However, there is a solution that you can employ in your project. Our controls for SharePoint go with an extension of RadGrid, called SPRadGrid. It is a standalone class that offers a built-in support for binding to SharePoint lists. What you need to do is to download the installer for the Telerik web parts for SharePoint 2010.

Go to Manager your producets -> Download Installer and Other Resources -> (Under RadControls for ASP.NET AJAX) Browser all product lines -> SharePoint 2007/2010 -> SharePoint 2010 Web Parts - Manual Installation. Within the zip file there is a wsp - make that with a .rar/.zip extension and extract the Telerik.Ajax.SharePoint.dll. In the dll, you will find the SPRadGrid class. Alternatively, you can download and run the installer of our extensions for SharePoint 2010 - it will install the web parts (you can use them or not as per your scenario) and deploy the dll to the GAC where you can safely reference it from.

I will look into this Telerik solution and the one offered by kukdai and post a reply soon.
0
 

Accepted Solution

by:
vpit earned 0 total points
ID: 38890788
I found an answer how to connect to an external list posted by http://blah.winsmarts.com/2010-3-Telerik_Silverlight_Grid_with_BCS_Lists_in_SharePoint_2010.aspx My implementation of his example is added below.

MainPage.xaml.cs
private List<Matters> matters = new List<Matters>();
        private IEnumerable<ListItem> returnedItems = null;
        

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            
        }

        
        
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            ClientContext context = ClientContext.Current;
            returnedItems = context.LoadQuery(
                context.Web.Lists.GetByTitle("myExternalListName").GetItems(new CamlQuery())
                );
            context.ExecuteQueryAsync(succeededCallback, failedCallback);
        }

        void succeededCallback(object sender, ClientRequestSucceededEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
                {
                    matters.Clear();
                    foreach (var item in returnedItems)
                    {
                        matters.Add(new Matters(item));
                    }
                    radGridView1.ItemsSource = matters;
                }
            );
        }

        void failedCallback(object sender, ClientRequestFailedEventArgs e)
        {
            MessageBox.Show(e.ErrorDetails.ToString(), "Error", MessageBoxButton.OK);
        }

Open in new window

MainPage.xaml using Telerik RadGrid
<Grid x:Name="LayoutRoot" Background="White" Height="580">
        <controls:RadGridView Name="radGridView1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="12,12,0,0" CanUserSelect="True" CanUserFreezeColumns="False" AutoExpandGroups="False" AutoGenerateColumns="False" CanUserDeleteRows="False" AlternateRowBackground="CadetBlue" Width="600">
            <controls:RadGridView.Columns>
                <controls:GridViewDataColumn Header="Client Name" DataMemberBinding="{Binding ClientName}" />
                <controls:GridViewDataColumn Header="Client Num" DataMemberBinding="{Binding ClientNum}" />
                <controls:GridViewDataColumn Header="Billing Attorney" DataMemberBinding="{Binding BillingName}" />
            </controls:RadGridView.Columns>
        </controls:RadGridView>
        <telerik:RadDataPager PageSize="100" Width="600" HorizontalAlignment="Left" Source="{Binding Items, ElementName=GridView}"
								  IsTotalItemCountFixed="True"
								  DisplayMode="FirstLastPreviousNextNumeric, Text" Margin="0,552,0,0" />
    </Grid>

Open in new window

Class file - _item.FieldValues are the database table column names
private ListItem _item;

        public myClassFileName(ListItem item) { _item = item; }
        public string ClientName { get { return _item.FieldValues["Client_Name"].ToString(); } }
        public string ClientNum { get { return _item.FieldValues["Client_Number"].ToString(); } }
        public string BillingName { get { return _item.FieldValues["Billing_Name"].ToString(); } }

Open in new window

0
 

Author Closing Comment

by:vpit
ID: 38904773
Kukdai's answer was a good series of steps which is why I gave him 500 points, but the details in the final solution were found and posted in my reply. While I gave the expert all 500 points for his valued input, I selected my reply as the "best answer" because it gave all the detailed steps.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

The vision: A MegaMenu for a SharePoint portal home page The mission: Make it easy to maintain. Allow rich content and sub headers as well as standard links. Factor in frequent changes without involving developers or a lengthy Dev/Test/Prod rel…
In this article you will learn how to create a free basic website on Bitbucket, a git service provider. Polymer creates dynamic HTML components, which allow more flexibility than static HTML. This tutorial uses Ubuntu Linux but can also be done on W…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now