DATAGRIDVIEW READ ONLY COLUMNS AND ROWS C#

ben reitman

Total Post:96

Points:676
Posted by  ben reitman
C# 
C#
 1564  View(s)
Ratings:
Rate this:
Hi Experts,

How to make read only columns and rows in C#

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: DataGridView Read Only Columns and Rows C#

    Hi Ben,


    You can use this code:

    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.Rows[1].ReadOnly = true;

            }

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!