<my:DataGridCheckBoxColumn Header="選中" x:Name="checkBox" IsReadOnly="False" CanUserResize="False" Width="35" /> private void btnDeleteNote_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < this.dgvNotesManage.Items.Count; i++) { var cntr = dgvNotesManage.ItemContainerGenerator.ContainerFromIndex(i); DataGridRow ObjROw = (DataGridRow)cntr; if (ObjROw != null) { FrameworkElement objElement = dgvNotesManage.Columns[0].GetCellContent(ObjROw); if (objElement != null) { if (objElement.GetType().ToString().EndsWith("CheckBox")) { System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement; if (objChk.IsChecked == true) { //if (System.Windows.MessageBox.Show("確認刪除此公告?", "確認公告刪除", MessageBoxButton.YesNo) == MessageBoxResult.Yes) //{ string noteId = ((DataRowView)this.dgvNotesManage.Items[i])["NoteId"].ToString(); string sql = string.Format("DELETE FROM OODBS_NoteUser WHERE NoteId='{0}'", noteId); bool flag2 = new CommunicationShare.Proxy.ProxyClient().DeleteNoteObject(sql); sql = string.Format("DELETE FROM OODBS_Notes WHERE NoteId='{0}'", noteId); bool flag1 = new CommunicationShare.Proxy.ProxyClient().DeleteNote(sql); } } } } } BindNoteManage(); } //單擊某行即選中那行的CheckBox private void dgvNotesManage_SelectionChanged(object sender, SelectionChangedEventArgs e) { var cntr = dgvNotesManage.ItemContainerGenerator.ContainerFromIndex(dgvNotesManage.SelectedIndex); DataGridRow ObjROw = (DataGridRow)cntr; if (ObjROw != null) { FrameworkElement objElement = dgvNotesManage.Columns[0].GetCellContent(ObjROw); if (objElement != null) { if (objElement.GetType().ToString().EndsWith("CheckBox")) { System.Windows.Controls.CheckBox objChk = (System.Windows.Controls.CheckBox)objElement; if (objChk.IsChecked == true) { objChk.IsChecked = false; } else objChk.IsChecked = true; } } } }