/// <summary> /// 返回选中行按升序排序的行号列表 /// </summary> /// <param name="dgv"></param> /// <returns></returns> public static List <int> GetSelectRowIndex(TSDataGridView dgv) { List <int> selectedIndex = new List <int>(); int cbColIndex = GetCheckBoxColumnIndex(dgv); if (cbColIndex < 0) { return(selectedIndex); } foreach (DataGridViewRow row in dgv.Rows) { bool isChecked = (bool)row.Cells[cbColIndex].EditedFormattedValue; if (!isChecked) { continue; } selectedIndex.Add(row.Index); } selectedIndex.Sort(); return(selectedIndex); }
private void DataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0 || e.ColumnIndex < 0) { return; } TSDataGridView dgv = sender as TSDataGridView; if (dgv == null) { return; } var cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; var val = cell.Value; if (CellEndEditHandler != null) { CellEndEditHandler(e.RowIndex, e.ColumnIndex, dgv.Columns[e.ColumnIndex].Name); } if (_dgvComboBox != null) { _dgvComboBox.SelectionChangeCommitted -= new System.EventHandler(DataGridViewComboBox_SelectionChangeCommitted); _dgvComboBox = null; } DataGridViewColumn column = dgv.Columns[e.ColumnIndex]; if (column is DataGridViewComboBoxColumn) { string cbSource = string.Format("{0}_{1}", column.Name, "source"); DataGridViewColumn srccolumn = dgv.Columns[cbSource]; if (srccolumn != null) { var cellSource = dgv.Rows[e.RowIndex].Cells[srccolumn.Name]; ComboOption cbOption = (ComboOption)cellSource.Value; if (cbOption != null) { var theCell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; var cbCell = (DataGridViewComboBoxCell)theCell; if (cbCell != null) { //cbCell.DataSource = cbOption.Items; //cbCell.DisplayMember = "Name"; //cbCell.ValueMember = "Id"; cbCell.Items.Clear(); cbCell.Items.AddRange(cbOption.Items); cbCell.DisplayMember = "Name"; cbCell.ValueMember = "Id"; } } } } }
private static void EditingControlShowing(TSDataGridView dgv, DataGridViewEditingControlShowingEventArgs e) { if (dgv == null) { return; } if (dgv.CurrentCell.ColumnIndex == dgv.Columns["copies"].Index) { DataGridViewTextBoxEditingControl dgvTxt = e.Control as DataGridViewTextBoxEditingControl; dgvTxt.SelectAll(); dgvTxt.KeyPress += new KeyPressEventHandler(Cells_KeyPress); } }
public static void SetDataBinding(TSDataGridView dgv, Dictionary <string, string> colDataMap) { if (dgv == null) { throw new ArgumentNullException("dgv"); } foreach (DataGridViewColumn column in dgv.Columns) { if (colDataMap.ContainsKey(column.Name)) { column.DataPropertyName = colDataMap[column.Name]; } } }
//public static void CellMouseClick(TSDataGridView dgv, DataGridViewCellMouseEventArgs e) //{ // if (dgv == null || e.ColumnIndex < 0 || e.RowIndex < 0) // return; // if (dgv.Columns["copies"] != null) // { // int copiesIndex = dgv.Columns["copies"].Index; // DataGridViewRow row = dgv.Rows[e.RowIndex]; // switch (dgv.Columns[e.ColumnIndex].Name) // { // case "plus": // { // int oldValue = int.Parse(row.Cells["copies"].Value.ToString()); // if (oldValue < MAX_ENTRUST_AMOUNT) // { // row.Cells["copies"].Value = oldValue + 1; // if (dgv.ClickRow != null) // { // } // } // else // { // //invalid input // } // } // break; // case "minus": // { // int oldValue = int.Parse(row.Cells["copies"].Value.ToString()); // if (oldValue > 1) // { // row.Cells["copies"].Value = oldValue - 1; // } // else // { // //invalid input // } // } // break; // } // } //} #endregion #endregion //#region set data //public static void SetData(TSDataGridView dgv, HSGrid hsGrid, DataTable dataTable) //{ // foreach (DataRow dataRow in dataTable.Rows) // { // SetRow(dgv, hsGrid, dataRow, dataTable.ColumnIndex); // } //} //public static void SetRow(TSDataGridView dgv, HSGrid hsGrid, Model.Data.DataRow dataRow, Dictionary<string, int> colIndexMap) //{ // int rowIndex = dgv.Rows.Add(); // DataGridViewRow row = dgv.Rows[rowIndex]; // SetRow(dgv, hsGrid, row, dataRow, colIndexMap); //} //private static void SetRow(TSDataGridView dgv, HSGrid hsGrid, DataGridViewRow row, Model.Data.DataRow dataRow, Dictionary<string, int> colIndexMap) //{ // bool isSelected = false; // List<HSGridColumn> columns = hsGrid.Columns; // foreach (HSGridColumn col in columns) // { // int index = -1; // if (colIndexMap.ContainsKey(col.Name)) // { // index = colIndexMap[col.Name]; // } // if (index < 0 || index >= dataRow.Columns.Count) // continue; // Model.Data.DataValue dataValue = dataRow.Columns[index]; // switch (col.ColumnType) // { // case HSGridColumnType.CheckBox: // { // int targetValue = dataValue.GetInt(); // isSelected = targetValue > 0 ? true : false; // row.Cells[col.Name].Value = isSelected; // } // break; // case HSGridColumnType.Text: // { // SetDataCell(ref row, col, dataValue); // } // break; // case HSGridColumnType.Image: // { // string imgPath = dataValue.GetStr(); // Image plusImg = Image.FromFile(imgPath); // Bitmap plusBt = new Bitmap(plusImg, new Size(20, 20)); // row.Cells[col.Name].Value = plusBt; // } // break; // default: // break; // } // } //} //private static void SetDataCell(ref DataGridViewRow row, HSGridColumn column, Model.Data.DataValue dataValue) //{ // switch (column.ValueType) // { // case DataValueType.Int: // { // row.Cells[column.Name].Value = dataValue.GetInt(); // } // break; // case DataValueType.Float: // { // row.Cells[column.Name].Value = dataValue.GetDouble(); // } // break; // case DataValueType.Char: // case DataValueType.String: // { // row.Cells[column.Name].Value = dataValue.GetStr(); // } // break; // default: // { // row.Cells[column.Name].Value = dataValue.GetStr(); // } // break; // } //} //#endregion //#region change the row //public void AddRow(TSDataGridView dgv, HSGrid hsGrid, DataTable dataTable, Model.Data.DataRow dataRow, Dictionary<string, int> colIndexMap) //{ // if (dataTable == null) // { // dataTable = new DataTable // { // Rows = new List<DataRow>(), // ColumnIndex = new Dictionary<string, int>() // }; // dataTable.Rows.Add(dataRow); // } // else // { // dataTable.Rows.Add(dataRow); // } // SetRow(dgv, hsGrid, dataRow, dataTable.ColumnIndex); //} //public static bool UpdateRow(TSDataGridView dgv, HSGrid hsGrid, DataTable dataTable, int rowIndex, DataRow dataRow) //{ // if (rowIndex < 0 || rowIndex >= dataTable.Rows.Count) // return false; // if (rowIndex >= dgv.Rows.Count) // return false; // dataTable.Rows[rowIndex] = dataRow; // DataGridViewRow row = dgv.Rows[rowIndex]; // SetRow(dgv, hsGrid, row, dataRow, dataTable.ColumnIndex); // return true; //} //private bool DeleteRow(TSDataGridView dgv, DataTable dataTable, int rowIndex) //{ // if (rowIndex < 0 || rowIndex >= dataTable.Rows.Count) // { // return false; // } // if (rowIndex >= dgv.Rows.Count) // { // return false; // } // dataTable.Rows.RemoveAt(rowIndex); // dgv.Rows.RemoveAt(rowIndex); // return true; //} //#endregion #region select row //public static Model.Data.DataRow GetDataRow(HSGrid hsGrid, DataGridViewRow row, int defSelection) //{ // DataRow dataRow = new DataRow(); // dataRow.Columns = new List<DataValue>(); // var columns = hsGrid.Columns; // for (int i = 0, count = columns.Count; i < count; i++) // { // HSGridColumn column = columns[i]; // if (column.ColumnType == HSGridColumnType.None) // continue; // DataValue dataValue = new DataValue(); // dataValue.Type = column.ValueType; // if (column.ColumnType == HSGridColumnType.CheckBox) // { // dataValue.Value = defSelection; // } // else // { // dataValue.Value = row.Cells[i].Value; // } // dataRow.Columns.Add(dataValue); // } // return dataRow; //} //public static Model.Data.DataTable GetSelectionRows(TSDataGridView dgv, HSGrid hsGrid) //{ // Model.Data.DataTable dataTable = new Model.Data.DataTable(); // dataTable.ColumnIndex = new Dictionary<string, int>(); // dataTable.Rows = new List<DataRow>(); // int cbColIndex = GetCheckBoxColumnIndex(dgv); // if (cbColIndex < 0) // return dataTable; // int validColCount = hsGrid.Columns.Count - 1; // foreach (DataGridViewRow row in dgv.Rows) // { // bool isChecked = (bool)row.Cells[cbColIndex].EditedFormattedValue; // if (!isChecked) // continue; // Model.Data.DataRow dataRow = GetDataRow(hsGrid, row, 1); // dataTable.Rows.Add(dataRow); // } // //TODO:add the column index // foreach (DataGridViewColumn column in dgv.Columns) // { // } // return dataTable; //} private static int GetCheckBoxColumnIndex(TSDataGridView dgv) { int index = -1; for (int i = 0, count = dgv.Columns.Count; i < count; i++) { DataGridViewColumn column = dgv.Columns[i]; if (column is DataGridViewCheckBoxColumn) { index = i; break; } } return(index); }
/// <summary> /// 按下Shift键之后,按住鼠标左键在Checkbox列上拖动多行,实现选中/选出多行功能。 /// 由于该事件直接响应鼠标事件,如果不先按下Shift键则不会触发后续事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { TSDataGridView dgv = sender as TSDataGridView; if (dgv == null) { return; } int columnIndex = e.ColumnIndex; int rowIndex = e.RowIndex; if (columnIndex < 0 || columnIndex > dgv.Columns.Count || rowIndex < 0 || rowIndex > dgv.Rows.Count) { return; } //string msg = string.Format("CellMouseDown - row: {0}, column: {1}", rowIndex, columnIndex); //Trace.WriteLine(msg); int cbIndex = GetCheckBoxColumnIndex(); if (cbIndex < 0) { return; } if (e.Button == System.Windows.Forms.MouseButtons.Left && Control.ModifierKeys == Keys.Shift) { isShiftSelected = true; if (cbIndex == columnIndex) { startRow = e.RowIndex; dgv.CellMouseUp += new DataGridViewCellMouseEventHandler(DataGridView_CellMouseUp); var cell = dgv.Rows[rowIndex].Cells[columnIndex]; if (Convert.ToBoolean(cell.EditedFormattedValue) == false) { isChecked = false; } else { isChecked = true; } } } }
private void DataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (sender == null || e.ColumnIndex < 0 || e.RowIndex < 0) { return; } TSDataGridView dgv = sender as TSDataGridView; if (dgv == null) { return; } if (MouseClickRow != null) { MouseClickRow(this, e.RowIndex); } }
public static void SetDataBinding(TSDataGridView dgv, string cbName, ComboOption cbOption) { if (dgv == null) { throw new ArgumentNullException("dgv"); } foreach (DataGridViewColumn column in dgv.Columns) { if (column.Name.Equals(cbName) && column is DataGridViewComboBoxColumn) { var cbColumn = column as DataGridViewComboBoxColumn; cbColumn.DataSource = cbOption.Items; cbColumn.ValueMember = "Id"; cbColumn.DisplayMember = "Text"; break; } } }
/// <summary> /// 双击单元格时发生该事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) { if (sender == null || e.ColumnIndex < 0 || e.RowIndex < 0) { return; } TSDataGridView dgv = sender as TSDataGridView; if (dgv == null) { return; } //Trace.WriteLine("DataGridView_CellMouseDoubleClick"); if (MouseDoubleClick != null) { MouseDoubleClick(this, e.RowIndex, e.ColumnIndex); } }
private void DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { TSDataGridView dgv = sender as TSDataGridView; int columnIndex = dgv.CurrentCell.ColumnIndex; if (Columns["copies"] != null && columnIndex == Columns["copies"].Index) { e.Control.KeyPress -= CopiesCheck; e.Control.KeyPress += CopiesCheck; } else if (e.Control is DataGridViewComboBoxEditingControl && !Columns[columnIndex].ReadOnly && CurrentCell.RowIndex != -1 ) { _dgvComboBox = (DataGridViewComboBoxEditingControl)e.Control; //add event handler for ComboBox selection _dgvComboBox.SelectionChangeCommitted += new System.EventHandler(DataGridViewComboBox_SelectionChangeCommitted); } }
/// <summary> /// 该事件处理函数在DataGridView_CellMouseDown中注册,并在本事件处理函数中删除注销。 /// 仅当按下Shift键,并在Checkbox列按下鼠标左键,才会触发注册本事件处理函数。 /// 改变Cell值时调用BeginEdit/EndEdit是为了让该值及时生效,也就是EditedFormatValue更新。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataGridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) { TSDataGridView dgv = sender as TSDataGridView; if (dgv == null) { return; } //Trace.WriteLine("DataGridView_CellMouseUp"); int columnIndex = e.ColumnIndex; int rowIndex = e.RowIndex; if (columnIndex < 0 || columnIndex > dgv.Columns.Count || rowIndex < 0 || rowIndex > dgv.Rows.Count) { return; } dgv.CellMouseUp -= DataGridView_CellMouseUp; int cbIndex = GetCheckBoxColumnIndex(); if (cbIndex < 0) { return; } if (startRow < 0 || startRow > dgv.Rows.Count) { return; } if (!isShiftSelected) { return; } bool result = isChecked ? false : true; int endRow = rowIndex; if (endRow < 0 || endRow > dgv.Rows.Count) { return; } if (startRow == endRow) { return; } int start, end; start = startRow < endRow ? startRow : endRow; end = startRow > endRow ? startRow : endRow; for (int i = start; i <= end; i++) { dgv.BeginEdit(false); var row = dgv.Rows[i]; var cell = row.Cells[cbIndex]; cell.Value = result; dgv.EndEdit(); SwitchSelection(row, columnIndex); } isShiftSelected = false; startRow = -1; }
public static void CellFormatting(TSDataGridView dgv, DataGridViewCellFormattingEventArgs e) { int columnIndex = e.ColumnIndex; int rowIndex = e.RowIndex; if (columnIndex < 0 || columnIndex > dgv.Columns.Count || rowIndex < 0 || rowIndex > dgv.Rows.Count) { return; } DataGridViewColumn column = dgv.Columns[e.ColumnIndex]; if (column != null) { if (column is DataGridViewImageColumn) { //e.Value = Image.FromFile((string)column.Tag); Image image = Image.FromFile((string)column.Tag); Bitmap bitmap = new Bitmap(image, new Size(20, 20)); e.Value = bitmap; } else if (dgv.Columns["limitupdown"] != null && columnIndex == dgv.Columns["limitupdown"].Index) { var row = dgv.Rows[rowIndex]; var cell = row.Cells[columnIndex]; if (cell.Value.ToString() == "涨停") { row.DefaultCellStyle.ForeColor = ControlConstVariable.LimitUpColor; e.CellStyle.ForeColor = ControlConstVariable.LimitStatusColumnForeColor; e.CellStyle.BackColor = ControlConstVariable.LimitUpColor; } else if (cell.Value.ToString() == "跌停") { row.DefaultCellStyle.ForeColor = ControlConstVariable.LimitDownColor; e.CellStyle.ForeColor = ControlConstVariable.LimitStatusColumnForeColor; e.CellStyle.BackColor = ControlConstVariable.LimitDownColor; } } else if (dgv.Columns["suspensionflag"] != null && columnIndex == dgv.Columns["suspensionflag"].Index) { var row = dgv.Rows[rowIndex]; var cell = row.Cells[columnIndex]; if (cell.Value.ToString().Contains("停牌")) { row.DefaultCellStyle.ForeColor = ControlConstVariable.LimitSuspensionColor; } } //else if (column is DataGridViewComboBoxColumn) //{ // string cbSource = string.Format("{0}_{1}", column.Name, "source"); // DataGridViewColumn srccolumn = dgv.Columns[cbSource]; // if(srccolumn != null) // { // var cellSource = dgv.Rows[e.RowIndex].Cells[srccolumn.Name]; // ComboOption cbOption = (ComboOption)cellSource.Value; // if (cbOption != null) // { // var cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; // var cbCell = (DataGridViewComboBoxCell)cell; // if (cbCell != null) // { // //cbCell.DataSource = cbOption.Items; // //cbCell.DisplayMember = "Name"; // //cbCell.ValueMember = "Id"; // cbCell.Items.AddRange(cbOption.Items); // cbCell.DisplayMember = "Name"; // cbCell.ValueMember = "Id"; // } // } // } //} } }
public static void AddColumns(TSDataGridView dgv, TSGrid hsGrid) { if (dgv == null) { throw new ArgumentNullException("dgv"); } if (hsGrid == null) { throw new ArgumentNullException("hsGrid"); } List <TSGridColumn> columns = hsGrid.Columns; DataGridViewColumn[] gridColumns = new DataGridViewColumn[columns.Count]; for (int i = 0, count = columns.Count; i < count; i++) { TSGridColumn col = columns[i]; DataGridViewColumn column = null; switch (col.ColumnType) { case TSGridColumnType.CheckBox: { column = new DataGridViewCheckBoxColumn(); } break; case TSGridColumnType.Image: { column = new DataGridViewImageColumn(); column.Tag = col.DefaultValue; } break; case TSGridColumnType.Text: { column = new DataGridViewTextBoxColumn(); } break; case TSGridColumnType.ComboBox: { column = new DataGridViewComboBoxColumn(); } break; case TSGridColumnType.NumericUpDown: { var numericCell = new DataGridViewNumericUpDownColumn(); numericCell.Minimum = 0; numericCell.Increment = 1; numericCell.Maximum = 100; column = numericCell; } break; default: { column = new DataGridViewTextBoxColumn(); } break; } if (col.ValueType == DataValueType.Float) { column.DefaultCellStyle.Format = "##0.000"; } column.HeaderText = col.Text; column.Name = col.Name; column.Width = col.Width; column.ReadOnly = (col.ReadOnly != 0) ? true : false; column.Visible = (col.Visible != 0) ? true : false; gridColumns[i] = column; if (hsGrid.Background == 1 && column.ReadOnly) { column.DefaultCellStyle.BackColor = Color.FromArgb(244, 243, 234); } } dgv.Columns.AddRange(gridColumns); }