Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class DataGridViewAllocationCell : DataGridViewButtonCell
- {
- public void Initialize(ReleaseSetupData releaseSetupData, string initialFieldName = NOTHING_SELECTED_TEXT, AllocationFieldType initialFieldType = AllocationFieldType.None)
- {
- FieldName = initialFieldName;
- FieldType = initialFieldType;
- contextMenu = new ContextMenuStrip();
- ToolStripMenuItem clearSelectionItem = RegisterMenuItem(NOTHING_SELECTED_TEXT);
- clearSelectionItem.Click += (object sender, EventArgs e) => MenuItem_OnClick(sender, e, NOTHING_SELECTED_TEXT, AllocationFieldType.None);
- contextMenu.Items.Add(new ToolStripSeparator());
- ToolStripMenuItem menuItemIndexField = RegisterMenuItem("Indexfelder");
- foreach (IndexField field in releaseSetupData.IndexFields)
- {
- RegisterSubMenuItem(menuItemIndexField, field.Name, AllocationFieldType.IndexField);
- }
- ToolStripMenuItem menuItemBatchField = RegisterMenuItem("Batchfelder");
- foreach (BatchField field in releaseSetupData.BatchFields)
- {
- RegisterSubMenuItem(menuItemBatchField, field.Name, AllocationFieldType.BatchField);
- }
- ToolStripMenuItem menuItemKofaxValue = RegisterMenuItem("KofaxValues");
- foreach (dynamic field in releaseSetupData.BatchVariableNames)
- {
- RegisterSubMenuItem(menuItemKofaxValue, field, AllocationFieldType.KofaxValue);
- }
- }
- private ContextMenuStrip contextMenu;
- private const string BUTTON_TEXT = "...";
- private const string NOTHING_SELECTED_TEXT = "[ Kein Eintrag ]";
- public string FieldName { get; private set; }
- public AllocationFieldType FieldType { get; private set; }
- private DataGridViewAllocationColumn ParentColumn { get { return OwningColumn as DataGridViewAllocationColumn; } }
- private int LabelWidth { get { return TextRenderer.MeasureText(FieldName, ParentColumn.DefaultCellStyle.Font).Width; } }
- private ToolStripMenuItem RegisterMenuItem(string itemName)
- {
- return contextMenu.Items.Add(itemName) as ToolStripMenuItem;
- }
- private void RegisterSubMenuItem(ToolStripMenuItem menuItem, string fieldName, AllocationFieldType fieldType)
- {
- menuItem.DropDownItems.Add(fieldName, null, (object sender, EventArgs e) => MenuItem_OnClick(sender, e, fieldName, fieldType));
- }
- private void MenuItem_OnClick(object sender, EventArgs e, string fieldName, AllocationFieldType fieldType)
- {
- FieldName = fieldName;
- FieldType = fieldType;
- DataGridView.Refresh();
- }
- protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
- {
- base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentBackground & ~DataGridViewPaintParts.ContentForeground);
- Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(ParentColumn.Index, rowIndex, false);
- Rectangle cellRectangle = GetContentBounds(rowIndex);
- Rectangle labelRectangle = new Rectangle(displayRectangle.Location, new Size(LabelWidth, displayRectangle.Height));
- cellRectangle.Offset(displayRectangle.Location);
- base.Paint(graphics, clipBounds, cellRectangle, rowIndex, elementState, value, BUTTON_TEXT, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.All);
- TextRenderer.DrawText(graphics, FieldName, cellStyle.Font, labelRectangle, cellStyle.ForeColor);
- }
- protected override Rectangle GetContentBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex)
- {
- Rectangle rectangle = base.GetContentBounds(graphics, cellStyle, rowIndex);
- return new Rectangle(rectangle.Width - rectangle.Height, rectangle.Top, rectangle.Height, rectangle.Height);
- }
- protected override void OnContentClick(DataGridViewCellEventArgs e)
- {
- base.OnContentClick(e);
- Rectangle contentRectangle = GetContentBounds(e.RowIndex);
- Rectangle displayRectangle = DataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
- Point location = new Point(displayRectangle.Left + contentRectangle.Left, displayRectangle.Top + contentRectangle.Bottom);
- contextMenu.Show(DataGridView, location);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement