Solved

SPGridview in webpart

Posted on 2011-03-09
2
921 Views
Last Modified: 2013-12-17
I am trying to bind a document library list to SPGridview.
I saw an example found at this site: http://nishantrana.wordpress.com/2009/03/23/using-spgridview-to-bound-to-list-data-in-sharepoint/ 

But I get an error and a warning:
Error      1      The name 'grdPropertyValues' does not exist in the current context      C:\Users\sogunroI\Documents\Visual Studio 2008\Projects\HotLineWP\HotLineWP\hotline\HotLineIncoming.ascx.cs      32      40      HotLineWP

Warning      2      Type of 'HotLineDocs.HotLineIncoming.SPGridView1' is not CLS-compliant      C:\Users\sogunroI\Documents\Visual Studio 2008\Projects\HotLineWP\HotLineWP\hotline\HotLineIncoming.ascx.designer.cs      23      71      HotLineWP


Please help. Thanks!


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace HotLineDocs
{
    public partial class HotLineIncoming : System.Web.UI.UserControl
    {
        //http://nishantrana.wordpress.com/2009/03/23/using-spgridview-to-bound-to-list-data-in-sharepoint/

        SPSite mySite = SPContext.Current.Site;
        SPWeb myWeb;
        SPList myList;

        protected void Page_Load(object sender, EventArgs e)
        {
            myWeb = mySite.OpenWeb();
            myList = myWeb.Lists["HOTLINE"];

            if (!Page.IsPostBack)
            {
                BindListToGrid(myList, grdPropertyValues);
            }
        }

        private void BindListToGrid(SPList myList, SPGridView gridView)
        {
            SPListItemCollection results = myList.Items;

            //Create the datatable object
            DataTable table;
            table = new DataTable();
            table.Columns.Add("Type", typeof(string));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Modified", typeof(string));
            table.Columns.Add("Modified By", typeof(string));
            table.Columns.Add("STATUS", typeof(string));
            table.Columns.Add("PRIORITY CODE", typeof(string));
            table.Columns.Add("PRIORITY", typeof(string));
            table.Columns.Add("CASE NUMBER", typeof(string));
            table.Columns.Add("COMPLETE", typeof(string));

            //Create the rows for each splistitem
            DataRow row;
            foreach (SPListItem result in results)
            {
                row = table.Rows.Add();
                row["Type"] = result["Type"].ToString();
                row["Name"] = result["Name"].ToString();
                row["Modified"] = result["Modified"].ToString();
                row["Modified By"] = result["Modified By"].ToString();
                row["STATUS"] = result["STATUS"].ToString();
                row["PRIORITY CODE"] = result["PRIORITY CODE"].ToString();
                row["PRIORITY"] = result["PRIORITY"].ToString();
                row["CASE NUMBER"] = result["CASE NUMBER"].ToString();
                row["COMPLETE"] = result["COMPLETE"].ToString();
            }

            //Create the bound fields
            SPBoundField boundField;
            boundField = new SPBoundField();
            boundField.HeaderText = "Type";
            boundField.DataField = "Type";
            boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            boundField.ItemStyle.Wrap = false;
            SPGridView1.Columns.Add(boundField);

            boundField = new SPBoundField();
            boundField.HeaderText = "Name";
            boundField.DataField = "Name";
            SPGridView1.Columns.Add(boundField);

            boundField = new SPBoundField();
            boundField.HeaderText = "Modified";
            boundField.DataField = "Modified";
            SPGridView1.Columns.Add(boundField);

            boundField = new SPBoundField();
            boundField.HeaderText = "Modified By";
            boundField.DataField = "Modified By";
            SPGridView1.Columns.Add(boundField);

            boundField = new SPBoundField();
            boundField.HeaderText = "STATUS";
            boundField.DataField = "STATUS";
            SPGridView1.Columns.Add(boundField);

            boundField = new SPBoundField();
            boundField.HeaderText = "PRIORITY CODE";
            boundField.DataField = "PRIORITY CODE";
            SPGridView1.Columns.Add(boundField);

            boundField = new SPBoundField();
            boundField.HeaderText = "PRIORITY";
            boundField.DataField = "PRIORITY";
            SPGridView1.Columns.Add(boundField);

            boundField = new SPBoundField();
            boundField.HeaderText = "CASE NUMBER";
            boundField.DataField = "CASE NUMBER";
            SPGridView1.Columns.Add(boundField);

            boundField = new SPBoundField();
            boundField.HeaderText = "COMPLETE";
            boundField.DataField = "COMPLETE";
            SPGridView1.Columns.Add(boundField);

        }
    }
}

Open in new window

0
Comment
Question by:Isaac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
SharePointGirl earned 500 total points
ID: 35087843
SPGridView grdPropertyValues; //add this under your list definition

protected override void CreateChildControls()
{
     grdPropertyValues = new SPGridView();
     Controls.Add(grdPropertyValues);
)

0
 
LVL 5

Author Comment

by:Isaac
ID: 35095601
SharePointGirl,

The code you gave, "SPGridView grdPropertyValues;" fixed the error but now I get a warning.
I should have given more details.  
The code you see above is a user control that will be in a web part.

I get the following warning now:
Warning      3      Field 'HotLineDocs.HotLineIncoming.grdPropertyValues' is never assigned to, and will always have its default value null      C:\Users\sogunroI\Documents\Visual Studio 2008\Projects\HotLineWP\HotLineWP\hotline\HotLineIncoming.ascx.cs      24      20      HotLineWP


The webpart code is below.
I tried to add the following in my "CreateChildControls()" but it does not recognize grdPropertyValues

grdPropertyValues = new SPGridView();
     Controls.Add(grdPropertyValues);

Any ideas?

Also, if this could have been done better, please enlighten me.  This is my first attempt at this.
Thanks for any help you can provide.

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace HotLineWP
{
    [Guid("6b6dfe8b-46b2-4523-a9ba-ed21421f857a")]
    public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
    {
        Control HotLineControl;
        String err;

        public WebPart1()
        {
        }

        protected override void Render(HtmlTextWriter writer)
        {
            try
            {
                HotLineControl.RenderControl(writer);
            }
            catch (Exception e)
            {
                writer.Write(e.Message + ":" + err);
            }
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            
            // TODO: add custom rendering code here.
            // Label label = new Label();
            // label.Text = "Hello World";
            // this.Controls.Add(label);
            try
            {
                this.Controls.Clear();
                HotLineControl = this.Page.LoadControl("hotline/HotLineIncomming.ascx");
            }
            catch (Exception e)
            {
                err = e.Message;
            }
        }
    }
}

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question