Net-informations.com
SiteMap  | About    

C# DataGridView Hide Columns and Rows

Displaying data in a tabular format is a task you are likely to perform frequently. The DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms . The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior.

The following C# source code manually creates a DataGridView columns and rows and hide the second column and second row.

dataGridView1.Rows[Index].Visible = false;

dataGridView1.Columns[Index].Visible = false;




using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

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

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);

            dataGridView1.Columns[1].Visible = false;
            dataGridView1.Rows[1].Visible = false;

        }
    }
}

 C# DataGridView - Related Contents