C# Readonly | C# Controls Datagridview Read Only Columns - c# - c# tutorial - c# net
What is Datagridview ?
- 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 DataGridView control provides TextBox, CheckBox, Image, Button, ComboBox and Link columns with the corresponding cell types.
Read Only Columns:
- In the DataGridView control, the ReadOnly Column property value determines whether users can edit cells in that column.
- In C#, a table is read-only if the value of the ReadOnly property is true for the cell, the cell's row, the cell's column, or the DataGridView.
- Here in the below table is representing to Windows Forms:

- Here Fetch Data button will display the data values from the SQL and displays it by clicking it.
- Go to tool box and click on the DataGridview option the form will be open.
- Go to tool box and click on the button option and put the name as Read Only Column in the button.
C# Sample Code - C# Examples
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WikiTechy_Csharp_Windows_Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataSet ds = new DataSet();
private void button1_Click(object sender, EventArgs e)
{
string connectionString =@"Data Source=VENKAT\KAASHIV;
Initial Catalog=wikitechy;
user id = venkat;password=venkat";
string sql = "SELECT * FROM login_table";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
connection.Open();
dataadapter.Fill(ds);
connection.Close();
dataGridView1.DataSource = ds.Tables[0];
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.Columns[1].ReadOnly = true;
}
}
}
Read Also
dot net training institute near me , summer internship with stipend , dot net training center in chennaiCode Explanation

- Here private void button2_Click(object sender, EventArgs e) click event handler for button2 specifies, which is a normal Button control in Windows Forms.Ý The two parameters, "object sender" and "EventArgs e" parameters of function of event of a button such as private void button2_Click(object sender, EventArgs e).
- Here dataGridView1.Columns[1].ReadOnly = true; read-only if the value of the ReadOnly property is true for the cell, the cell's row, the cell's column, or the DataGridView.
Sample C# examples - Output :

- Here in this output the Fetch Data button will display the data values from the SQL and displays it by clicking it.
- Here in this output table displays the Id "1,2,3" and name "venkat, jln, arun".

- Here in this output table displays the Id "1,2,3" and name "venkat, jln, arun".
- Here in this output table displays Read Only Column which specifies to read the "jln" column in the DataTable.