C# – All in One → C# DataGridView – Show/Hide Rows and Columns – Camposha

C# – All in One

C# – All in One

C# – All in One

C# DataGridView – Show/Hide Rows and Columns

Lets see how to show and hide both rows an columns programmatically. In brief this is what we do :

  • Populate datagridview with data,both rows and columns.
  • When hide button is clicked,we hide either the row or column that’s selected.
  • When show button is clicked, we show either the row or column that’s selected.

 

Here’s the code :

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Add_n_Hide
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            update();
        }

        public void update()
        {
            //ADD COLUMN
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Position";
            dataGridView1.Columns[1].Name = "Team";
            dataGridView1.Columns[0].Name = "Points";

            //ADD ROWS
            ArrayList row = new ArrayList();
            row.Add("1");
            row.Add("Real Madrid");
            row.Add("90");
            dataGridView1.Rows.Add(row.ToArray());

            row = new ArrayList();
            row.Add("2");
            row.Add("Barcelona");
            row.Add("88");
            dataGridView1.Rows.Add(row.ToArray());

            row = new ArrayList();
            row.Add("3");
            row.Add("Atletico Madrid");
            row.Add("79");
            dataGridView1.Rows.Add(row.ToArray());

            //ADD BUTTON COLUMN
            DataGridViewButtonColumn button = new DataGridViewButtonColumn();
            button.HeaderText = "Click Me";
            button.Name = "myButton";
            button.Text = "Click Me";
            button.UseColumnTextForButtonValue=true;
            dataGridView1.Columns.Add(button);
        }

        private void hideColumnBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns[0].Visible = false;

        }

        private void showColumnBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns[0].Visible = true;
        }

        private void hideRowBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows[0].Visible = false;
        }

        private void showRowBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows[0].Visible = true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex==3)
            {
                MessageBox.Show((e.RowIndex+1).ToString()+" clicked");
            }
        }
    }
}

Best Regards.

Leave a Reply

Your email address will not be published. Required fields are marked *

Join Us
X