private void btnOk_Click(object sender, EventArgs e) { if (excelReader != null) { var inds = new HashSet <int>(); foreach (DataGridViewColumn col in dgvExcelInput.SelectedColumns) { inds.Add(col.Index); } ExcelData = ((DataTable)dgvExcelInput.DataSource).Copy(); for (int i = ExcelData.Columns.Count - 1; i >= 0; --i) { if (!inds.Contains(i)) { ExcelData.Columns.RemoveAt(i); } } ExcelData.ReplaceAllOccurrences(DBNull.Value, 0); ExcelData.AcceptChanges(); } }