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

Exposing Columns property of RadGridView in a user control

6 Answers 133 Views
GridView
This is a migrated thread and some comments may be shown as answers.
SUNIL
Top achievements
Rank 2
Iron
Iron
Iron
SUNIL asked on 15 Oct 2010, 06:03 PM
I have a user control in which one of the UI elements is radgridview.
I would like the developer using  this user control to be able to set the columns collection of radgridview on user control.
I have exposed a property from user control, with the following code. I can also define the columns for user control placed on a form by editing this property in design view.  But if I close the form and re-open it, then the columns I defined a few moments ago,  suddenly disappear. I am using Q1 2008 SP1 version.

[Browsable(true)]
        [Description("Columns for grid")]
        [Category("Customization")]
        public GridViewColumnCollection GridColumns
        {
            get { return radGridView1.MasterGridViewTemplate.Columns; }
            set
            {
                foreach (GridViewDataColumn column in value)
                {
                    radGridView.MasterGridViewTemplate.Columns.Add(column);
                }
                 
            }
        }

6 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 15 Oct 2010, 06:35 PM
Hello Sunil,

You forgot the DesignerSerializationVisibility attribute, try setting that to Content, like this:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(true)]
[Description("Columns for grid")]
[Category("Customization")]
public GridViewColumnCollection GridColumns
{
    get { return radGridView1.MasterGridViewTemplate.Columns; }
    set
    {
        foreach (GridViewDataColumn column in value)
        {
            radGridView.MasterGridViewTemplate.Columns.Add(column);
        }
          
    }
}

It should work like this, if it is not working you can try also creating a property named ShouldSerializeGridColumns that returns true, but i think that was required just in the earlier days.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
SUNIL
Top achievements
Rank 2
Iron
Iron
Iron
answered on 15 Oct 2010, 06:48 PM
Emanuel,

Your suggestion worked, except that the originally defined columns are showing up also when I close and re-open the form.
So if I change the  exposed property of Columns in user control to include Col1 and Col2, and if original columns were XY and AB, then on closing/opening the form I get 4 columns show up on user control in the following order - XY, AB, Col1 and Col2.

Thanks
Sunil
0
SUNIL
Top achievements
Rank 2
Iron
Iron
Iron
answered on 15 Oct 2010, 06:52 PM
Emanuel,

Is there a way to avoid exposing the Columns collection in user control, but just exposing the radgridview in user control, and then setting columns property of exposed radgridview in user control. I tried it  but  it didn't seem to work.

Thanks
Sunil
0
Emanuel Varga
Top achievements
Rank 1
answered on 15 Oct 2010, 06:53 PM
Hello again Sunil,

The designer is sometimes acting crazy, please try to add your control to a new form and see if you have the same behavior with this new attribute.

Best Regards,
Emanuel Varga
0
Maxime
Top achievements
Rank 1
answered on 19 Oct 2011, 03:16 PM
Hi! I try to do the same thing as mentioned above.
Is anyone have an answer to that question?

"
Is there a way to avoid exposing the Columns collection in user control, but just exposing the radgridview in user control, and then setting columns property of exposed radgridview in user control. I tried it  but  it didn't seem to work."

Thanks!

Maxime.
0
Jack
Telerik team
answered on 21 Oct 2011, 01:34 PM
Hello Maxime,

It is not recommended to allow changes in an inherited user control. Although this was possible in previous Visual Studio versions, Microsoft disabled the feature in Visual Studio 2010.
 
Kind regards,
Jack
the Telerik team

Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

Tags
GridView
Asked by
SUNIL
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Emanuel Varga
Top achievements
Rank 1
SUNIL
Top achievements
Rank 2
Iron
Iron
Iron
Maxime
Top achievements
Rank 1
Jack
Telerik team
Share this question
or