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:
 c-sharp read only column
  1. Here Fetch Data button will display the data values from the SQL and displays it by clicking it.
  2. Go to tool box and click on the DataGridview option the form will be open.
  3. 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;          
        }  
    }
}

Code Explanation

 c-sharp read only column output1
  1. 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).
  2. 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 :

 c-sharp read only column output1
  1. Here in this output the Fetch Data button will display the data values from the SQL and displays it by clicking it.
  2. Here in this output table displays the Id "1,2,3" and name "venkat, jln, arun".
 c-sharp read only column output2
  1. Here in this output table displays the Id "1,2,3" and name "venkat, jln, arun".
  2. Here in this output table displays Read Only Column which specifies to read the "jln" column in the DataTable.

Related Searches to C# Readonly | C# Controls Datagridview Read Only Columns