How to apply & ldquo; Readonly & rdquo; in the gridview text box using BoundField during edit mode

advertisements

I want READONLY on text box using Boundsfield. So far enabled is working but it not carry value in rowupdating

 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        var nopoject = ddlproject.SelectedValue.ToString();
        Int32 curntMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
        int Mont1 = curntMonth - 1;
        var Comtext = "Rst" + Mont1.ToString();
        GridView1.EditIndex = e.NewEditIndex;
        BindData();

        foreach (GridViewRow row in GridView1.Rows)
        {
            for (int i = 0; i < GridView1.Columns.Count; i++)
            {
                String headertext = GridView1.Columns[i].HeaderText;
                String cellText = row.Cells[i].Text;

                if (Comtext == "Rst1")
                {
                     GridView1.Rows[e.NewEditIndex].Cells[i].Enabled = true;
                }}}

Below code not working :

GridView1.Rows[e.NewEditIndex].Cells[i].Attributes.Add("readonly", "readonly");
GridView1.Rows[e.NewEditIndex].Cells[i].CssClass = "read-only";

Please advise. I want readonly on boundsfield programically when inline edit Thank you


You can use this snippet.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            TextBox textBox = e.Row.Cells[0].Controls[0] as TextBox;
            textBox.Enabled = false;
        }
    }
}