• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1209
  • Last Modified:

SharePoint - Combine two SPDataSource objects for one GridView

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
donq1
Asked:
donq1
  • 3
  • 2
2 Solutions
 
donq1Author Commented:
And as always, code examples are most welcome.
0
 
MlandaTCommented:
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
 
donq1Author Commented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
SharePointGirlCommented:
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
 
MlandaTCommented:
"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
 
donq1Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now