DataGridView批量删除
DataGridViewSelectedRowCollection selectedRowCollection = this.dgdWorkUnit.DataGrid.SelectedRows; List<DataGridViewRow> deleteViewRowList = new List<DataGridViewRow>(); if (selectedRowCollection.Count > 0) { //选中的是整行 deleteViewRowList.AddRange(selectedRowCollection.Cast<DataGridViewRow>()); } if (!deleteViewRowList.Any()) { //选中的是单元格 DataGridViewSelectedCellCollection selectedCellCollection = this.dgdWorkUnit.DataGrid.SelectedCells; if (selectedCellCollection.Count > 0) { deleteViewRowList = (from DataGridViewCell dataGridViewCell in selectedCellCollection select dataGridViewCell.OwningRow).Distinct().ToList(); } }
此时,只需要判断deleteViewRowList是否为空,如果为空,表示没有选中任务行或者单元格。之所以这样判断是因为用户在选择的时候,有可能选择行,也有可能选择单元格。

浙公网安备 33010602011771号