Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
- namespace WinFormsApp1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //no changing rows and columns
- dgvTable.AllowUserToAddRows = false;
- dgvTable.AllowUserToDeleteRows = false;
- dgvTable.AllowUserToResizeRows = false;
- dgvTable.AllowUserToResizeColumns = false;
- //hide heareds
- dgvTable.RowHeadersVisible = false;
- dgvTable.ColumnHeadersVisible = false;
- //align text(value) to middleCenter - middle and centred
- dgvTable.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; // in all cells - ad default
- //use current form font but bolded and put it for dvgTable
- dgvTable.Font = new Font(this.Font, FontStyle.Bold);
- //remove scroll bars - can be none/vertical/horizontal/both
- dgvTable.ScrollBars = ScrollBars.None;
- //disable pressing cells
- //dgvTable.Enabled = false;
- //set custom dimensions
- SetDimension(5, 5);
- //paint fields
- PaintFields();
- //add text to fields
- AddText();
- }
- private void SetDimension(int numRows, int numColumns)
- {
- //number of rows and columns
- dgvTable.RowCount = numRows;
- dgvTable.ColumnCount = numColumns;
- int dimension = 75;
- for (int i = 0; i < dgvTable.RowCount; i++)
- {
- dgvTable.Rows[i].Height = dimension;
- }
- for (int i = 0; i < dgvTable.ColumnCount; i++)
- {
- dgvTable.Columns[i].Width = dimension;
- }
- //resize whole dvgTable
- dgvTable.Width = dgvTable.ColumnCount * dimension + 3;
- dgvTable.Height = dgvTable.RowCount * dimension + 3; // +3 beacause left and up sides are larger
- }
- private void AddText()
- {
- //go through dgvTable
- for (int row = 0; row < dgvTable.Rows.Count; row++) // Rows.Count/RowCount same
- {
- //var variable; // not good because with var must be initialized
- var currentRow = dgvTable.Rows[row]; // var is DataGridView
- for (int column = 0; column < dgvTable.ColumnCount; column++)
- {
- //DataGridViewCell currentCell = currentRow.Cells[column];
- DataGridViewCell currentCell = currentRow.Cells[column];
- //currentCell.Style.ForeColor = Color.Red; //text color - default is black
- //currentCell.Style.BackColor = Color.Blue; //background color
- currentCell.Value = $"{row} - {column}"; //putting value on current field
- currentCell.ToolTipText = $"FIELD: {row} - {column}"; //changing the pop up(what we see when hovering) for current field
- // dgvTable.Rows[row].Cells[column].Value = $"{row} - {column}";
- }
- }
- }
- private void PaintFields()
- {
- Random rand = new Random();
- //go through dgvTable
- for (int row = 0; row < dgvTable.Rows.Count; row++) // Rows.Count/RowCount same
- {
- //var variable; // not good because with var must be initialized
- var currentRow = dgvTable.Rows[row]; // var is DataGridView
- for (int column = 0; column < dgvTable.ColumnCount; column++)
- {
- //DataGridViewCell currentCell = currentRow.Cells[column];
- var currentCell = currentRow.Cells[column];
- currentCell.Style.BackColor = Color.FromArgb(rand.Next(75, 256), rand.Next(75, 256), rand.Next(75, 256));//paint current field with random color
- // start value is 75 - field is not lighter, so we could see black colored text
- }
- }
- }
- // what will happen on mouse click on cell
- private void dgvTable_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
- {
- MessageBox.Show($"Pressed cell: {e.RowIndex} - {e.ColumnIndex}");
- DataGridViewCell cell = dgvTable.Rows[e.RowIndex].Cells[e.ColumnIndex];
- //var
- //colors of current cell
- cell.Style.BackColor = Color.Black;
- cell.Style.ForeColor = Color.White;
- //cahnge color of selected cell
- cell.Style.SelectionBackColor = Color.Orange;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement