Punuth
Punuth

Reputation: 417

How to make a column read only in data grid in asp.net

I have created a DataGrid and I take the data from database and put it in there.
All the data retrieval are done by dynamically in the code behind.
Here is a some portion of my code.

SqlConnection con = new SqlConnection("Data Source=PUNUTHL\\SQL;Initial Catalog=mytest;Integrated Security=True");
SqlDataAdapter ad;
DataSet ds = new DataSet();
static DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ad = new SqlDataAdapter("Select * from product",con);
        ad.Fill(ds);
        dt = ds.Tables[0];
        DataGrid1.DataSource = dt;
        DataGrid1.DataBind();
    }
}   

Upto this point it works perfectly.
But I wanted to add the edit function for that DataGrid
But according to my query I took the "product_ID" values as well which can not allows to be updated.
Since I use both "Etid" and "Delete" commands, "prodcut-id" may be my 3rd column as shown in the picture.

enter image description here
So I wanted to be that column read only when the client hit on the edit button.So how can I tackle this problem.Are there any way to get the column names or column indexed?Please can some one help me.

Upvotes: 0

Views: 1027

Answers (1)

Akansha
Akansha

Reputation: 943

This is one of the way to make a column at any index readonly.

 DT.Columns(0).ReadOnly = True '// Make the column(0) readonly and assign to DataGrid.

   Datagrid!.DataSource = DT

where DT is datatable used to collect the data from database and is datasource to the datagrid Datagrid1.

For further editing make the column readonly= false and make changes and again turn it into readonly.

Here in your case you can make your Id column that is 2 as readonly

Upvotes: 2

Related Questions