7

I am trying to create the example at this link http://kitmenke.com/blog/2010/02/18/editing-in-sharepoints-spgridview/comment-page-1/#comment-1777 inside a User Control and not in a Web Part.

Here is my code-behind file named SimpleLogic.ascx.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Data;
using System.Text;


namespace SimpleLogic.ControlTemplates.SimpleLogic
{
    public partial class SimpleLogic : UserControl
    {
        const string LIST_NAME = "clientmatter";
        string[] LIST_COLUMNS = null;
        string VIEW_FIELDS = string.Empty;
        string[] DATA_KEY_NAMES = null;
        private SPSite site = new SPSite("http://abcdev");
        private SPWeb _currentweb;
        private SPWeb sPWeb;

        public SimpleLogic(SPWeb currentWeb)
        {
            // TODO: Complete member initialization
            this.sPWeb = currentWeb;

            DATA_KEY_NAMES = new string[1];
            DATA_KEY_NAMES[0] = "ID";
            LIST_COLUMNS = new string[4];
            LIST_COLUMNS[0] = "ID";
            LIST_COLUMNS[1] = "Title";
            LIST_COLUMNS[2] = "clientid";
            LIST_COLUMNS[3] = "Client";
            VIEW_FIELDS = GetViewFields(LIST_COLUMNS);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            SimpleLogic logic = new SimpleLogic(SPControl.GetContextWeb(Context));
            SimpleSPGrid grid = new SimpleSPGrid(logic);

            this.Controls.Add(grid);
        }

        public string[] GetDataKeyNames()
        {
            return DATA_KEY_NAMES;
        }

        public List<BoundField> GetColumns()
        {
            List<BoundField> fields = new List<BoundField>();
            fields.Add(GetBoundField("ID"));
            fields[0].ReadOnly = true;
            fields.Add(GetBoundField("Title"));
            fields.Add(GetBoundField("clientid"));
            fields.Add(GetBoundField("Client"));
            return fields;
        }

        private BoundField GetBoundField(string name)
        {
            BoundField bf = new BoundField();
            bf.HeaderText = name;
            bf.DataField = name;
            return bf;
        }

        public DataTable Select()
        {
            SPQuery q = new SPQuery();
            q.ViewFields = VIEW_FIELDS;
            q.Query = "<Where><IsNotNull><FieldRef Name='clientid' /></IsNotNull></Where>";
            SPList list = _currentweb.Lists[LIST_NAME];
            SPListItemCollection items = list.GetItems(q);
            return items.GetDataTable();
        }

        private string GetViewFields(string[] columns)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string field in columns)
            {
                sb.AppendFormat("<FieldRef Name='{0}' />", field);
            }
            return sb.ToString();
        }

        public void Update(Dictionary<string, string> data)
        {
            SPList list = _currentweb.Lists[LIST_NAME];
            int id = Int32.Parse(data["ID"]);
            SPListItem item = list.GetItemById(id);
            item["Title"] = data["Title"];
            item["clientid"] = data["clientid"];
            item["Client"] = data["Client"];
            item.Update();
        }
    }
}

Here is my extra class called "SimpleSPGrid.cs"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using System.Collections;

namespace SimpleLogic.ControlTemplates.SimpleLogic
{
    class SimpleSPGrid
    {
        private SimpleLogic _logic;
        private ObjectDataSource _gridDS;
        private SPGridView _grid;

        public SimpleSPGrid(SimpleLogic logic)
        {
            _logic = logic;
        }

        protected sealed override void CreateChildControls()
        {

            const string GRIDID = "grid";
            const string DATASOURCEID = "gridDS";

            _gridDS = new ObjectDataSource();
            _gridDS.ID = DATASOURCEID;
            _gridDS.TypeName = typeof(SimpleLogic).AssemblyQualifiedName;
            _gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gridDS_ObjectCreating);
            _gridDS.SelectMethod = "Select";
            _gridDS.UpdateMethod = "Update";
            _gridDS.Updating += new ObjectDataSourceMethodEventHandler(gridDS_Updating);

            this.Controls.Add(_gridDS);

            _grid = new SPGridView();
            _grid.ID = GRIDID;
            _grid.DataSourceID = _gridDS.ID;
            _grid.AutoGenerateColumns = false;
            _grid.DataKeyNames = _logic.GetDataKeyNames();

            CommandField command = new CommandField();
            command.ShowEditButton = true;
            _grid.Columns.Add(command);

            foreach (BoundField column in _logic.GetColumns())
            {
                _grid.Columns.Add(column);
            }
            this.Controls.Add(_grid);
        }

        void gridDS_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();

            foreach (DictionaryEntry entry in e.InputParameters)
            {
                string value = entry.Value == null ? null : entry.Value.ToString();
                data.Add(entry.Key.ToString(), value);
            }

            e.InputParameters.Clear();
            e.InputParameters.Add("data", data);
        }

        private void gridDS_ObjectCreating(object sender, ObjectDataSourceDisposingEventArgs e)
        {
            e.ObjectInstance = _logic;
        }
    }
}

I am getting compile errors. Right now it states that

“SimpleLogic.ControlTemplates.SimipleLogic.SimpleSPGrid.CreateChildControls(): no suitable method found to override”.

What am I doing wrong? Thanks.

1 Answer 1

1

Your class SimpleSPGrid does not inherit from anything, so I presume it inherits from Object. CreateChildControls() is a method of the Control class. Since the Object class does not have a CreateChildControls() method, SimpleSPGrid can't override it.

Solution: Change the line

Class SimpleSPGrid 

to

Class SimpleSPGrid : Control

Another option, if you really want SimpleSPGrid to inherit from Object and not Control, change CreateChildControls to virtual, not override.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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