![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Untrapable exception when deleting last row in databound DataGridV Hi everyone, Background: I'm working with a standard Windows Forms DataGridView control. It's databound to a List<> of custom business objects. When I delete the LAST row using the user delete event (Del key press), I get an ArgumentOutOfRangeException exception when I delete the last row of the DataGridView: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index This exception bubbles up to Main(). It has no stack trace and has no InnerException. I do NOT get this exception when I delete the last row when the delete is triggered from a context menu item. Note, the exception occurs after the UserDeletingRow event, and therefore is not captured by the try/catch block in the first event handler. Also note that the exception happens for both grids, but only after the UserDeletingRow event, not after the context menu event handler event. Any suggestions on how to track this down better (get a stack trace, trap the exception, etc.), or any suggestions on how to fix the problem would be greatly appreciated! The relevant event handlers and associated methods are below: private void dataGridViewVocabProperties_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { Vocabulary selectedVocabItem = bindingSourceVocabulary.Current as Vocabulary; VocabularyPropertyListItem propertyItem = dataGridViewVocabProperties.Rows[e.Row.Index].DataBoundItem as VocabularyPropertyListItem; try { DialogResult result = DeleteVocabularyProperty(selectedVocabItem, propertyItem.PropertyName, String.Format(DELETE_ASSOCIATION_PROPERTY_FORMAT_STRING, propertyItem.PropertyName, selectedVocabItem.Term), DELETE_VOCABULARY_PROPERTY_CAPTION); if (result == DialogResult.No) { e.Cancel = true; } } catch (Exception ex) { int i = 1; } } private void dataGridViewAssociationProperties_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { Vocabulary selectedVocabItem = bindingSourceVocabulary.Current as Vocabulary; AssociationPropertyListItem propertyItem = dataGridViewAssociationProperties.Rows[e.Row.Index].DataBoundItem as AssociationPropertyListItem; AssociationListItem associationItem = bindingSourceAssociations.Current as AssociationListItem; DialogResult result = DeleteAssociationProperty(selectedVocabItem, associationItem.AssociationServiceObject.GUID, propertyItem.PropertyName, String.Format(DELETE_ASSOCIATION_PROPERTY_FORMAT_STRING, propertyItem.PropertyName, selectedVocabItem.Term), DELETE_VOCABULARY_PROPERTY_CAPTION); if (result == DialogResult.No) { e.Cancel = true; } } private DialogResult DeleteVocabularyProperty(Vocabulary selectedVocabItem, string propertyName, string promptMessage, string promptCaption) { DialogResult result = MessageBox.Show(promptMessage, promptCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Vocabulary currentObj = GetCompleteVocabFromLocalCacheOrLoadFromService(selectedVocabItem.GUID); try { Q6.ProblemDomain.Q6ServicesDAO.DeleteVocabularyProperty(currentObj.GUID, propertyName); Vocabulary updatedVocab = GetCompleteVocabFromServiceAndUpdateLocalCache(currentObj.GUID); RefreshVocabularyPropertiesBindingSource(updatedVocab); SetBindingSourcesForSelectedVocabulary(); MessageBox.Show( String.Format("The vocabulary property for the [{0}] vocabulary item was successfully deleted.", updatedVocab.Term) + Environment.NewLine + String.Format("GUID: {0}", updatedVocab.GUID), "Success"); } catch (Exception ex) { MessageBox.Show("An error has occurred: " + ex.Message, "Exception"); } finally { this.Cursor = Cursors.Default; } } return result; } private DialogResult DeleteAssociationProperty(Vocabulary selectedVocabItem, string associationGUID, string propertyName, string promptMessage, string promptCaption) { DialogResult result = MessageBox.Show(promptMessage, promptCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Vocabulary currentObj = GetCompleteVocabFromLocalCacheOrLoadFromService(selectedVocabItem.GUID); try { Q6.ProblemDomain.Q6ServicesDAO.DeleteAssociationProperty(associationGUID, propertyName); Vocabulary updatedVocab = GetCompleteVocabFromServiceAndUpdateLocalCache(currentObj.GUID); RefreshAssociationsBindingSource(updatedVocab); MessageBox.Show("The association property for the selected association was successfully deleted." + Environment.NewLine + String.Format("GUID: {0}", associationGUID), "Success"); } catch (Exception ex) { MessageBox.Show("An error has occurred: " + ex.Message, "Exception"); } finally { this.Cursor = Cursors.Default; } } return result; } private void deletePropertyToolStripMenuItem_Click(object sender, EventArgs e) { ToolStripMenuItem item = sender as ToolStripMenuItem; ContextMenuStrip menu = item.Owner as ContextMenuStrip; DataGridView grid = menu.SourceControl as DataGridView; Vocabulary selectedVocabItem = bindingSourceVocabulary.Current as Vocabulary; string promptMessage; string promptCaption; if (grid.Name == dataGridViewVocabProperties.Name) { VocabularyPropertyListItem propertyItem = dataGridViewVocabProperties.Rows[lastPropertyGridRowIndex].DataBoundItem as VocabularyPropertyListItem; promptMessage = String.Format(DELETE_VOCABULARY_PROPERTY_FORMAT_STRING, propertyItem.PropertyName, selectedVocabItem.Term); promptCaption = DELETE_VOCABULARY_PROPERTY_CAPTION; DeleteVocabularyProperty(selectedVocabItem, propertyItem.PropertyName, promptMessage, promptCaption); } else if (grid.Name == dataGridViewAssociationProperties.Name) { AssociationPropertyListItem propertyItem = dataGridViewAssociationProperties.Rows[lastPropertyGridRowIndex].DataBoundItem as AssociationPropertyListItem; promptMessage = String.Format(DELETE_ASSOCIATION_PROPERTY_FORMAT_STRING, propertyItem.PropertyName); promptCaption = DELETE_ASSOCIATION_PROPERTY_CAPTION; AssociationListItem associationItem = bindingSourceAssociations.Current as AssociationListItem; DeleteAssociationProperty(selectedVocabItem, associationItem.AssociationServiceObject.GUID, propertyItem.PropertyName, promptMessage, promptCaption); } else { throw new Exception("Invalid sender encountered: " + sender.ToString()); } } private void SetBindingSourcesForSelectedVocabulary() { Vocabulary selectedVocab = bindingSourceVocabulary.Current as Vocabulary; Vocabulary completeVocab = GetCompleteVocabFromLocalCacheOrLoadFromService(selectedVocab.GUID); SetBindingSourcesForGivenVocabulary(completeVocab); } private void RefreshVocabularyPropertiesBindingSource(Vocabulary vocab) { bindingSourceVocabularyProperties.DataSource = new SortableBindingList<VocabularyPropertyListItem>(VocabularyPropertyListItem.GetVocabularyPropertyListItem(vocab.Properties)); } private void RefreshAssociationsBindingSource(Vocabulary vocab) { bindingSourceAssociations.DataSource = new SortableBindingList<AssociationListItem>(AssociationListItem.GetAssociationListItemList(vocab.Associations)); } private void SetBindingSourcesForGivenVocabulary(Vocabulary vocab) { txtUpdateVocabTerm.Text = vocab.Term; lblUpdateVocabGUIDValue.Text = vocab.GUID; bindingSourceAssociations.DataSource = new SortableBindingList<AssociationListItem>(AssociationListItem.GetAssociationListItemList(vocab.Associations)); RefreshVocabularyPropertiesBindingSource(vocab); SetAssociationPropertiesBindingSource(); ClearFactTable(); } |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Exception stack is corrupt when catching and storing the exception | .NET General | |||
| Vista Install Error: Exception Unknown Software Exception | Vista installation & setup | |||