0

我这样做:

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

但它不起作用。当单元格为空或包含“”时,我需要将只读属性设置为 true。

4

3 回答 3

0

没有direct way设置Gridview columnreadonly
但是您可以在您的 Gridivew 事件中设置controls to readonly该列中的内容。RowDataBound例如

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;
    }
}
于 2013-02-13T05:54:11.727 回答
0

对于绑定字段,您可以假设 Controls(0) 是您想要的控件。

因此这有效:

    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
于 2015-07-15T06:22:54.407 回答
-1

这行得通吗?

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

它确实看起来好像BoundField有一个ReadOnly属性。但我现在不确定是否e.Row.Cells[3]BoundField类型。也许你可能不得不把它投射到BoundField

于 2013-02-13T05:16:18.640 回答