Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to develop a custom web part for SharePoint 2007. How do I display the results of a CAML query on a data grid? The grid view should be customizable, and should not adversely affect performance.

share|improve this question
    
Do you mean a using a custom field type, or an entirely custom data view web part? –  Stuart Pegg May 16 '11 at 17:28
    
entire data view webpart. –  Karthikeyan May 17 '11 at 0:07
add comment

4 Answers

up vote 2 down vote accepted

Haven't had a chance to test this code yet, but this here is the basic idea.

public static void TestOneFuncOne()
{
    using (SPSite site = new SPSite("yoursiteurl"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPList list = web.Lists["Tasks"];
            DataTable dt;
            SPQuery query = new SPQuery();
            query.query = "Your CAML Query Here";
            dt = list.GetItems(query).GetDataTable();
            YourGridview.DataSource = dt;
            YourGridview.DataBind();
        }
    }
}

Does it sufficiently answer your question Or you were looking for more information in terms of best practices to handle a large list etc?

share|improve this answer
    
i have a doubt, does the method "getdatatable" affect the page rendering time, anyhow the list will be having only 1000 items and the query will fetch only 5 items. –  Karthikeyan May 17 '11 at 0:14
    
It really depends. Not sure if your question is if GetDataTable(); adds more processing time when compared to just executing the query and returning a SPListItemCollection. If so yes, but then you still have to manipulate the returned SPListItemCollection to present the content (render time). So I don't think it's really worse than any other method. –  Karthik Murugesan May 17 '11 at 6:26
add comment

There are many different options/combinations to consider. The method you choose will probably depend on your requirements.

Get the data from SharePoint:

  • ObjectDataSource - The more general way to retrieve data. You would implement your own select method to return results from your CAML query.
  • SPDataSource - The "sharepointized" datasource. The link actually has a code sample using SPDataSource with a regular GridView.

Then, you can simply hook one of those up to a GridView or SPGridView. I can recommend a very good tutorial using an ObjectDataSource with SPGridView: Building A SPGridView Control – Part 1: Introducing the SPGridView.

share|improve this answer
add comment

As Stuart Pegg has alluded to, you should probably look at using DataViewWebPart for this. When you say customizable, I am assuming you mean customizable through SharePoint designer, which DVWP is.

You can put the CAML query in either through the code in the page that you are inserting the DVWP into, or you can create it using the GUI tools that SPD provides you.

share|improve this answer
add comment

Link : How to display data from list in applciation page using SPGridview

private void BindGrid() { try {using (SPSite site = new SPSite(SPContext.Current.Site.Url))

     using (SPWeb web = site.OpenWeb())
     {

          //Creation of data table
          table = new DataTable();
          table.Columns.Add("INDEX", typeof(Int32));
          table.Columns.Add("ENTRY", typeof(string));

                                  string dayOfWeek = DateTime.Now.DayOfWeek.ToString();
                                  SPList list = site.RootWeb.Lists["Cafeteria"];

          //Query to get the Entress items with Price big
          SPQuery query = new SPQuery();
          query.Query = "<Where><And><Eq><FieldRef Name='Day' /><Value Type='Choice'>" + dayOfWeek + "</Value></Eq><Eq><FieldRef Name='Type' /><Value Type='Choice'>etr</Value></Eq></And></Where><OrderBy><FieldRef Name='PrBig' Ascending='False' /></OrderBy>";
          SPListItemCollection coll = list.GetItems(query);

          GetDetails(web, coll);

gvCafe.DataSource = table; gvCafe.DataBind(); private void GetDetails(SPWeb web, SPListItemCollection coll) { if (coll.Count > 0) { row = table.Rows.Add(); //SetDefaultImage(web); row["INDEX"] = 1; row["ENTRY"] = SPUtility.ConcatUrls(coll[0].Web.Url, coll[0].Url); } }

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.