1

Working in a C#/Winforms project, I have derived my own class from DataGridView so that I can implement several extensions.

One of the extensions I want to implement is a different way of processing the ReadOnly property. I have implemented a new ReadOnly property that does what I want, and it's working well, except that all the data bindings to the ReadOnly property are looking at the base DataGridView ReadOnly property.

How can I override the data binding as well as the property?

// My implementation of MyGridView

class MyGridView : DataGridView
{
    private bool _readOnly=true;
    public new bool ReadOnly //shadows property in base class
    {
                get { return _readOnly; }
        set
        {
            _readOnly = value;
            // my custom implementation here
        }
    }
}


// And the problem is here

// is bound to the base ReadOnly, not the new ReadOnly
labelText.DataBindings.Add("Visible", MyGridView1, "ReadOnly");  
  • *"`//overrides base class`"*, no actually [you are shadowing](http://stackoverflow.com/questions/673779/what-is-shadowing) the base class. What are you trying to actually do in the setter, could the work be done instead inside of a overridden [`OnReadOnlyChanged`](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.onreadonlychanged(v=vs.110).aspx) method? – Scott Chamberlain Oct 06 '15 at 16:48
  • Scott, Thanks for your fast response. I've edited the post to reflect that I am shadowing instead of overriding. The setter is applying the `ReadOnly` property to specific columns instead of the entire `DataGridView`, so it really is a different implementation of `ReadOnly`, and probbaly can't live in the `OnReadOnlyChanged` method. – John Jeheimer Oct 06 '15 at 17:00
  • Please add more code showing what the setter is doing, because from your current description I still don't see a reason why you could not do it inside `OnReadOnlyChanged`. – Scott Chamberlain Oct 06 '15 at 17:02

1 Answers1

1

Give your ReadOnly property a different name, in order to avoid confusion when binding.

Maybe ReadOnlySpecificColumns?

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Yes, this occurred to me, but it seems inelegant. I was hoping for a way to shadow both the implementation of `ReadOnly` (done), and the databinding as well. I'm sure it's possible, I just don't know how. – John Jeheimer Oct 06 '15 at 17:23
  • Your `ReadOnly` property does not extend or modify the functionality of the original one but introduces a new functionality. Giving it a new name would better reflect this fact and avoid confusion for someone else reading your code. – Olivier Jacot-Descombes Oct 06 '15 at 17:30
  • I understand your point of view, and I think it's reasonable, but I disagree. I regard this as a different implementation of ReadOnly, which modifies the functionality of the base class. I do, however, recognize your approach as an effective alternative that will achieve my desired functionality. But let's file it as "related, but not precisely on-topic", as my question is how to override both a property and it's corresponding data source. – John Jeheimer Oct 06 '15 at 17:43
  • 1
    Since binding works "by name", there is no way to distinguish between the two properties. – Olivier Jacot-Descombes Oct 06 '15 at 17:48
  • Ahh, thanks. This is interesting - I am surprised that this isn't possible. – John Jeheimer Oct 06 '15 at 18:16