title: How to disable click of a column from a DataGridView?
body: <p>I have a <code>DataGridView</code> on WindowsForm where I'm passing data from my database. The purpose of this <code>DataGridView</code> is to enable to user to click on a value and modify it. However, I don't want the user to leave the value blank, add words, only numbers.. and of course not change the Id, which is the primary key of my table.</p>
  
  <p>I'm trying to achieve this by disabling the column which host the id of the table, however I don't know how to tell that column to disable editing.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public partial class eraseGrade : Form
  {
      Conexion con;
      String rut;
      DataTable dt;
  
      SqlDataAdapter sda;
      SqlCommandBuilder scb;
  
  public eraseGrade()
      {
          InitializeComponent();
  
          cbxAsig.Enabled = false;
          cbxAsig.Enabled = false;
          btnNotas.Enabled = false;
          btnBorra.Enabled = false;
      }
  
  private void btnBuscar_Click_1(object sender, EventArgs e)
      {
          Conexion con = Conexion.saberEstado();
  
          rut = txtRut.Text.Trim();
  
          if (validarTXTVacios(txtRut))
          {
              MessageBox.Show("Add a Rut, please");
          }
          else
          {
              Alumno a = new Alumno(rut);
              a.buscar(a);
              cbxAsig.Items.Clear();
  
              if (a.Nombre != null)
              {
                  lblNombre.Text = a.Nombre + " " + a.Apellido;
  
                  cbxAsig.Enabled = true;
  
                  AsignaturaAlumno b = new AsignaturaAlumno();
  
                  List&lt;AsignaturaAlumno&gt; l = b.buscarTodosByAlumno(rut);
  
                  List&lt;String&gt; codigosAsig = new List&lt;string&gt;();
  
                  if (l.Count != 0)
                  {
                      for (int i = 0; i &lt; l.Count(); i++)
                      {
                          codigosAsig.Add(l.ElementAt(i).Cod_asig.ToString());
                      }
  
                      Asignatura asigT = new Asignatura();
                      List&lt;Asignatura&gt; asig = new List&lt;Asignatura&gt;();
  
                      for (int i = 0; i &lt; codigosAsig.Count(); i++)
                      {
                          asig.Add(asigT.buscarbyCod(codigosAsig.ElementAt(i).ToString()));
                      }
  
                      for (int i = 0; i &lt; asig.Count(); i++)
                      {
                          cbxAsig.Items.Add(asig.ElementAt(i).CodAsignatura);
                      }
  
                      cbxAsig.Enabled = true;
                      btnNotas.Enabled = true;
  
  
                  }
  
              }
              else
              {
                  lblNombre.Text = "";
                  MessageBox.Show("Alumno no encontrado");
                  lblAsig.Text = "";
                  cbxAsig.Enabled = false;
                  btnNotas.Enabled = false;
                  btnBorra.Enabled = false;
  
              }
  
          }
      }
  
   private void cbxAsig_SelectedIndexChanged(object sender, EventArgs e)
      {
          Conexion con = Conexion.saberEstado();
  
          Asignatura asignatura = new Asignatura();
          Asignatura l = asignatura.buscarbyCod(cbxAsig.SelectedItem.ToString());
  
          lblAsig.Text = l.Nombre + " (" + l.IdSeccion + ")";
      }
  </code></pre>
  
  <p>Here is the code that obtains data from the database and inserts it into the <code>DataGridView</code>.</p>
  
  <pre><code>private void btnNotas_Click(object sender, EventArgs e)
      {
          Conexion con = Conexion.saberEstado();
  
          if (cbxAsig.SelectedItem != null)
          {
              String codigo = cbxAsig.SelectedItem.ToString();
  
              sda = new SqlDataAdapter(@"SELECT id, num_eval AS Evaluacion, porcentaje AS Porcentaje, nota AS Nota  FROM registro WHERE rut = @rut AND cod_asig = @cod_asig", con.Con);
              sda.SelectCommand.Parameters.AddWithValue("@rut", rut);
              sda.SelectCommand.Parameters.AddWithValue("@cod_asig", codigo);
              dt = new DataTable();
              sda.Fill(dt);
              dataGridView1.DataSource = dt;
              btnBorra.Enabled = true;
  }
          else
          {
              MessageBox.Show("Seleccione una asignatura");
          }
      }
  
      public Boolean validarTXTVacios(TextBox r)
      {
  
          if (txtRut.Text.Equals(""))
          {
              return true;
          }
          return false;
  
      }
  </code></pre>
  
  <p>Here is the button that updates the table with the new data from the columns. It doesn't respect primary keys or data types.</p>
  
  <pre><code>    private void btnBorra_Click(object sender, EventArgs e)
      {
  
           scb = new SqlCommandBuilder(sda);
           sda.Update(dt);
  
  
      }
  
  
  
  }
  }
  </code></pre>