Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Windows.Forms;
- using System;
- using System.Linq;
- using System.Drawing;
- using System.ComponentModel;
- namespace scripts
- {
- public partial class CustomGUIs
- {
- public static List<Dictionary<string, object>> DataGrid(List<Dictionary<string, object>> entries, List<string> propertyNames, bool spanAllScreens)
- {
- List<Dictionary<string, object>> filteredEntries = new List<Dictionary<string, object>>();
- bool escapePressed = false; // Flag to track if Escape was pressed
- var form = new Form();
- form.StartPosition = FormStartPosition.CenterScreen;
- form.Text = $"Total Entries: {entries.Count}";
- form.BackColor = Color.White;
- var dataGridView = new DataGridView();
- dataGridView.Dock = DockStyle.Fill;
- dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
- dataGridView.MultiSelect = true;
- dataGridView.ReadOnly = true;
- dataGridView.AutoGenerateColumns = false;
- dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
- dataGridView.BackgroundColor = Color.White;
- foreach (var propertyName in propertyNames)
- {
- dataGridView.Columns.Add(new DataGridViewTextBoxColumn
- {
- HeaderText = propertyName,
- DataPropertyName = propertyName
- });
- }
- // Manually populate DataGridView rows
- foreach (var entry in entries)
- {
- var row = new DataGridViewRow();
- row.CreateCells(dataGridView);
- for (int i = 0; i < propertyNames.Count; i++)
- {
- row.Cells[i].Value = entry.ContainsKey(propertyNames[i]) ? entry[propertyNames[i]] : null;
- }
- dataGridView.Rows.Add(row);
- }
- var searchBox = new TextBox();
- searchBox.Dock = DockStyle.Top;
- searchBox.TextChanged += (sender, e) =>
- {
- string searchText = searchBox.Text.ToLower();
- var searchTerms = searchText.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(query => query.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
- var filtered = entries.Where(entry =>
- {
- return searchTerms.All(orQuery =>
- {
- return orQuery.Any(term =>
- {
- return entry.Values.Any(value => value != null && value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) >= 0);
- });
- });
- }).ToList();
- // Clear and repopulate DataGridView with filtered data
- dataGridView.Rows.Clear();
- foreach (var item in filtered)
- {
- var newRow = new DataGridViewRow();
- newRow.CreateCells(dataGridView);
- for (int i = 0; i < propertyNames.Count; i++)
- {
- newRow.Cells[i].Value = item.ContainsKey(propertyNames[i]) ? item[propertyNames[i]] : null;
- }
- dataGridView.Rows.Add(newRow);
- }
- // Adjust form size
- dataGridView.AutoResizeColumns();
- int requiredWidth = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + SystemInformation.VerticalScrollBarWidth + 50;
- int availableWidth = Screen.PrimaryScreen.WorkingArea.Width - 20;
- form.Width = Math.Min(requiredWidth, availableWidth);
- if (form.Width != requiredWidth)
- {
- form.StartPosition = FormStartPosition.Manual;
- form.Location = new Point(
- (Screen.PrimaryScreen.WorkingArea.Width - form.Width) / 2,
- form.Location.Y
- );
- }
- };
- // KeyDown event handling for DataGridView
- dataGridView.KeyDown += (sender, e) =>
- {
- if (e.KeyCode == Keys.Escape)
- {
- escapePressed = true; // Set flag when Escape is pressed
- form.Close();
- }
- else if (e.KeyCode == Keys.Enter)
- {
- // Clear previously filtered entries if any
- filteredEntries.Clear();
- foreach (DataGridViewRow selectedRow in dataGridView.SelectedRows)
- {
- Dictionary<string, object> selectedEntry = new Dictionary<string, object>();
- for (int i = 0; i < propertyNames.Count; i++)
- {
- selectedEntry[propertyNames[i]] = selectedRow.Cells[i].Value;
- }
- filteredEntries.Add(selectedEntry);
- }
- escapePressed = true; // Set flag when Escape is pressed
- form.Close();
- }
- else if (e.KeyCode == Keys.Tab)
- {
- searchBox.Focus();
- }
- // Check for Shift + Left/Right Arrow keys
- else if ((e.KeyCode == Keys.Right || e.KeyCode == Keys.Left) && e.Shift)
- {
- int totalColumnWidth = dataGridView.Columns.Cast<DataGridViewColumn>().Sum(c => c.Width);
- int visibleWidth = dataGridView.ClientRectangle.Width;
- int currentOffset = dataGridView.HorizontalScrollingOffset;
- int scrollOffset = 1000; // You can adjust this value to change scroll speed
- if (e.KeyCode == Keys.Right)
- {
- // Attempt to scroll right
- if (currentOffset + visibleWidth < totalColumnWidth)
- {
- dataGridView.HorizontalScrollingOffset += scrollOffset;
- }
- }
- else if (e.KeyCode == Keys.Left)
- {
- // Attempt to scroll left
- dataGridView.HorizontalScrollingOffset = Math.Max(currentOffset - scrollOffset, 0);
- }
- e.Handled = true; // Prevent further processing of the key in other controls
- }
- else if (e.KeyCode == Keys.Right)
- {
- int totalColumnWidth = dataGridView.Columns.Cast<DataGridViewColumn>().Sum(c => c.Width);
- int visibleWidth = dataGridView.ClientRectangle.Width;
- int currentOffset = dataGridView.HorizontalScrollingOffset;
- int scrollOffset = 50; // You can adjust this value to change scroll speed
- if (currentOffset + visibleWidth < totalColumnWidth)
- {
- dataGridView.HorizontalScrollingOffset += scrollOffset;
- }
- e.Handled = true; // Prevent further processing of the key in other controls
- }
- else if (e.KeyCode == Keys.Left)
- {
- int totalColumnWidth = dataGridView.Columns.Cast<DataGridViewColumn>().Sum(c => c.Width);
- int visibleWidth = dataGridView.ClientRectangle.Width;
- int currentOffset = dataGridView.HorizontalScrollingOffset;
- int scrollOffset = 50; // You can adjust this value to change scroll speed
- dataGridView.HorizontalScrollingOffset = Math.Max(currentOffset - scrollOffset, 0);
- e.Handled = true; // Prevent further processing of the key in other controls
- }
- };
- // KeyDown event handling for SearchBox
- searchBox.KeyDown += (sender, e) =>
- {
- if (e.KeyCode == Keys.Escape)
- {
- escapePressed = true; // Set flag when Escape is pressed
- form.Close();
- }
- else if (e.KeyCode == Keys.Down)
- {
- if (dataGridView.Rows.Count > 0)
- {
- dataGridView.Focus();
- dataGridView.CurrentCell = dataGridView.Rows[0].Cells[0];
- dataGridView.Rows[0].Selected = true;
- }
- }
- else if (e.KeyCode == Keys.Enter)
- {
- dataGridView.Focus();
- dataGridView.Rows[0].Selected = true;
- foreach (DataGridViewRow selectedRow in dataGridView.SelectedRows)
- {
- Dictionary<string, object> selectedEntry = new Dictionary<string, object>();
- for (int i = 0; i < propertyNames.Count; i++)
- {
- selectedEntry[propertyNames[i]] = selectedRow.Cells[i].Value;
- }
- filteredEntries.Add(selectedEntry);
- }
- form.Close();
- }
- };
- form.Load += (sender, e) =>
- {
- //sort the entries based on the first column
- dataGridView.Sort(dataGridView.Columns[0], ListSortDirection.Ascending);
- foreach (DataGridViewColumn column in dataGridView.Columns)
- {
- column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
- }
- dataGridView.AutoResizeColumns();
- int padding = 20;
- int requiredHeight = dataGridView.Rows.GetRowsHeight(DataGridViewElementStates.Visible) + dataGridView.ColumnHeadersHeight + 2 * dataGridView.Rows[0].Height + SystemInformation.HorizontalScrollBarHeight;
- int availableHeight = Screen.PrimaryScreen.WorkingArea.Height - padding * 2;
- form.Height = Math.Min(requiredHeight, availableHeight);
- if (spanAllScreens)
- {
- int totalScreenWidth = Screen.AllScreens.Sum(screen => screen.WorkingArea.Width);
- form.Width = totalScreenWidth;
- form.StartPosition = FormStartPosition.Manual;
- form.Location = new Point(
- Screen.AllScreens.OrderBy(screen => screen.Bounds.X).First().Bounds.X,
- (Screen.PrimaryScreen.WorkingArea.Height - form.Height) / 2
- );
- }
- else
- {
- int requiredWidth = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + SystemInformation.VerticalScrollBarWidth + 43;
- // Calculate the available size minus padding
- int availableWidth = Screen.PrimaryScreen.WorkingArea.Width - padding * 2;
- // Ensure the form fits within the padded screen working area
- form.Height = Math.Min(requiredHeight, availableHeight);
- form.Width = Math.Min(requiredWidth, availableWidth);
- // Center the form manually if any dimension is adjusted
- if (form.Height != requiredHeight || form.Width != requiredWidth)
- {
- form.StartPosition = FormStartPosition.Manual;
- form.Location = new Point(
- (Screen.PrimaryScreen.WorkingArea.Width - form.Width) / 2,
- (Screen.PrimaryScreen.WorkingArea.Height - form.Height) / 2
- );
- }
- }
- };
- form.Controls.Add(dataGridView);
- form.Controls.Add(searchBox);
- searchBox.Select();
- form.ShowDialog();
- if (!escapePressed)
- {
- // Extract filtered entries based on selected rows
- foreach (DataGridViewRow row in dataGridView.SelectedRows)
- {
- Dictionary<string, object> selectedEntry = new Dictionary<string, object>();
- for (int i = 0; i < propertyNames.Count; i++)
- {
- selectedEntry[propertyNames[i]] = row.Cells[i].Value;
- }
- filteredEntries.Add(selectedEntry);
- }
- }
- return filteredEntries;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment