?
Solved

SharePoint - Combine two SPDataSource objects for one GridView

Posted on 2011-05-03
6
Medium Priority
?
1,194 Views
Last Modified: 2012-06-27
I have a project when I need to show the results a query on a SharePoint custom list and a SharePoint library in the same GridView.  If the result comes from the list, I need to format a link one way.  If it comes from the library, I format it another way.

From the list...    
            <asp:TemplateField HeaderText="Lot Number" SortExpression="LotNumber">
                <ItemTemplate>
                    <%# this.RenderFromList((string)Eval("ID"))%>                
                </ItemTemplate>
                <HeaderStyle Wrap="False" Width="85px"  />
            </asp:TemplateField>

From the library...    
            <asp:TemplateField HeaderText="Lot Number" SortExpression="LotNumber">
                <ItemTemplate>
                    <%# this.RenderFromLibrary((string)Eval("Name"))%>                
                </ItemTemplate>
                <HeaderStyle Wrap="False" Width="85px"  />
            </asp:TemplateField>

You see that from the List, I am passing the items ID number, which will be used to form a link to a page that will display item information.  From the library, I am passing the Name field, so I can actually open up the document.

All the other metadata fields on the list and library that I would search on, and build the query string from, are the same.  Right now, I have two GridView controls showing the results from each, based on the same query string.  And that works well.

What I need is...
1. A way to combine the two SPDataSource objects, either into a single datasource, or some sort of intermediary object.

2. A way to pass the results to the method in the asp:TemplateField control, one based on the list item ID, the other on the library document name.

Thanks,

Don B
0
Comment
Question by:donq1
  • 3
  • 2
6 Comments
 

Author Comment

by:donq1
ID: 35514514
And as always, code examples are most welcome.
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 35688880
Best of my knowledge, it is not possible to use two SPDataSources on the same gridview. You have to do this in code as exemplified in the first section here: http://www.sharepointnutsandbolts.com/2008/06/spdatasource-every-sharepoint-developer.html
0
 

Author Comment

by:donq1
ID: 35694572
That article won't help.  I have methods to get items from a list.  I can collect the items from a list and a library using something like the following:

            SPQuery oQuery = new SPQuery();
            oQuery.Query = strQuery;
            SPListItemCollection collListItems = list.GetItems(oQuery);

            foreach (SPListItem oListItem in collListItems)
            {
            // Do something
             }

But how do I get the results of those queries together, into some sort of DataSource, that I can then bind to a GridView?

From the list and library, I want to pull two text fields that are common to both.  From the library I am going to pull the document Name, and from the list I am going to pull the ID.  I can have a boolean value set if the result came from the list or the library.  Then, when I am formatting the item in the TemplateField, I can use a method to format a URL.

So, I need to get the below into a datasource:
Boolean - FromList (List = true, Library = false)
String - NameOrID
String - TextFieldA
String - TextFieldB

Also, I don't want to use an XmlDataSource, as that will not allow GridView sorting.  To bad.

So I guess what I am asking, how do I get some home made data into a datasource.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 9

Accepted Solution

by:
SharePointGirl earned 1000 total points
ID: 35718009
Can you not create class called Docs  which contains all the columns you require and bind to that.

List<Docs>List<Docs> docs = new List<Docs>();
foreach (SPListItem oListItem in collListItems)
            {
                  Docs doc = new Doc();
                  doc.Name = oListItem["Name"];
                  doc.TextFieldA = oListItem["TextFieldA "];


                       docs.Items.Add(doc);
                       
                        }
   REPEAT FOR DOCUMENT

grid.DataSource = docs;

grid.DataBind();
0
 
LVL 31

Assisted Solution

by:MlandaT
MlandaT earned 1000 total points
ID: 35718355
"But how do I get the results of those queries together, into some sort of DataSource, that I can then bind to a GridView?"

You can create a new list. Then add items from the two sources to this list. Then bind the grid to this new list.
0
 

Author Comment

by:donq1
ID: 35720048
SharePoint Girl and Mlanda T, you both have good ideas. So I'm happy to split the points between you.  However, I did not go with either.  I was doing this as a user control hosted by a SmartPart, and did not want to get too complex with creating classes and such.  In the end, I simply populated a DataTable with the results of two queries, including a boolean value for what list/library it came from.  Then I did a bind to the GridView, and formatted the special field with a method called from the asp:TemplateField control.

Of course, now I could not use the default sorting and paging of a proper DataSource. So I had to write methods for that myself.  Thanks anyway.

Don B
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

807 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