SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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;

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

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

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

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

Your Answer


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.