protected virtual void SetCellTag(DataGridViewCell cell) { if (cell.Value == null || cell.Value == System.DBNull.Value || cell.ColumnIndex < 0 || cell.RowIndex < 0) { return; } string text = cell.Value.ToString(); MedGridView grid = cell.DataGridView as MedGridView; MedGridViewColumn girdViewColumn = grid.MedGridViewColumns[cell.ColumnIndex]; if (!string.IsNullOrEmpty(girdViewColumn.DictTableName) && !string.IsNullOrEmpty(girdViewColumn.DictValueFieldName) && !string.IsNullOrEmpty(girdViewColumn.DisplayFieldName) && !girdViewColumn.DictValueFieldName.Equals(girdViewColumn.DisplayFieldName)) { text = TransDictCode(girdViewColumn.DictTableName, girdViewColumn.DictValueFieldName, girdViewColumn.DisplayFieldName, girdViewColumn.DictWhereString, cell.Value); } else if (!string.IsNullOrEmpty(girdViewColumn.Format)) { if (cell.Value is DateTime && (IsDateTimeFomrat(girdViewColumn.Format) || IsFunctionFormat(girdViewColumn.Format))) { text = FormatDateTime((DateTime)cell.Value, girdViewColumn.Format); } } cell.Tag = text; }
/// <summary> /// GridView下拉框选择 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void gridView_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex < 0 || e.RowIndex < 0) { return; } MedGridView grid = sender as MedGridView; DataGridViewCell cell = grid.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (cell.ReadOnly == true) { return; } if (grid.MedGridViewColumns.Count <= e.ColumnIndex) { return; } MedGridViewColumn gridViewColumn = grid.MedGridViewColumns[e.ColumnIndex]; if (IsDateTimeFomrat(gridViewColumn.Format) && (cell.Value == null || cell.Value is DateTime)) { DateTime dateTime; if (cell.Value == null) { dateTime = DateTime.Now; } else { dateTime = (DateTime)cell.Value; } Rectangle rect = grid.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true); string replaceString = "-"; string formatString = TransDateFormat(gridViewColumn.Format, replaceString); Dialog.ShowDateTimeSelector(dateTime, grid, new Point(rect.Left + grid.Location.X, rect.Bottom + grid.Location.Y) , new EventHandler(delegate(object sender1, EventArgs e1) { if (sender1 is DateTime) { cell.Value = (DateTime)sender1; SetCellTag(cell); } }), formatString); } else if (!string.IsNullOrEmpty(gridViewColumn.DictTableName) && !string.IsNullOrEmpty(gridViewColumn.DictValueFieldName)) { Rectangle rect = grid.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); string displayName = !string.IsNullOrEmpty(gridViewColumn.DisplayFieldName) ? gridViewColumn.DisplayFieldName : gridViewColumn.DictValueFieldName; DataRow[] rows = BuildPopupItemsData(gridViewColumn.DictTableName, gridViewColumn.DictWhereString); Dialog.ShowCustomSelection(rows, displayName, grid, new Point(rect.Left, rect.Bottom), new Size(rect.Width, 300) , new EventHandler(delegate(object s1, EventArgs e1) { if (s1 is int) { int index = (int)s1; cell.Value = rows[index][gridViewColumn.DictValueFieldName]; SetCellTag(cell); } })); } }
/// <summary> /// GridView单元格绘制事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected virtual void gridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { MedGridView grid = sender as MedGridView; if (e.Value != null && e.Value != System.DBNull.Value && e.ColumnIndex >= 0) { if (e.RowIndex >= 0) { MedGridViewColumn girdViewColumn = grid.MedGridViewColumns[e.ColumnIndex]; if (!string.IsNullOrEmpty(girdViewColumn.DictTableName) && !string.IsNullOrEmpty(girdViewColumn.DictValueFieldName) && !string.IsNullOrEmpty(girdViewColumn.DisplayFieldName) && !girdViewColumn.DictValueFieldName.Equals(girdViewColumn.DisplayFieldName)) { Rectangle rect = e.CellBounds; e.Handled = true; e.PaintBackground(rect, true); string text = TransDictCode(girdViewColumn.DictTableName, girdViewColumn.DictValueFieldName, girdViewColumn.DisplayFieldName, girdViewColumn.DictWhereString, e.Value); Color color = e.CellStyle.ForeColor; if (grid.SelectedCells != null && grid.SelectedCells.Count > 0 && grid.SelectedCells.Contains(grid.Rows[e.RowIndex].Cells[e.ColumnIndex])) { color = e.CellStyle.SelectionForeColor; } e.Graphics.DrawString(text, e.CellStyle.Font, new SolidBrush(color), rect.X, rect.Y + (rect.Height - e.Graphics.MeasureString("A", e.CellStyle.Font).Height) / 2); } else if (!string.IsNullOrEmpty(girdViewColumn.Format)) { if (e.Value is DateTime && (IsDateTimeFomrat(girdViewColumn.Format) || IsFunctionFormat(girdViewColumn.Format))) { Rectangle rect = e.CellBounds; e.Handled = true; e.PaintBackground(rect, true); string text = ""; if (e.Value != null) { text = FormatDateTime((DateTime)e.Value, girdViewColumn.Format); } Color color = e.CellStyle.ForeColor; if (grid.SelectedCells != null && grid.SelectedCells.Count > 0 && grid.SelectedCells.Contains(grid.Rows[e.RowIndex].Cells[e.ColumnIndex])) { color = e.CellStyle.SelectionForeColor; } e.Graphics.DrawString(text, e.CellStyle.Font, new SolidBrush(color), rect.X, rect.Y + (rect.Height - e.Graphics.MeasureString("A", e.CellStyle.Font).Height) / 2); } } } else//列标题处理 { if (e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width > e.CellBounds.Width - 5) { //Modify by wenpei.x@2014-02-11 //清点单样式改为读取配置的列标题样式ColumnHeadersDefaultCellStyle //旧处理 //Rectangle rect = e.CellBounds; //e.Handled = true; //e.PaintBackground(rect, true); //string text = e.Value.ToString(); //e.Graphics.DrawString(text, e.CellStyle.Font, new SolidBrush(e.CellStyle.ForeColor), rect.X, rect.Y + (rect.Height - e.Graphics.MeasureString("A", e.CellStyle.Font).Height) / 2); DataGridViewCellStyle colStyle = grid.ColumnHeadersDefaultCellStyle; Rectangle rect = e.CellBounds; e.Handled = true; e.PaintBackground(rect, true); string text = e.Value.ToString(); e.Graphics.DrawString(text, colStyle.Font, new SolidBrush(colStyle.ForeColor), rect.X, rect.Y + (rect.Height - e.Graphics.MeasureString("A", colStyle.Font).Height) / 2); } } } }