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

[Solved] Auto Generation of GridDropDownColumns

13 Answers 255 Views
Grid
This is a migrated thread and some comments may be shown as answers.
TheProgrammer
Top achievements
Rank 1
TheProgrammer asked on 07 Dec 2009, 10:41 AM
Here's what I am trying to do. A application for certain users to update data in the Database.

How it works:
On the first page a list of tables is displayed in a radgrid. Users can click on the table they wish to edit and will send them to the next page. Where they are able to edit data from the chosen table. This again with the radgrid.

This application works fine and only consist of 2 aspx pages. The name of the table is sent from page 1 to page 2 where it changes the connectionstring to display the data of the chosen table. Radgrid auto-generate columns is on and does the job nicely.

However..

Here is the problem.
Radgrid displays al fields of the database in edit-mode as text fields. However there are some columns with foreign keys which I wish to display a combobox/radcombo for.

Question:
How can I, preferable let Radgrid generate those columns automatically? Or is there no other way than check all data types and relations during runtime and programmaticly build up the columns ?

Thank you.

13 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 07 Dec 2009, 12:07 PM
Hello,

You can create your own custom column editor class that extends one of the standard column editor base classes such as GridTextColumnEditor, GridDropDownColumnEditor, or GridBoolColumnEditor. Probably the following link would help you in this.
Custom editors extending auto-generated editors

Also checkout the demo: Grid / Using Server-side API

-Shinu.
0
TheProgrammer
Top achievements
Rank 1
answered on 07 Dec 2009, 01:22 PM
Yes, this looks like it should work for me.. However it fails building on me.

        protected void RadGrid1_CreateColumnEditor(object sender, GridCreateColumnEditorEventArgs e) 
        { 
            if ((e.Column as GridBoundColumn).DataField == "DomainID") 
            { 
                e.ColumnEditor = new GridDropDownListColumnEditor(); 
            } 
        } 
 
        #region CustomEditors declaration 
        private class GridDropDownListColumnEditor : GridTextColumnEditor 
        { 
            private GridDropDownColumn combo; 
 
            protected override void LoadControlsFromContainer() 
            { 
                this.combo = this.ContainerControl.Controls[0] as GridDropDownColumn; 
            } 
 
            public override bool IsInitialized 
            { 
                get 
                { 
                    return this.combo != null; 
                } 
            } 
 
 
            public override string SelectedText 
            { 
                get 
                { 
                    return this.combo.SelectedText; 
                } 
                set 
                { 
                    this.combo.SelectedText = value
                } 
            } 
 
 
 
            public override string SelectedValue 
            { 
                get 
                { 
                    return this.combo.SelectedValue; 
                } 
                set 
                { 
                    this.combo.SelectedValue = value
                } 
            } 
 
            protected override void AddControlsToContainer() 
            { 
                this.combo = new GridDropDownColumn(); 
                this.ContainerControl.Controls.Add(combo); 
            } 
            
        } 
 
 
        #endregion 

Looks like it should work according to the site you gave me.
All ingredients are there:
  • LoadControlsFromContainer: This method is where your column editor should extract values from the TableCell of the grid and assign them to the controls it contains. 
  • AddControlsToContainer: This method is where your column editor should create its child controls and add them to its Controls collection.
  • IsInitialized: This method should return a boolean value indicating whether it is fully loaded and bound, ready for use.
  • For GridDropDownColumnEditor, this includes both the SelectedText and SelectedValue properties (of type String).

These are the errors. I hope you can help me.
Error   1   'WebApplication3.frmEditTable.GridDropDownListColumnEditor' does not implement inherited abstract member 'Telerik.Web.UI.GridTextColumnEditor.Text.set'
Error   2   'WebApplication3.frmEditTable.GridDropDownListColumnEditor' does not implement inherited abstract member 'Telerik.Web.UI.GridTextColumnEditor.Text.get'
Error   3   'WebApplication3.frmEditTable.GridDropDownListColumnEditor.SelectedText': no suitable method found to override 
Error   4   'WebApplication3.frmEditTable.GridDropDownListColumnEditor.SelectedValue': no suitable method found to override
 
Thanks.
0
TheProgrammer
Top achievements
Rank 1
answered on 08 Dec 2009, 10:05 AM
Ok, I've tried changing the GridDropDownListColumnEditor to GridDropDownColumnEditor. However that didnt make much difference. Cant seem to find any examples out there that uses the dropdown in the custom editors.
I am stuck. Hopefully someone here has the answer.

Thanks
0
Tsvetoslav
Telerik team
answered on 10 Dec 2009, 10:41 AM
Hi,

Attached is a sample that demonstrates the implementation of a custom drop-down column editor.

Best wishes,
Tsvetoslav
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Dario Zanelli
Top achievements
Rank 1
answered on 25 Jan 2010, 09:24 AM
Hi Tsvetoslav ,
sorry for the late intervention, but I tried your example and it is working.
But How can I add a RequiredFieldValidator to the RadCombobox?
I have tried but it doesn't work. Here there is the code that I added:

this._reqValidator = new RequiredFieldValidator();
this._reqValidator.ErrorMessage = "This field is required.";
this._reqValidator.ControlToValidate = this.combo.ID;
this._reqValidator.Font.Bold = true;
this._reqValidator.Display = ValidatorDisplay.Dynamic;
this.ContainerControl.Controls.Add(this._reqValidator);


Thanks a lot!

Dario
0
Tsvetoslav
Telerik team
answered on 26 Jan 2010, 12:55 PM
Hello Dario,

You should add any extra controls to the column editor in the AddControlsToContainer method. Therefore, try overriding the AddControlsToContainer base method and add the code for the required field validator to it:

protected override void AddControlsToContainer()
{
    base.AddControlsToContainer();
    this._reqValidator = new RequiredFieldValidator();
    this._reqValidator.ErrorMessage = "This field is required.";
    this._reqValidator.ControlToValidate = this.combo.ID;
    this._reqValidator.Font.Bold = true;
    this._reqValidator.Display = ValidatorDisplay.Dynamic;
    this.ContainerControl.Controls.Add(this._reqValidator);
}


I hope this helps.

Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Dario Zanelli
Top achievements
Rank 1
answered on 28 Jan 2010, 11:39 AM
Thank you Tsvetoslav !
It works!
The last problem I have is the following: I would like to insert the RadComboBox inside a DIV, in order to have the message of error below the correspondant ComboBox, and not on the right, that would spoil the layout.

Where do I have to add the following code? In the AddControlsToContainer function it gives me an error: Object reference null....

HtmlGenericControl divCombo = new HtmlGenericControl("DIV");
divCombo.Controls.Add(this.combo);
this.ContainerControl.Controls.Add(divCombo);



Thanks a lot!

Dario
0
Tsvetoslav
Telerik team
answered on 29 Jan 2010, 11:35 AM
Hello Dario,

Try creating the div and adding it to the ContainerControl in the AddControlsToContainer method, keep it in a variable private to the class and in the CreateControls method add the combo to the div. 

Best wishes,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Dario Zanelli
Top achievements
Rank 1
answered on 29 Jan 2010, 11:48 AM
Hi Tsvetoslav ,

Thanks for the answer. Unfortunately it doesn't work, the eror that I got is:

Object reference not set to an instance of an object.

in the CreateControls() function.

I report to you the complete code of the class:

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

public class ComboBoxEditor : GridDropDownListColumnEditor
{

    private String _datafield;
    private bool _mandatory;
    private bool _enableChange;
    private Unit _width;
    private CustomValidator _reqValidator;
    private RadComboBox combo;
    private HtmlGenericControl divCombo;

    public ComboBoxEditor(GridDropDownColumn owner, bool mandatory, String datafield, bool enableChange, Unit width)
        : base(owner)
    {
        this._datafield = datafield;
        this._mandatory = mandatory;
        this._enableChange = enableChange;
        this._width = width;
    }

    public RadComboBox ComboControl
    {
        get
        {
            return base.ComboBoxControl;
        }
    }

    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override int SelectedIndex
    {
        get { return base.SelectedIndex; }
        set
        {
            base.SelectedIndex = value;
        }
    }

    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override string SelectedValue
    {
        get
        {
            return base.SelectedValue;
        }
        set
        {
            base.SelectedValue = value;
        }
    }

    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override string SelectedText
    {
        get
        {
            return base.SelectedText;
        }
        set
        {
            base.SelectedText = value;
        }
    }

    protected override void CreateControls()
    {
        base.CreateControls();
        base.ControlsCreated = true;
        this.combo = base.ComboBoxControl;
        this.combo.AllowCustomText = false;
        this.combo.Skin = "WebBlue";
        this.combo.Width = this._width;
        this.divCombo.Controls.Add(this.combo);
    }

    protected override void AddControlsToContainer()
    {
        base.AddControlsToContainer();
        this.divCombo = new HtmlGenericControl("DIV");
        this.ContainerControl.Controls.Add(this.divCombo);

        if (this._mandatory)
        {
            this._reqValidator = new CustomValidator();
            this._reqValidator.ErrorMessage = "This field is required.";
            this._reqValidator.ControlToValidate = this.combo.ID;
            this._reqValidator.Font.Bold = true;
            this._reqValidator.Display = ValidatorDisplay.Dynamic;
            this._reqValidator.ClientValidationFunction = "ValidateComboBox";
            this.ContainerControl.Controls.Add(this._reqValidator);
        }
    }
}


Thanks for the support,

Dario




0
Tsvetoslav
Telerik team
answered on 04 Feb 2010, 09:17 AM
Hello Dario,

Excuse me for missing to override another method in the edtior's base class. Please, see the attached file for a ready implementation of your scenario.

Greetings,
Tsvetoslav
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Dario Zanelli
Top achievements
Rank 1
answered on 04 Feb 2010, 09:47 AM
Hi Tsvetoslav!
Thanks for your answer and code!
I have tried it but the Validator doesn't work: it both doesn't show the message in case no option is selected, and it doesn't forbid the updating of the database. So I have an error message saying: "The update conflicted....".
If I update an existing record the update doen't work even if the comboboxes are filled; the insertion of a new record instead works.
At the moment the code that is closer to the solution I need is the one I enclosed in my last post. The only problem is that I would like to insert the combobox inside the DIV (see method AddControlsToContainer() ), in order to have the message of the validator below it insted of beside.

Thanks,

Dario
0
Tsvetoslav
Telerik team
answered on 04 Feb 2010, 04:08 PM
Hello Dario,

I tested the code on my side and it behaves all right. Attached is a working sample.

Greetings,
Tsvetoslav
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Dario Zanelli
Top achievements
Rank 1
answered on 05 Feb 2010, 08:16 AM
Hi Tsvetoslav!
Thank you for your answer.
I have solved the problem adding a <BR> at the beginning of the error message.
Now it works properly.

Kind regards,

Dario Zanelli
Tags
Grid
Asked by
TheProgrammer
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
TheProgrammer
Top achievements
Rank 1
Tsvetoslav
Telerik team
Dario Zanelli
Top achievements
Rank 1
Share this question
or