This is a migrated thread and some comments may be shown as answers.

[Solved] Using RadGrid in a web part

7 Answers 155 Views
RadControls
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sanaz
Top achievements
Rank 1
Sanaz asked on 28 Aug 2008, 05:41 PM
I want to create a web part that uses the RadGrid control. The grid needs to have add new / edit and delete record functionality. Similart to this example  http://sharepoint.telerik.com/Products/RadControlsAjax/Pages/RadGrid.aspx.

Is there any code sample that can help me to write the web part?

Thanks

7 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 01 Sep 2008, 12:41 PM
Hello Sanaz,

To see more information along the lines of the requested functionality, please refer to the following post. I hope it gets you started properly.

Regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Sanaz
Top achievements
Rank 1
answered on 02 Sep 2008, 01:51 PM
Hi Yavor,

Thanks for your response. I was able to have the RadGrid working in my webpart. It displays my data as expected. 

I have a problem with Edit functionality. The AutoGenerateEditColumn property it set to True. When I click on the edit, I got the following error:
"Multiple controls with the same ID 'AutoGeneratedEditButton' were found. FindControl requires that controls have unique IDs. "

It would be great to help me out with this issue.

Regards,
Sanaz
0
Sebastian
Telerik team
answered on 02 Sep 2008, 02:01 PM
Hello Sanaz,

Is it possible that you have more than one auto-generated edit column in your grid? If so, remove the other auto-generated edit column instances to see whether this eliminates the exception. Alternatively, if you need additional edit column, consider adding a template column with edit button inside its item template (having CommandName="Edit" for it).

Best regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Sanaz
Top achievements
Rank 1
answered on 11 Sep 2008, 03:44 PM

Hi,

By setting the EnableColumnsViewState to false my previous problem has gone.

Now I have problem inserting new record into the grid. I've attached my code. On the edit form, insert button does not call the insert method "InsertAdminComment" so the new record doesn't insert.  

I appreciate your help.

Thanks, 

public partial class CommentsWebPart : System.Web.UI.WebControls.WebParts.WebPart

    {
        private RadGrid gvComments = null;
        private ObjectDataSource ods=null;
        private ObjectDataSource odsFiscalYear;
        private Panel pnlContainer { get; set; }

          #region Overrides
        protected override void OnInit(EventArgs e)
        {

            base.OnInit(e);
            EnsureChildControls();
        }
 
        protected override void CreateChildControls()
        {

            DefineGridStructure();
            base.CreateChildControls();
 
        }

        #endregion

        # region private method
        private void DefineGridStructure()
        {
            pnlContainer = new Panel();
            this.Controls.Add(pnlContainer);
            pnlContainer.EnableViewState = false;

            //Represents the specified types
            Type typeAdminComments = typeof(AdminComments);
            Type typePeriods=typeof(Periods);

            //Object Data Source
            ods = new ObjectDataSource(typeAdminComments.AssemblyQualifiedName.ToString(), "GetAdminCommentsByUserID");
            ods.ID = "ods1";
            ods.SelectParameters.Add("UserID", "test");
 
          ods.InsertMethod = "InsertAdminComment";

            ods.InsertParameters.Add("FiscalYear", "2008");
            ods.InsertParameters.Add("Comment",     "my first test Heyyyyyyyy");

            odsFiscalYear = new ObjectDataSource(typePeriods.AssemblyQualifiedName.ToString(), "GetFiscalYears");
            odsFiscalYear.ID = "odsFiscalYear";

            //Grid Defininition
            gvComments = new RadGrid();
            gvComments.ID = "gvComments1";
            gvComments.Skin = "Vista";
            gvComments.AutoGenerateColumns = false;

            gvComments.AllowAutomaticUpdates = true;
            gvComments.AllowAutomaticDeletes = true;
            gvComments.AllowAutomaticInserts = true;

            gvComments.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;  

            //Events 
           gvComments.ItemInserted += new GridInsertedEventHandler(gvComments_ItemInserted);
               
            gvComments.MasterTableView.EnableColumnsViewState = false;
            gvComments.DataSourceID = ods.ID;

            pnlContainer.Controls.Add(ods);
            pnlContainer.Controls.Add(odsFiscalYear);
            pnlContainer.Controls.Add(gvComments);

            //Grid Columns

            GridDropDownColumn d = new GridDropDownColumn();
            d.HeaderText = "Fiscal Year";
            d.DataField = "FiscalYear";
            d.ListValueField = "ValueField";
            d.ListTextField = "TextField";
            d.DataSourceID = "odsFiscalYear";
            //gvComments.Columns.Add(d);
            gvComments.MasterTableView.Columns.Add(d);

            GridBoundColumn b = new GridBoundColumn();
            b.HeaderText = "Comment";
            b.DataField = "Comment";
            gvComments.MasterTableView.Columns.Add(b);

            GridEditCommandColumn e = new GridEditCommandColumn();
            e.HeaderText = "Edit";
            e.UniqueName = "EditCol";
            gvComments.MasterTableView.Columns.Add(e);

            GridButtonColumn del = new GridButtonColumn();
            del.HeaderText = "Delete";
            del.UniqueName = "DelCol";
            del.CommandName = "Delete";
            del.Text = "Delete";
            del.ConfirmText = "Delete this comment?";
            gvComments.MasterTableView.Columns.Add(del);  

        }
#endregion

        #region Events
 

        protected void gvComments_ItemInserted(object source, GridInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
                //DisplayMessage("Record cannot be inserted. Reason: " + e.Exception.Message);
            }
            else
            {
                //DisplayMessage("Record inserted");
            }
        }
        #endregion
    }

 

0
Vlad
Telerik team
answered on 12 Sep 2008, 06:08 AM
Hello Sanaz,

ViewState is disabled in your case: pnlContainer.EnableViewState = false;

You need to enable this if you want this event to be executed.

All the best,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Sanaz
Top achievements
Rank 1
answered on 12 Sep 2008, 09:25 PM

Hi – Great - Now the inset method is called and the event is fired.

I want to add another column to my grid that is a boundcolumn “DateUpdated”. The DateUpdated is not a parameter of 'InsertAdminComment' method. When I added the column I got the following error when inserting

"ObjectDataSource 'ods1' could not find a non-generic method 'InsertAdminComment' that has parameters: FiscalYear, Comment, DateUpdated"  

How can I fix this? There are going to be more columns in the grid but not in the insert method parameters. Is it necessary that the columns in the grid match with the parameters in the insert/update/delete methods?

Thanks

0
Vlad
Telerik team
answered on 15 Sep 2008, 06:34 AM
Hi Sanaz,

You can set ReadOnly to true for this column.

Sincerely yours,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
RadControls
Asked by
Sanaz
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Sanaz
Top achievements
Rank 1
Sebastian
Telerik team
Vlad
Telerik team
Share this question
or