0

I have written code where it will show the Sharepoint document library in sp grid view.It is showing the grid but i want the default spmenu field for the type icon. please see the attached image. Below is the code of mine

private void CreateBoundField(string sDataField, string sHeaderText, bool bReadOnly, SPGridView theGridView)
    {
        BoundField field = new BoundField();
        field.DataField = sDataField;
        field.HeaderText = sHeaderText;
        field.HtmlEncode = false;
        field.HeaderStyle.ForeColor = ColorTranslator.FromHtml("#808080");
        field.HeaderStyle.Font.Names = new string[] { "verdana", "arial", "helvetica", "sans-serif" };
        field.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        if (sHeaderText.ToLower().Equals("type"))
        {
            field.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            field.ItemStyle.Width = Unit.Pixel(35);
            field.HeaderStyle.Width = Unit.Pixel(35);
        }
        field.ReadOnly = bReadOnly;
        if (!bReadOnly)
            field.SortExpression = sDataField;
        field.Visible = true;
        theGridView.Columns.Add(field);
    }  

SPGridView gv = new SPGridView();
                gv.ID = "gv";
                gv.EmptyDataText = "No items found!";
                gv.AllowSorting = true;
                gv.AllowFiltering = true;
                gv.AutoGenerateColumns = false;
                gv.EnableSortingAndPagingCallbacks = true;
                CreateBoundField("Type", "Type", false, gv);
                CreateBoundField("Title", "Title", false, gv);
                CreateBoundField("Date", "Date", false, gv);
                CreateBoundField("ProcessTask", "ProcessTask", false, gv);
                DataTable dt = new DataTable();
                SPWeb web = SPContext.Current.Web;
                SPListItemCollection items = web.Lists["Shared Documents"].Items;            
                dt.Columns.Add("Type");
                dt.Columns.Add("Title");
                dt.Columns.Add("Date");
                dt.Columns.Add("ProcessTask");
                foreach (SPListItem item in items)
                {
                    DataRow dr = dt.NewRow();
                    string value = item["ProcessTask"].ToString();
                    value = value.Substring(value.LastIndexOf(";#") + 2);
                    string docicon = SPUtility.ConcatUrls("/_layouts/images",
                    SPUtility.MapToIcon(item.Web, SPUtility.ConcatUrls(item.Web.Url, item.Url), "", IconSize.Size16));
                    dr["Type"] = string.Format("<img src='{0}' />", docicon);
                    dr["Title"] = item.Title;
                    dr["Date"] = item["Created"].ToString().Split(' ')[0];
                    dr["ProcessTask"] = value;
                    dt.Rows.Add(dr);
                }
                gv.DataSource = dt;
                gv.DataBind();
                this.Controls.Add(gv);
               SPListItemCollection _GridCollection = SPContext.Current.Web.Lists["Shared Documents"].Items;
                DataTable _MyDatabale = _GridCollection.GetDataTable();
                gvdetails.DataSource = _MyDatabale;
                gvdetails.DataBind();

}

Out put: image is attached enter image description here

1

Here's an example on how to use the SharePoint:SPMenuField on a SPGridView.

Basically, you first have to add a SharePoint:MenuTemplate control, then a SharePoint:SPMenuField to the SPGridView columns collection, with the MenuTemplateId pointing to the ID of the SPMenuField control previously created.

  • Hi Mazzotti, Basically i want default properties for the type value from sharepoint SPMENU field. or view in browser property.. can you help me on that please – santosh kondapalli Jan 29 '14 at 18:31
  • I didn't get you. Can you rephrase? Also, did you check the link I gave you? – MdMazzotti Jan 29 '14 at 18:35
  • Basically if you see my screen shot which i have posted earlier i want only "View In WebBrowser" and if the Type is visio i wan't to have the option "Edit it in Visio". I am creating the SP Grid Dynamically so i ma not sure how to add SPMENU Field in my code Above. Hope i made it clear – santosh kondapalli Jan 29 '14 at 18:43
  • I can see a CreateBoundField method. What is it? – MdMazzotti Jan 29 '14 at 19:28
  • It will just create Columns for the table and bind to the Grid.I have used this like below (CreateBoundField("Type", "Type", false, gv); CreateBoundField("Title", "Title", false, gv); CreateBoundField("Date", "Date", false, gv); CreateBoundField("ProcessTask", "ProcessTask", false, gv); Where Type,Title,Date,Process task are my column names for my Grid – santosh kondapalli Jan 29 '14 at 19:41

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.