0

I want to bind the items from multiple list into a gridview. I earlier used SPSiteDataQuery to bind the items from multiple list in a gridview but then I was not able to cast the items into ListItemCollection as I need to get the item property though which I can add a hyperlink field in it with the help of which we can redirect the user to the default edit form of that item.

So how do I bind the items from multiple list into a single grid in a way through which I can get items so that I can navigate to their edit form in the list in which they are?

0
protected void BindGrid()
    {
        using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
        {
            using (SPWeb objWeb = oSite.OpenWeb())
            {
                try
                {
                    SPList List = objWeb.Lists["List1"];
                    SPList List1 = objWeb.Lists["List2"];
                    SPList List2 = objWeb.Lists["List3"];
                    SPList List3 = objWeb.Lists["List4"];
                    SPQuery query = new SPQuery();

                    string where = "<Where><And><Eq>";
                    where += "<FieldRef Name='Status'/>";
                    where += "<Value Type='Choice'>Approved</Value>";
                    where += "</Eq><Eq>";
                    where += "<FieldRef Name='Author' LookupId='True'/>";
                    where += "<Value Type='Text'>" + SPContext.Current.Web.CurrentUser.ID + "</Value>";
                    where += "</Eq></And></Where>";
                    query.Query = where;

                    DataTable dt = new DataTable();

                    //Add columns to Data Table
                    dt.Columns.Add("Title", typeof(string));
                    dt.Columns.Add("Status", typeof(string));
                    dt.Columns.Add("ID", typeof(Int32));
                    dt.Columns.Add("List URL", typeof(string));

                    SPListItemCollection Coll1 = List.GetItems(query);
                    SPListItemCollection Coll2 = List1.GetItems(query);
                    SPListItemCollection Coll3 = List2.GetItems(query);
                    SPListItemCollection Coll4 = List3.GetItems(query);

                    foreach (SPListItem item in Coll1)
                    {
                        DataRow row = dt.NewRow();
                        row["Title"] = item.Title;
                        row["ID"] = item.ID;
                        row["Status"] = item["Status"];
                        row["List URL"] = SPContext.Current.Web.Url + List.RootFolder.ServerRelativeUrl + "/EditForm.aspx?ID=" + item.ID + "&Source=/_Layouts/15/hks/getlistitems.aspx";
                        dt.Rows.Add(row);
                    }

                    foreach (SPListItem item in Coll2)
                    {
                        DataRow row = dt.NewRow();
                        row["Title"] = item.Title;
                        row["ID"] = item.ID;
                        row["Status"] = item["Status"];
                        row["List URL"] = SPContext.Current.Web.Url + List1.RootFolder.ServerRelativeUrl + "/EditForm.aspx?ID=" + item.ID + "&Source=/_Layouts/15/hks/getlistitems.aspx";
                        dt.Rows.Add(row);
                    }

                    foreach (SPListItem item in Coll3)
                    {
                        DataRow row = dt.NewRow();
                        row["Title"] = item.Title;
                        row["ID"] = item.ID;
                        row["Status"] = item["Status"];
                        row["List URL"] = SPContext.Current.Web.Url + List2.RootFolder.ServerRelativeUrl + "/EditForm.aspx?ID=" + item.ID + "&Source=/_Layouts/15/hks/getlistitems.aspx";
                        dt.Rows.Add(row);
                    }

                    foreach (SPListItem item in Coll4)
                    {
                        DataRow row = dt.NewRow();
                        row["Title"] = item.Title;
                        row["ID"] = item.ID;
                        row["Status"] = item["Status"];
                        row["List URL"] = SPContext.Current.Web.Url + List3.RootFolder.ServerRelativeUrl + "/EditForm.aspx?ID=" + item.ID + "&Source=/_Layouts/15/hks/getlistitems.aspx";
                        dt.Rows.Add(row);
                    }
                    //Add Columns to Grid
                    BoundField col1 = new BoundField();
                    col1.HeaderText = "Title";
                    col1.DataField = "Title";
                    mygrid.Columns.Add(col1);

                    BoundField col2 = new BoundField();
                    col2.HeaderText = "ID";
                    col2.DataField = "ID";
                    mygrid.Columns.Add(col2);

                    BoundField col3 = new BoundField();
                    col3.HeaderText = "Status";
                    col3.DataField = "Status";
                    mygrid.Columns.Add(col3);

                    HyperLinkField col4 = new HyperLinkField();
                    col4.HeaderText = "Edit";
                    col4.Text = "Edit Item";
                    col4.DataNavigateUrlFields = new string[] { dt.Columns["List URL"].ToString() };
                    mygrid.Columns.Add(col4);

                    //Bind Data in GridView
                    mygrid.DataSource = dt;
                    mygrid.DataBind();

                }
                catch (Exception ex)
                {
                    Page.ClientScript.RegisterClientScriptBlock(typeof(SPAlert),
                        "alert", "<script language=\"javascript\">alert('" + ex.Message + " .')</script>");
                }
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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