C#  

Hide Column Headers and Show Row Headers in C#

Sometimes, you need to display your data in a table (DataGridView) horizontally instead of vertically. To do this, you need to perform the following actions:

DataGridView

  1. Create a Win Form: Form1
  2. Insert a DataGridView from Tools into Form1
  3. Insert a Button from Tools to Form1
  4. and write below the code
private void button3_Click(object sender, EventArgs e)
        {
            dataGridView1.RowHeadersVisible = true;
            //Optional
            //dataGridView1.ColumnHeadersVisible = false;
            dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            //Optional
            //dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dataGridView1.Columns.Add("Col1", "Col 1");
            dataGridView1.Columns.Add("Col2", "Col 2");


            dataGridView1.Rows.Add();
            dataGridView1.Rows[0].HeaderCell.Value = "Row 1";
            dataGridView1.Rows.Add();
            dataGridView1.Rows[1].HeaderCell.Value = "Row 2";
            dataGridView1.Rows.Add();
            dataGridView1.Rows[2].HeaderCell.Value = "Row 3";
        }