private void updateButton_Click(object sender, RoutedEventArgs e) { int selectedIndex = dataGrid.SelectedIndex; if (selectedIndex < 0 || selectedIndex >= dataGrid.Items.Count) { return; } if (instanceTextBox.Text.Equals("")) { MessageBox.Show("Please set Instance"); return; } if (sourceTextBox.Text.Equals("")) { MessageBox.Show("Please set Source Path"); return; } if (destinationTextBox.Text.Equals("")) { MessageBox.Show("Please set Destination Path"); return; } bool instanceIsRepeat = false, sourceIsRepeat = false; for (int i = 0; i < dataGrid.Items.Count; i++) { if (i == selectedIndex) { continue; } Rule rule = (Rule)dataGrid.Items[i]; if (instanceTextBox.Text.Equals(rule.Instance)) { instanceIsRepeat = true; break; } if (sourceTextBox.Text.Equals(rule.Source)) { sourceIsRepeat = true; break; } } if (instanceIsRepeat) { MessageBox.Show("Please set OTHER Instance"); instanceTextBox.Clear(); return; } if (sourceIsRepeat) { MessageBox.Show("Please set OTHER Source Path"); sourceTextBox.Clear(); return; } MessageBoxResult messageBoxResult = MessageBox.Show("Do you want to change the selected Rule?", "Question", MessageBoxButton.YesNo); if (messageBoxResult == MessageBoxResult.No) { return; } dataGrid.Items[selectedIndex] = new Rule() { Instance = instanceTextBox.Text, Source = sourceTextBox.Text, Destination = destinationTextBox.Text }; dataGrid.SelectedIndex = -1; saveButton.IsEnabled = true; }