0

I do it like this:

 if (e.Row.Cells[3].Text != null && e.Row.Cells[3].Text != " " 
          && e.Row.Cells[3].Text != "")
 {
     e.Row.Cells[3].Attributes.Add("readonly", "true");
 }

But it doesn't work. I need to set readonly property in true when cell is empty or contains " ".

शेखर
  • 17,412
  • 13
  • 61
  • 117
user1848942
  • 355
  • 1
  • 5
  • 21

3 Answers3

0

There is no direct way to set Gridview column to readonly.
But You can set the controls to readonly that are in that column in RowDataBound event of your Gridivew. e.g.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == DataControlRowState.Alternate)
    {
        TextBox txt = (TextBox)e.Row.FindControl("ControlID");
        txt.ReadOnly = true;
    }
}
शेखर
  • 17,412
  • 13
  • 61
  • 117
0

For bound fields you can assume Controls(0) is the control you want.

Therefore this works:

    Protected Sub DropDownListNoVariation_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim sourceGridViewRow As GridViewRow
    Dim sourceDropDown As DropDownList
    Dim TargetTextBox As TextBox

    sourceGridViewRow = sender.Parent.Parent
    sourceDropDown = sender
    If sourceDropDown.Text = "True" Then
        TargetTextBox = sourceGridViewRow.Cells(4).Controls(0)
        TargetTextBox.ReadOnly = True
        TargetTextBox = sourceGridViewRow.Cells(5).Controls(0)
        TargetTextBox.ReadOnly = True
    Else
        TargetTextBox = sourceGridViewRow.Cells(4).Controls(0)
        TargetTextBox.ReadOnly = False
        TargetTextBox = sourceGridViewRow.Cells(5).Controls(0)
        TargetTextBox.ReadOnly = False
    End If

End Sub
Shaun Keon
  • 717
  • 7
  • 4
-1

Would this work?

if (e.Row.Cells[3].Text != null && e.Row.Cells[3].Text != " " && e.Row.Cells[3].Text != "")
 {
     e.Row.Cells[3].ReadOnly = true;
 }

It sure seems as though the BoundField has a ReadOnly property. But I am not sure now if the e.Row.Cells[3] is of BoundField type. Maybe you might have to cast it to BoundField

Hari Pachuveetil
  • 10,294
  • 3
  • 45
  • 68