Advertisement
Filip13

Data Grid View (dgv) in c# windows forms

Apr 24th, 2024
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.84 KB | Source Code | 0 0
  1. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
  2.  
  3. namespace WinFormsApp1
  4. {
  5.     public partial class Form1 : Form
  6.     {
  7.         public Form1()
  8.         {
  9.             InitializeComponent();
  10.         }
  11.  
  12.         private void Form1_Load(object sender, EventArgs e)
  13.         {
  14.             //no changing rows and columns
  15.             dgvTable.AllowUserToAddRows = false;
  16.             dgvTable.AllowUserToDeleteRows = false;
  17.             dgvTable.AllowUserToResizeRows = false;
  18.             dgvTable.AllowUserToResizeColumns = false;
  19.  
  20.             //hide heareds
  21.             dgvTable.RowHeadersVisible = false;
  22.             dgvTable.ColumnHeadersVisible = false;
  23.  
  24.             //align text(value) to middleCenter - middle and centred
  25.             dgvTable.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; // in all cells - ad default
  26.  
  27.             //use current form font but bolded and put it for dvgTable
  28.             dgvTable.Font = new Font(this.Font, FontStyle.Bold);
  29.  
  30.             //remove scroll bars - can be none/vertical/horizontal/both
  31.             dgvTable.ScrollBars = ScrollBars.None;
  32.  
  33.  
  34.             //disable pressing cells
  35.             //dgvTable.Enabled = false;
  36.  
  37.  
  38.  
  39.             //set custom dimensions
  40.             SetDimension(5, 5);
  41.  
  42.             //paint fields
  43.             PaintFields();
  44.  
  45.             //add text to fields
  46.             AddText();
  47.  
  48.         }
  49.  
  50.         private void SetDimension(int numRows, int numColumns)
  51.         {
  52.             //number of rows and columns
  53.             dgvTable.RowCount = numRows;
  54.             dgvTable.ColumnCount = numColumns;
  55.  
  56.  
  57.             int dimension = 75;
  58.  
  59.             for (int i = 0; i < dgvTable.RowCount; i++)
  60.             {
  61.                 dgvTable.Rows[i].Height = dimension;
  62.             }
  63.  
  64.             for (int i = 0; i < dgvTable.ColumnCount; i++)
  65.             {
  66.                 dgvTable.Columns[i].Width = dimension;
  67.             }
  68.  
  69.  
  70.             //resize whole dvgTable
  71.             dgvTable.Width = dgvTable.ColumnCount * dimension + 3;
  72.             dgvTable.Height = dgvTable.RowCount * dimension + 3;    // +3 beacause left and up sides are larger
  73.  
  74.         }
  75.  
  76.         private void AddText()
  77.         {
  78.             //go through dgvTable
  79.             for (int row = 0; row < dgvTable.Rows.Count; row++)  // Rows.Count/RowCount same
  80.             {
  81.                 //var variable; // not good because with var must be initialized
  82.                 var currentRow = dgvTable.Rows[row]; // var is DataGridView
  83.                 for (int column = 0; column < dgvTable.ColumnCount; column++)
  84.                 {
  85.                     //DataGridViewCell currentCell = currentRow.Cells[column];
  86.                     DataGridViewCell currentCell = currentRow.Cells[column];
  87.  
  88.                     //currentCell.Style.ForeColor = Color.Red; //text color - default is black
  89.                     //currentCell.Style.BackColor = Color.Blue; //background color
  90.  
  91.                     currentCell.Value = $"{row} - {column}";    //putting value on current field
  92.                     currentCell.ToolTipText = $"FIELD: {row} - {column}";   //changing the pop up(what we see when hovering) for current field
  93.  
  94.                     // dgvTable.Rows[row].Cells[column].Value = $"{row} - {column}";
  95.                 }
  96.             }
  97.  
  98.         }
  99.  
  100.         private void PaintFields()
  101.         {
  102.             Random rand = new Random();
  103.  
  104.             //go through dgvTable
  105.             for (int row = 0; row < dgvTable.Rows.Count; row++)  // Rows.Count/RowCount same
  106.             {
  107.                 //var variable; // not good because with var must be initialized
  108.                 var currentRow = dgvTable.Rows[row]; // var is DataGridView
  109.                 for (int column = 0; column < dgvTable.ColumnCount; column++)
  110.                 {
  111.                     //DataGridViewCell currentCell = currentRow.Cells[column];
  112.                     var currentCell = currentRow.Cells[column];
  113.  
  114.                     currentCell.Style.BackColor = Color.FromArgb(rand.Next(75, 256), rand.Next(75, 256), rand.Next(75, 256));//paint current field with random color
  115.                     // start value is 75 - field is not lighter, so we could see black colored text
  116.                 }
  117.             }
  118.  
  119.         }
  120.  
  121.         // what will happen on mouse click on cell
  122.         private void dgvTable_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  123.         {
  124.             MessageBox.Show($"Pressed cell: {e.RowIndex} - {e.ColumnIndex}");
  125.  
  126.             DataGridViewCell cell = dgvTable.Rows[e.RowIndex].Cells[e.ColumnIndex];
  127.             //var
  128.  
  129.  
  130.             //colors of current cell
  131.             cell.Style.BackColor = Color.Black;
  132.             cell.Style.ForeColor = Color.White;
  133.  
  134.  
  135.             //cahnge color of selected cell
  136.             cell.Style.SelectionBackColor = Color.Orange;
  137.         }
  138.  
  139.  
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement