0

I'm trying to make some columns readonly for not being able to edit them. I have tried a lot of solutions but none of them worked.I am using a gridview without datasource.( I have more tables and i'm displaying them using a dropdownlist).Anyway i want to edit just 2 column or one and i can't do this.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   //// this function doesn't work
   BoundField bound = GridView1.Columns[0] as BoundField;
   bound.InsertVisible = false;
   bound.ReadOnly = true;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{         
   ((BoundField)GridView1.Columns[1]).ReadOnly = true;//this doesn't work too
   GridViewRow row = GridView1.Rows[e.RowIndex];
   string contract_ID = (row.Cells[2].Controls[0] as TextBox).Text;  
   string room_ID= (row.Cells[6].Controls[0] as TextBox).Text;
            
   string query = "UPDATE CONTRACTE_INCHIRIERE SET ID_CAMERA='"+room_ID+"' WHERE ID_CONTRACT="+contract_ID;

   Response.Write(query);  
            
   using (OracleCommand cmd = new OracleCommand(query,con))
   {             
     con.Open();
     cmd.ExecuteNonQuery();
     con.Close();
   }
           
   //Reset the edit index.
   GridView1.EditIndex = -1;

   //Bind data to the GridView control.
    SortingBindGrid();
}

enter image description here enter image description here

Sydney_dev
  • 1,448
  • 2
  • 17
  • 24

1 Answers1

0

Try:

GridView1.ReadOnly = true;

Or this for a single cell:

GridView1.Rows[rowIndex][colName].ReadOnly = true;