Exposing Columns property of RadGridView in a user control

7 posts, 1 answers
  1. Sunil
    Sunil avatar
    231 posts
    Member since:
    Jan 2004

    Posted 15 Oct 2010 Link to this post

    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);
                    }
                     
                }
            }
  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 15 Oct 2010 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Sunil
    Sunil avatar
    231 posts
    Member since:
    Jan 2004

    Posted 15 Oct 2010 Link to this post

    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
  5. Sunil
    Sunil avatar
    231 posts
    Member since:
    Jan 2004

    Posted 15 Oct 2010 Link to this post

    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
  6. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 15 Oct 2010 Link to this post

    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
  7. Maxime
    Maxime avatar
    1 posts
    Member since:
    Oct 2011

    Posted 19 Oct 2011 Link to this post

    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.
  8. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 21 Oct 2011 Link to this post

    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.

Back to Top
UI for WinForms is Visual Studio 2017 Ready