how to cancel the adding row process in DataGridView?
-
For information I am working with a bindinglist as datasource : see the objectview project here: http://sourceforge.net/projects/objectviews OK, i want to be more explicit, It's really easy to work with them both. But in my context, I have keys collections. So I wanted to have an inputBox which would appear when the object bounded contains a key. Of course this inputBox is asking a unique key for the new object (row...) The idea is that once the object is created, the user is not allowed to edit the key column (which has been set in read only mode at generation time). Currently, how am I working? I have a control which extend the DataGridView and which is composed of an ObjectView let's call them dgvExt and collection. in dgvExt i have my implementation to generate columns, to know which are keys,... in dgvExt I have a EventHandler which listen the collection.RowAdding event. this event is sending in parameter the innerobject (null at this step) that will be bounded to the row. so.. In my EventHandler, I am creating my self the new object if it contains keys. and I set it in the parameter sent by collection. maybe you will understand why I want to work like this when you'll this code:
void collection\_RowAdding(object sender, InstanceEventArgs e) { InputBox ib; Type type = typeof(ConfigurationElement); if (e.Type.Equals(typeof(KeyValueConfigurationElement))) { ib = new InputBox("Insert the key", "Insert key"); if (ib.ShowDialog() == DialogResult.OK) e.Instance = new KeyValueConfigurationElement(ib.EnteredText, ""); } if (e.Instance != null && e.Instance is ConfigurationElement) { MethodInfo init = typeof(ConfigurationElement).GetMethod("Init", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type\[\] { }, null); init.Invoke(e.Instance, new object\[\] { }); } }
In fact the problem is not what I want to do here, but how to cancel the adding process in dgvExt efficiently? Why to cancel? the inputBox allow the user to cancel... but I don't want a row with empty key... this problem is really hard for me... certainly I am just not on the right way.
enjoy the code with you is my priority, Blaise Braye