public int Compare(object x, object y) { OfferBookGridRow obj1 = (OfferBookGridRow)x; OfferBookGridRow obj2 = (OfferBookGridRow)y; return(string.Compare(obj1.Cells[this.columnIndex].Value.ToString(), obj2.Cells[this.columnIndex].Value.ToString()) * (direction == ListSortDirection.Ascending ? 1 : -1)); }
// the OnCellMouseDown is overriden so the control can check to see if the // user clicked the + or - sign of the group-row /// <summary> /// /// </summary> /// <param name="e"></param> protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e) { if (e.RowIndex < 0) { return; } OfferBookGridRow row = (OfferBookGridRow)base.Rows[e.RowIndex]; if (row.IsGroupRow && row.IsIconHit(e)) { System.Diagnostics.Debug.WriteLine("OnCellMouseDown " + DateTime.Now.Ticks.ToString()); row.Group.Collapsed = !row.Group.Collapsed; //this is a workaround to make the grid re-calculate it's contents and backgroun bounds // so the background is updated correctly. // this will also invalidate the control, so it will redraw itself row.Visible = false; row.Visible = true; } else { base.OnCellMouseDown(e); } }
/// <summary> /// /// </summary> /// <param name="e"></param> protected override void OnCellDoubleClick(DataGridViewCellEventArgs e) { if (e.RowIndex < 0) { return; } if (e.RowIndex >= 0) { OfferBookGridRow row = (OfferBookGridRow)base.Rows[e.RowIndex]; if (row.IsGroupRow) { row.Group.Collapsed = !row.Group.Collapsed; //this is a workaround to make the grid re-calculate it's contents and backgroun bounds // so the background is updated correctly. // this will also invalidate the control, so it will redraw itself row.Visible = false; row.Visible = true; return; } } base.OnCellClick(e); }
/// <summary> /// /// </summary> /// <param name="index"></param> /// <param name="dataGridViewRow"></param> /// <returns></returns> public int InsertAt(int index, OfferBookGridRow dataGridViewRow) { int _ret = 0; try { if (this.grouped) { int pos; //lock (this.Rows) lock (syncRoot) { pos = ValidatePosition(index, ref dataGridViewRow); if (pos > 0) { this.Rows.Insert(pos, dataGridViewRow); } else { this.Rows.Insert(1, dataGridViewRow); } _ret = pos; } } else { //Workaround: the broadcasting service is sending the signal even after the cancellation. //We must make sure that the list is empty after cancellation if (index <= this.Rows.Count + 1) { //lock (this.Rows) lock (syncRoot) { this.Rows.Insert(index, dataGridViewRow); _ret = index; } } } } catch (ArgumentOutOfRangeException outOfRangeEx) { logger.Warn(String.Format("OfferBookGrid > InsertAt({0}): a posição {1} de {2} do livro (Agrupado:{3}) não existe mais ({4}).", this.Stock, index, this.RowCount, this.grouped, outOfRangeEx.Message)); } catch (Exception ex) { //AplicacaoGeral.ReportarErro("OfferBookGrid > InsertAt", ex + " | " + index.ToString() + " | " + this.RowCount.ToString() + " | " + this.grouped.ToString()); logger.ErrorFormat("Exception [{0}] [{1}] [{2}] [{3}]", ex, index, this.RowCount, this.grouped); } return(_ret); }
/// <summary> /// /// </summary> /// <param name="e"></param> protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e) { OfferBookGridRow row = (OfferBookGridRow)base.Rows[e.RowIndex]; if (row.IsGroupRow) { e.Cancel = true; } else { base.OnCellBeginEdit(e); } }
/// <summary> /// /// </summary> /// <param name="index"></param> /// <param name="dataGrigViewRow"></param> /// <returns></returns> private int ValidatePosition(int index, ref OfferBookGridRow dataGrigViewRow) { StringBuilder str = new StringBuilder(); int _groupsCount = 0; int _itemsCount = 0; int _return = 0; foreach (IOfferBookGridGroup _group in _groups) { lock (this.SyncRoot) { _groupsCount++; _itemsCount += _group.ItemCount; if (index <= _itemsCount) { /********************************************************************************************/ /***************************************** Buy side *****************************************/ /********************************************************************************************/ if (this.Type.Equals(GridType.Buy)) { /********************************************************************************************/ /********** When the row has a valid group, insert it into the corresponding group **********/ /********************************************************************************************/ /* The row belongs to the previous group and direction are ascending */ if (Double.Parse(dataGrigViewRow.Cells[BUYSIDE_COL_PRC].Value.ToString()).Equals(Double.Parse(_group.Value.ToString()))) { _return = index + _groupsCount; _group.ItemCount++; _group.Quantidade += Int32.Parse(dataGrigViewRow.Cells[BUYSIDE_COL_QTY].Value.ToString()); dataGrigViewRow.Group = _group; break; } /* The row belongs to the previous following */ if (_groups.Count > _groupsCount) { if (Double.Parse(dataGrigViewRow.Cells[BUYSIDE_COL_PRC].Value.ToString()).Equals(Double.Parse(_groups[_groupsCount].Value.ToString()))) { _return = index + _groupsCount + 1; _groups[_groupsCount].ItemCount++; _groups[_groupsCount].Quantidade += Int32.Parse(dataGrigViewRow.Cells[BUYSIDE_COL_QTY].Value.ToString()); dataGrigViewRow.Group = _groups[_groupsCount]; break; } } /********************************************************************************************/ /*********** When the row does not have a valid group, insert it into a new group ***********/ /********************************************************************************************/ // Before if (Double.Parse(dataGrigViewRow.Cells[BUYSIDE_COL_PRC].Value.ToString()) > Double.Parse(_group.Value.ToString())) { //if (index.Equals(0)) //{ IOfferBookGridGroup grp = CreateGroup(_groupsCount - 1, _groupsCount + index - 1, Double.Parse(dataGrigViewRow.Cells[BUYSIDE_COL_PRC].Value.ToString())); _return = _groupsCount + index; grp.Quantidade += Int32.Parse(dataGrigViewRow.Cells[BUYSIDE_COL_QTY].Value.ToString()); dataGrigViewRow.Group = grp; break; //} } // After if (Double.Parse(dataGrigViewRow.Cells[BUYSIDE_COL_PRC].Value.ToString()) < Double.Parse(_group.Value.ToString())) { IOfferBookGridGroup grp = CreateGroup(_groupsCount, _groupsCount + _itemsCount, Double.Parse(dataGrigViewRow.Cells[BUYSIDE_COL_PRC].Value.ToString())); _return = _groupsCount + _itemsCount + 1; grp.Quantidade += Int32.Parse(dataGrigViewRow.Cells[BUYSIDE_COL_QTY].Value.ToString()); dataGrigViewRow.Group = grp; break; } } /********************************************************************************************/ /***************************************** Sell side ****************************************/ /********************************************************************************************/ if (this.Type.Equals(GridType.Sell)) { /********************************************************************************************/ /********** When the row has a valid group, insert it into the corresponding group **********/ /********************************************************************************************/ /* The row belongs to the previous group and direction are ascending */ if (Double.Parse(dataGrigViewRow.Cells[SELLSIDE_COL_PRC].Value.ToString()).Equals(Double.Parse(_group.Value.ToString()))) { _return = index + _groupsCount; _group.ItemCount++; _group.Quantidade += Int32.Parse(dataGrigViewRow.Cells[SELLSIDE_COL_QTY].Value.ToString()); dataGrigViewRow.Group = _group; break; } /* The row belongs to the previous following */ if (_groups.Count > _groupsCount) { if (Double.Parse(dataGrigViewRow.Cells[SELLSIDE_COL_PRC].Value.ToString()).Equals(Double.Parse(_groups[_groupsCount].Value.ToString()))) { _return = index + _groupsCount + 1; _groups[_groupsCount].ItemCount++; _groups[_groupsCount].Quantidade += Int32.Parse(dataGrigViewRow.Cells[SELLSIDE_COL_QTY].Value.ToString()); dataGrigViewRow.Group = _groups[_groupsCount]; break; } } /********************************************************************************************/ /*********** When the row does not have a valid group, insert it into a new group ***********/ /********************************************************************************************/ // After if (Double.Parse(dataGrigViewRow.Cells[SELLSIDE_COL_PRC].Value.ToString()) > Double.Parse(_group.Value.ToString())) { IOfferBookGridGroup grp = CreateGroup(_groupsCount, _groupsCount + _itemsCount, Double.Parse(dataGrigViewRow.Cells[0].Value.ToString())); _return = _groupsCount + _itemsCount + 1; grp.Quantidade += Int32.Parse(dataGrigViewRow.Cells[SELLSIDE_COL_QTY].Value.ToString()); dataGrigViewRow.Group = grp; break; } // Before if (Double.Parse(dataGrigViewRow.Cells[SELLSIDE_COL_PRC].Value.ToString()) < Double.Parse(_group.Value.ToString())) { //if (index.Equals(0)) //{ IOfferBookGridGroup grp = CreateGroup(_groupsCount - 1, _groupsCount + index - 1, Double.Parse(dataGrigViewRow.Cells[SELLSIDE_COL_PRC].Value.ToString())); _return = _groupsCount + index; grp.Quantidade += Int32.Parse(dataGrigViewRow.Cells[SELLSIDE_COL_QTY].Value.ToString()); dataGrigViewRow.Group = grp; break; //} } } } } } return(_return); }