Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class MetaFieldTable
- {
- public MetaFieldTable(DataGridView dataGridView)
- {
- this.dataGridView = dataGridView;
- dataGridView.AllowDrop = false;
- dataGridView.AllowUserToAddRows = false;
- dataGridView.AllowUserToDeleteRows = false;
- dataGridView.AllowUserToOrderColumns = false;
- dataGridView.AllowUserToResizeRows = false;
- DataGridViewTextBoxColumn metaFieldColumn = new DataGridViewTextBoxColumn()
- {
- HeaderText = "MetaFields",
- ReadOnly = true,
- SortMode = DataGridViewColumnSortMode.NotSortable
- };
- this.dataGridView.Columns.Add(metaFieldColumn);
- DataGridViewAllocationColumn allocationColumn = new DataGridViewAllocationColumn()
- {
- HeaderText = "Allocation",
- ReadOnly = true,
- SortMode = DataGridViewColumnSortMode.NotSortable,
- AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
- };
- this.dataGridView.Columns.Add(allocationColumn);
- }
- private DataGridView dataGridView;
- public void CreateMetaFieldTable(MetaField[] metaFields, ReleaseSetupData releaseSetupData)
- {
- //for (int i = 0; i < dataGridView.Rows.Count; i++)
- //{
- // double metaFieldID = (dataGridView.Rows[i].Cells[0].Tag as MetaField).ID;
- // if (metaFields.Any(field => field.ID == metaFieldID))
- // metaFields = metaFields.Where(field => field.ID != metaFieldID).ToArray();
- // else
- // dataGridView.Rows.Remove(dataGridView.Rows[i]);
- //}
- dataGridView.Rows.Clear();
- for (int i = 0; i < metaFields.Length; i++)
- {
- MetaField currentMetaField = metaFields[i];
- dataGridView.Rows.Add(currentMetaField.Name, null);
- DataGridViewRow newRow = dataGridView.Rows[i];
- DataGridViewCell metaFieldCell = newRow.Cells[0];
- metaFieldCell.Tag = currentMetaField;
- (newRow.Cells[1] as DataGridViewAllocationCell).Initialize(releaseSetupData);
- }
- }
- private class DataGridViewAllocationColumn : DataGridViewButtonColumn
- {
- public DataGridViewAllocationColumn()
- {
- CellTemplate = new DataGridViewAllocationCell();
- }
- public override object Clone()
- {
- return base.Clone() as DataGridViewAllocationColumn;
- }
- }
- private class DataGridViewAllocationCell : DataGridViewButtonCell
- {
- public void Initialize(ReleaseSetupData releaseSetupData)
- {
- FieldName = NOTHING_SELECTED_TEXT;
- FieldType = AllocationFieldType.None;
- 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("Indexfields");
- foreach (IndexField field in releaseSetupData.IndexFields)
- {
- RegisterSubMenuItem(menuItemIndexField, field.Name, AllocationFieldType.IndexField);
- }
- ToolStripMenuItem menuItemBatchField = RegisterMenuItem("Batchfields");
- foreach (BatchField field in releaseSetupData.BatchFields)
- {
- RegisterSubMenuItem(menuItemBatchField, field.Name, AllocationFieldType.BatchField);
- }
- }
- private ContextMenuStrip contextMenu;
- private const string BUTTON_TEXT = "...";
- private const string NOTHING_SELECTED_TEXT = "[ Nothing Selected ]";
- 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);
- }
- }
- }
Add Comment
Please, Sign In to add comment