简体   繁体   中英

How can i make a column from GridView(without datasource) readOnly through code? (asp.net C#)

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();
}

在此处输入图像描述 在此处输入图像描述

Try:

GridView1.ReadOnly = true;

Or this for a single cell:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM