public static void Colorize(DataGridViewSelectedCellCollection cells, FollowUpMethod method, Color[] theme) { Color color_to_use = new Color(); switch (method) { case FollowUpMethod.Call: color_to_use = theme[0]; break; case FollowUpMethod.Email: color_to_use = theme[1]; break; case FollowUpMethod.Sample: color_to_use = theme[2]; break; case FollowUpMethod.None: color_to_use = Color.White; break; default: color_to_use = Color.White; break; } foreach (DataGridViewCell cell in cells) { cell.Style.BackColor = color_to_use; } }
/** * 선택한셀에 공통된 정보를 채울때 * * */ public void setSelectedCellsValue(DataGridView grid, string value) { DataGridViewSelectedCellCollection cells = grid.SelectedCells; foreach (DataGridViewCell cell in cells) { if (cell.ColumnIndex == 2) { if (value == null) { cell.Selected = false; } else if (value.Equals("1")) { cell.Selected = true; } else { cell.Selected = false; } } else { cell.Value = value; } } }
private void GetResult(int i) { DataGridViewSelectedCellCollection sc = this.dataGridView1.SelectedCells; List <decimal> ListD = new List <decimal>(); foreach (DataGridViewCell r in sc) { decimal d = 0m; if (r.Value == null) { r.Value = 0.0m; } bool b = Decimal.TryParse(r.Value.ToString(), out d); if (!b) { MessageBox.Show("您所选择的单元格含有非数字,请取消其选择,谢谢!"); return; } ListD.Add(d); } decimal result = i == 0 ? ListD.Sum() : i == 1 ? ListD.Average() : ListD.Count; MessageBox.Show("统计结果是:" + result.ToString("F4")); }
private void callAreaBatchUpdatebtn_Click(object sender, EventArgs e) { if (callAreatabControl.SelectedIndex == 0) { dialog.MessageBox.Show(GlobalData.GlobalLanguage.zone_cannot_batch); } else { BatchUpdateForm form = new BatchUpdateForm(); DialogResult dr = form.ShowDialog(); if (dr == DialogResult.OK) { DataGridViewSelectedCellCollection collection = callNumdataGridView.SelectedCells; for (int i = 0; i < collection.Count; i = i + 3) { DataGridViewCell cell = collection[i]; Caller caller = InitData.list_caller[cell.RowIndex]; Caller tem = new Caller(); tem.ID = caller.ID; tem.callerNum = caller.callerNum; tem.callZone = caller.callZone; tem.employeeNum = employee.employeeNum; szwlForm.mainForm.dm.updateCaller(tem); } InitData.list_caller = szwlForm.mainForm.dm.selectCaller(); callNumdataGridView.DataSource = null; callNumdataGridView.DataSource = InitData.list_caller; dialog.MessageBox.Show(GlobalData.GlobalLanguage.update_success); } } }
/* * Event handler for copy. */ public void CopyEvent() { if (dataGridView.SelectedCells.Count == 0) { return; } string encoded = ""; DataGridViewSelectedCellCollection cells = dataGridView.SelectedCells; List <List <DataGridViewCell> > selected = SelectedCells(cells); for (int rowNum = 0; rowNum < selected.Count; rowNum++) { List <DataGridViewCell> row = selected[rowNum]; string line = ""; for (int colNum = 0; colNum < row.Count; colNum++) { line += row[colNum].Value + "\t"; } line.Remove(line.LastIndexOf("\t")); encoded += line + "\n"; } Clipboard.SetText(encoded); if (encoded.Length > 2 && Copied != null) { Copied(); } }
private void selectionChanged(object sender, EventArgs e) { /* * Check to see if the user has selected all the letters positions allowing for the word to * be found */ DataGridViewSelectedCellCollection cells = this.wordSearch.SelectedCells; if (cells.Count <= 0 || this.locationsOfLetters == null) { return; } //Points selected List <Point> pointsSelected = new List <Point>(); foreach (DataGridViewTextBoxCell cell in cells) { pointsSelected.Add(new Point(cell.ColumnIndex, cell.RowIndex)); } //If the letters positions are the same as the selected positions if (pointsSelected.All(this.locationsOfLetters.Contains) && pointsSelected.Count == this.locationsOfLetters.Count) { this.label1.Text = "Found word"; } else { this.label1.Text = "Not found word yet"; } }
private void gridView_KeyUp(object sender, KeyEventArgs e) { var grid = ((DataGridView)sender); DataGridViewSelectedCellCollection activeCells = grid.SelectedCells; if (_rowDeleted) { _rowDeleted = false; return; } if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back) { foreach (DataGridViewCell activeCell in activeCells) { //_editedCellValue = activeCell.Value.ToString(); if (activeCell == null) { return; } string oldValue = activeCell.Value.ToString(); Color oldColor = activeCell.Style.BackColor; _ignoreForHistory = true; activeCell.Value = ""; // gridView_CellValueChanged(sender, new DataGridViewCellEventArgs(activeCell.ColumnIndex, activeCell.RowIndex)); AddNewEditHistory(_gridUi.tabControl1.SelectedTab.Text, activeCell, oldValue, activeCell.Value.ToString(), oldColor); } ((DataGridView)sender).BeginEdit(false); } }
// 一番左上のセルを返す // 矩形選択に対応、Ctrl押しながらの複数選択は適当に対応 public static DataGridViewCell SelectMostTopLeftCell(DataGridViewSelectedCellCollection dataGridViewSelectedCellCollection) { int?minRow = null; int?minColumn = null; DataGridViewCell resultCell = null; foreach (DataGridViewCell cell in dataGridViewSelectedCellCollection) { if (minRow == null || cell.RowIndex < minRow) { minRow = cell.RowIndex; } } foreach (DataGridViewCell cell in dataGridViewSelectedCellCollection) { if (minRow == cell.RowIndex) { if (minColumn == null || cell.ColumnIndex < minColumn) { minColumn = cell.ColumnIndex; resultCell = cell; } } } return(resultCell); }
// Инициализыция формы public FormRowStatistic(string table, DataGridViewSelectedCellCollection selectedCells) { InitializeComponent(); using (var context = new AquariumContext()) { int key = int.Parse(selectedCells[0].Value.ToString()); switch (table) { case "Fish": DataGridView.DataSource = context.Statistics.Where(s => s.FishId == key).ToList(); break; case "Plant": DataGridView.DataSource = context.Statistics.Where(s => s.PlantId == key).ToList(); break; } DataGridView.Columns["Fish"].Visible = false; DataGridView.Columns["Plant"].Visible = false; DataGridView.Columns["Plant"].Visible = false; DataGridView.Columns["Id"].HeaderText = "№"; DataGridView.Columns["DateTime"].HeaderText = "Дата/Время"; DataGridView.Columns["LivingСonditions"].HeaderText = "Условия обитания"; DataGridView.Columns["Nutrition"].HeaderText = "Питание"; DataGridView.Columns["Population"].HeaderText = "Популяция"; DataGridView.Columns["FishId"].Visible = false; DataGridView.Columns["PlantId"].Visible = false; DataGridView.Refresh(); } }
private void deleteItemButton_Click(object sender, EventArgs e) { DataGridViewSelectedCellCollection cellCollection = orderGridView.SelectedCells; if (cellCollection.Count != 1) { MessageBox.Show("برجاء اختيار صنف واحد لمسحه"); return; } var selectedProduct = cellCollection[0].Value.ToString(); foreach (var order in salaManager.liveOrders) { if (order.tarabyza == selectedTarabyza) { foreach (var item in order.drinks) { if (item.Key == selectedProduct) { Order tempOrder = order; salaManager.liveOrders.Remove(order); tempOrder.drinks.Remove(selectedProduct); salaManager.liveOrders.Add(tempOrder); orderGridView.Rows.Clear(); reloadOrderData(); return; } } } } }
/// <summary> /// Invoked when the state of a data-grid cell has changed. /// </summary> private void dataGrid_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e) { DataGridViewSelectedCellCollection selectedCells = this.dataGrid.SelectedCells; TableListEntry entry = this.tableList.SelectedItem as TableListEntry; if (entry.Table.IsReadOnly) { this.smoothButton.Enabled = false; } else { /* DataGridViewCellStyle style = Util.DefaultStyle; * style.BackColor = Color.Black; * style.ForeColor = Color.White; * * foreach (DataGridViewCell cell in this.dataGrid.SelectedCells) * { * cell.Style = style; * } */ if (this.Smooth(selectedCells, false)) { this.smoothButton.Enabled = true; } else { this.smoothButton.Enabled = false; } } }
private static DataGridViewCell GetTopLeftSelectedCell(DataGridViewSelectedCellCollection dataGridViewSelectedCellCollection) { int leftTopIndex = 0; int leftMostIndex = 100; int topMostIndex = 100; for (int i = 0; i < dataGridViewSelectedCellCollection.Count; i++) { int currentCol = dataGridViewSelectedCellCollection[i].ColumnIndex; int currentRow = dataGridViewSelectedCellCollection[i].RowIndex; if (currentCol == leftMostIndex) { if (currentRow < topMostIndex) { topMostIndex = currentRow; leftTopIndex = i; } } if (currentCol < leftMostIndex) { leftTopIndex = currentCol; topMostIndex = currentRow; leftTopIndex = i; } } return(dataGridViewSelectedCellCollection[leftTopIndex]); }
private static DataGridViewCell GetBottomRightSelectedCell(DataGridViewSelectedCellCollection dataGridViewSelectedCellCollection) { int rightBottomIndex = 0; int rightMostIndex = 100; int bottomMostIndex = 100; for (int i = 0; i < dataGridViewSelectedCellCollection.Count; i++) { int currentCol = dataGridViewSelectedCellCollection[i].ColumnIndex; int currentRow = dataGridViewSelectedCellCollection[i].RowIndex; if (currentCol == rightMostIndex) { if (currentRow > bottomMostIndex) { bottomMostIndex = currentRow; rightBottomIndex = i; } } if (currentCol > rightMostIndex) { rightBottomIndex = currentCol; bottomMostIndex = currentRow; rightBottomIndex = i; } } return(dataGridViewSelectedCellCollection[rightBottomIndex]); }
public static IEnumerable <DataGridViewCell> AsEnumerable(this DataGridViewSelectedCellCollection cells) { for (int i = 0; i < cells.Count; i++) { yield return(cells[i]); } }
private bool Smooth(DataGridViewSelectedCellCollection selectedCells, bool forReal) { bool ret = false; if (this.smoothComboBox.SelectedIndex == 0 || this.smoothComboBox.SelectedIndex == 1) { if (this.SelectedRow(selectedCells)) { if (forReal) { IList <DataGridViewCell> cells = this.SortCellsByRow(selectedCells); this.SmoothHorizontal(cells); } ret = true; } } if (this.smoothComboBox.SelectedIndex == 0 || this.smoothComboBox.SelectedIndex == 2) { if (this.SelectedColumn(selectedCells)) { if (forReal) { IList <DataGridViewCell> cells = this.SortCellsByColumn(selectedCells); this.SmoothVertical(cells); } ret = true; } } return(ret); }
public void DataGridViewSelectedCellCollection_Contains_InvokeEmpty_ReturnsFalse(DataGridViewCell dataGridViewCell) { using var control = new DataGridView(); DataGridViewSelectedCellCollection collection = control.SelectedCells; Assert.False(collection.Contains(dataGridViewCell)); }
private List <DataGridViewCell> SortCellsByColumn(DataGridViewSelectedCellCollection input) { List <DataGridViewCell> result = new List <DataGridViewCell>(); foreach (DataGridViewCell cell in input) { if (cell == null) { continue; } result.Add(cell); } result.Sort(delegate(DataGridViewCell a, DataGridViewCell b) { if (a.ColumnIndex < b.ColumnIndex) { return(-1); } else if (a.ColumnIndex == b.ColumnIndex) { if (a.RowIndex < b.RowIndex) { return(-1); } else if (a.RowIndex == b.RowIndex) { return(0); } } return(1); }); return(result); }
public void DataGridViewSelectedCellCollection_Insert_Invoke_ThrowsNotSupportedException(int index, DataGridViewCell dataGridViewCell) { using var control = new DataGridView(); DataGridViewSelectedCellCollection collection = control.SelectedCells; Assert.Throws <NotSupportedException>(() => collection.Insert(index, dataGridViewCell)); }
private void buttonRemove_Click(object sender, EventArgs e) { if (dataGridViewRR.Rows.Count == 0) { return; } DataGridViewSelectedCellCollection sel = dataGridViewRR.SelectedCells; if (sel.Count == 0) { return; } foreach (DataGridViewRow row in dataGridViewRR.Rows) { if (row.Cells[0].Value == null && (row.Index == dataGridViewRR.CurrentCell.RowIndex)) { dataGridViewRR.Rows.Remove(row); } else if (row.Index == dataGridViewRR.CurrentCell.RowIndex) { if (row.Cells[0].Value != null && row.Cells[0].Value.ToString().ToLowerInvariant().IndexOf("tv") > -1) { continue; } defaultHz.Items.Remove(row.Cells[0].Value); dataGridViewRR.Rows.Remove(dataGridViewRR.Rows[dataGridViewRR.CurrentCell.RowIndex]); } } }
public void DataGridViewSelectedCellCollection_IListGetEnumerator_InvokeNotEmpty_Success() { using var control = new DataGridView { RowCount = 3, ColumnCount = 1, SelectionMode = DataGridViewSelectionMode.CellSelect }; control.Rows[0].Cells[0].Selected = true; control.Rows[1].Cells[0].Selected = false; control.Rows[2].Cells[0].Selected = true; DataGridViewSelectedCellCollection collection = control.SelectedCells; IList iList = collection; IEnumerator enumerator = iList.GetEnumerator(); for (int i = 0; i < 2; i++) { Assert.Throws <InvalidOperationException>(() => enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Same(control.Rows[2].Cells[0], enumerator.Current); Assert.True(enumerator.MoveNext()); Assert.Same(control.Rows[0].Cells[0], enumerator.Current); Assert.False(enumerator.MoveNext()); Assert.Throws <InvalidOperationException>(() => enumerator.Current); enumerator.Reset(); Assert.Throws <InvalidOperationException>(() => enumerator.Current); } }
private void dataGridView1_SelectionChanged(object sender, EventArgs e) { DataGridViewSelectedCellCollection cells = dataGridView1.SelectedCells; foreach (DataGridViewCell cell in cells) { int rowidx = cell.RowIndex; int colidx = cell.ColumnIndex; if (colidx > 0) { dataGridView1.Rows[rowidx].Cells[colidx].Style.Format = "F0"; } } for (int a = 0; a < dataGridView1.Rows.Count; a++) { for (int b = 3; b < dataGridView1.Columns.Count; b++) { if (!cells.Contains(dataGridView1.Rows[a].Cells[b])) { Int64 buf; dataGridView1.Rows[a].Cells[b].Style.Format = "C0"; dataGridView1.Rows[a].Cells[b].Style.FormatProvider = CultureInfo.GetCultureInfo("id-ID"); } } } }
public void DataGridViewSelectedCellCollection_Item_GetInvalidIndexEmpty_ThrowsArgumentOutOfRangeException(int index) { using var control = new DataGridView(); DataGridViewSelectedCellCollection collection = control.SelectedCells; Assert.Throws <ArgumentOutOfRangeException>("index", () => collection[index]); }
/* * Retrieve the cells in the given collection of selected cells as a two-dimensional data structure * (rows/columns). */ List <List <DataGridViewCell> > SelectedCells(DataGridViewSelectedCellCollection collection) { List <List <DataGridViewCell> > rows = new List <List <DataGridViewCell> >(); foreach (DataGridViewCell cell in collection) { int rowIndex = cell.RowIndex; while (rowIndex >= rows.Count) { rows.Add(new List <DataGridViewCell>()); } List <DataGridViewCell> addTo = rows[rowIndex]; while (cell.ColumnIndex >= addTo.Count) { addTo.Add(null); } addTo[cell.ColumnIndex] = cell; } List <List <DataGridViewCell> > result = new List <List <DataGridViewCell> >(); rows.ForEach(row => { if (row.Count > 0) { List <DataGridViewCell> newRow = new List <DataGridViewCell>(); row.ForEach(cell => { if (cell != null) { newRow.Add(cell); } }); result.Add(newRow); } }); return(result); }
public void DataGridViewSelectedCellCollection_Clear_Invoke_ThrowsNotSupportedException() { using var control = new DataGridView(); DataGridViewSelectedCellCollection collection = control.SelectedCells; Assert.Throws <NotSupportedException>(() => collection.Clear()); }
/// <summary> /// 获取表格中选定单元格的数据 /// </summary> /// <param name="isSelectAll">表格</param> /// <param name="isSelectAll">是否选中全部</param> /// <returns></returns> public static string getDatatabelSelText(DataGridView view, bool isSelectAll) { string tableText = ""; if (view != null) { // 选定的单元格集合 DataGridViewSelectedCellCollection selCell = view.SelectedCells; if (isSelectAll) { view.SelectAll(); } tableText = view.GetClipboardContent() != null?view.GetClipboardContent().GetText() : ""; // 还原 if (isSelectAll) { view.ClearSelection(); foreach (DataGridViewCell cell in selCell) { cell.Selected = true; } } } return(tableText); }
public List <int> GetDelSelectedRowsIndexList() { List <int> indexList = new List <int>(); switch (dataGridView.SelectionMode) { default: case DataGridViewSelectionMode.CellSelect: DataGridViewSelectedCellCollection cells = dataGridView.SelectedCells; foreach (DataGridViewCell cell in cells) { int index = GetDataSourceRowIndexFromDataGridViewRow(cell.OwningRow); if (!indexList.Contains(index)) { indexList.Add(index); } } break; case DataGridViewSelectionMode.FullRowSelect: DataGridViewSelectedRowCollection rows = dataGridView.SelectedRows; foreach (DataGridViewRow row in rows) { int index = GetDataSourceRowIndexFromDataGridViewRow(row); indexList.Add(index); } break; } return(indexList); }
private void addGridVIew_CellContentClick(object sender, DataGridViewCellEventArgs e) { DataGridViewSelectedCellCollection cell = addGridVIew.SelectedCells; // addname.Text = cell[0].Value.ToString; // addvolume.Text = row[0].Cells["flavour_unit"].Value.ToString(); }
/// <summary> /// Recompute selected points /// Required: selected point must have calculation /// </summary> public void Undo() { DataGridViewSelectedCellCollection x = timeSeriesSpreadsheet1.GetSelectedCells(); try { foreach (DataGridViewCell cell in x) { if (cell.ColumnIndex != 0) // not date column { DataRow row = ((DataRowView)cell.OwningRow.DataBoundItem).Row; MultipleSeriesDataTable tbl = row.Table as MultipleSeriesDataTable; DateTime t = Convert.ToDateTime(row[0]); string interval = tbl.TableName; HdbPoet.TimeSeriesDataSet.SeriesRow s = tbl.LookupSeries(cell.ColumnIndex); if (s.IsComputed) { Hdb.Instance.Calculate_Series(s.hdb_site_datatype_id, interval, t, dataSet.GraphRow.TimeZone); } else { Logger.WriteLine("Warning series is not computed " + s.SiteName + " " + s.hdb_site_datatype_id); } } } } catch (OracleException ex) { MessageBox.Show(ex.Message); } }
public static IEnumerable <T> Select <T>(this DataGridViewSelectedCellCollection cells, Func <DataGridViewCell, T> func) { foreach (DataGridViewCell c in cells) { yield return(func(c)); } }
void ansDelete_Click(object sender, EventArgs e) { if (this.EditMode == Mode.New) { if (dgvDetailList.SelectedCells.Count > 0) { this.dgvDetailList.Rows.RemoveAt(dgvDetailList.CurrentCell.RowIndex); if (dgvDetailList.Rows.Count <= 0) { this.ansDetails.Items.Find("ansDelete", true)[0].Enabled = false; } } } else { DataGridViewSelectedCellCollection itemList = dgvDetailList.SelectedCells; foreach (DataGridViewCell item in itemList) { DataGridViewRow row = dgvDetailList.Rows[item.RowIndex]; if (DAL.Common.Utility.IsNumeric(row.Cells[8].Value.ToString())) { row.Cells[2].Value = row.Cells[2].Value.ToString().Trim().Length == 0 ? "REMOVED" : row.Cells[2].Value; } } } }