how to make GridView column read only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agarwalsunitadhn
    New Member
    • Jan 2008
    • 82

    how to make GridView column read only

    I am using Gridview and on that data is binded conditionally.t he problem is that at the time of editing whole row of the grid is in editable mode and i want only few of the columns editable. my code is as follows
    Code:
     Private Sub LoadGrid()
            Select Case (Session("sid").ToString())
                Case "d"
                    constr = "select dc ,dnh,dn from dst order by dc"
                Case "s"
                    constr = "select sc,sn,id from sd order by sc"
                
            End Select
    
            cmd = New SqlCommand
    
           
            cmd.Parameters.Clear()
            Try
                If con.State = ConnectionState.Closed Then con.Open()
    
                cmd.CommandText = constr
                cmd.Connection = con
    
                Myreader = cmd.ExecuteReader()
                Me.GridView1.DataSource = Myreader
                Me.GridView1.DataBind()
                If (Session("sid").ToString() = "d" Or Session("sid").ToString() = "s" ) Then
                    UndideGrid(3)
                           End If
            Catch ex As Exception
                If con.State = ConnectionState.Open Then con.Close()
            Finally
                If con.State = ConnectionState.Open Then con.Close()
            End Try
        End Sub
        
    
        Public Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
            GridView1.EditIndex = -1
            Call LoadGrid()
            lblMessage.Text = ""
        End Sub
    
        Public Sub UndideGrid(ByVal x As Integer)
            If (GridView1.Columns.Count > 0) Then
                GridView1.Columns(x).Visible = False
            Else
                GridView1.HeaderRow.Cells(x).Visible = False
                Dim gvr As GridViewRow
                For Each gvr In GridView1.Rows
                    gvr.Cells(x).Visible = False
                Next
            End If
        End Sub
    
           Public Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
            GridView1.EditIndex = e.NewEditIndex
            Call LoadGrid()
        End Sub
    
        Public Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
            Dim idx As Integer = e.RowIndex
            Dim row1 As GridViewRow = GridView1.Rows(idx)
    
            If (Session("sid").ToString() = "d" Or Session("sid").ToString() = "s") Then
                Dim f1 As TextBox = CType(row1.Cells(1).Controls(0), TextBox)
                Dim f2 As TextBox = CType(row1.Cells(2).Controls(0), TextBox)
                Dim f3 As TextBox = CType(row1.Cells(3).Controls(0), TextBox)
                lblMessage.Text = "Field Value f1=" & f1.Text & " f2=" & f2.Text & " f3=" & f3.Text
                Select Case (Session("sid").ToString())
                    Case "d"
                        constr = "update Dst set dnh=@f2 where dc=@f1"
                    Case "s"
                        constr = "update sd set sn=@f2 where sc=@f1 and id=@f3"
                        
                End Select
                cmd = New SqlCommand(constr, con)
                Try
                    If con.State = ConnectionState.Closed Then con.Open()
    
                    cmd.Parameters.Clear()
                    cmd.Parameters.Add(New SqlParameter("@f2", SqlDbType.NVarChar)).Value = f2.Text
                    cmd.Parameters.Add(New SqlParameter("@f1", SqlDbType.NVarChar)).Value = f1.Text
                    If (Session("sid").ToString() = "d") Then
                    Else
                        cmd.Parameters.Add(New SqlParameter("@f3", SqlDbType.Int)).Value = Convert.ToInt32(f3.Text)
                    End If
                    Dim rowno As Integer = cmd.ExecuteNonQuery()
    
                Catch ex As Exception
                    lblMessage.Text = lblMessage.Text & "Error" & ex.Message
                    If con.State = ConnectionState.Open Then con.Close()
                Finally
                    If con.State = ConnectionState.Open Then con.Close()
                End Try
            
                   GridView1.EditIndex = -1
            Call LoadGrid()
        End Sub
    
        Protected Sub rdDist_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rdDist.CheckedChanged
            Label18.Text = "District Details"
            Session("sid") = "d"
            Call LoadGrid()
    
            'Call LoadGrid()
        End Sub
    
        Protected Sub rdSubDiv_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rdSubDiv.CheckedChanged
            Label18.Text = "SubDivision Details"
            Session("sid") = "s"
            Call LoadGrid()
            lblMessage.Text = ""
    
        End Sub
    
       End Class
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Make the boundField readonly.

    Comment

    • agarwalsunitadhn
      New Member
      • Jan 2008
      • 82

      #3
      Originally posted by PRR
      Make the boundField readonly.
      but how to do this at runtime.please view my code at once then tell how to make the cell uneditable

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I wouldn't recommend doing this at run time (it's a little messy). I would first start by specifying whether or not the control should be enabled in the EditItemTemplat e for for each TemplateField that is bound to your underlying data source.

        If, for some reason, you are unable to do this, then you need to implement a method that handles the GridView's OnRowDataBound event. In this method you will have to set each control accordingly.

        -Frinny

        Comment

        • agarwalsunitadhn
          New Member
          • Jan 2008
          • 82

          #5
          Originally posted by Frinavale
          I wouldn't recommend doing this at run time (it's a little messy). I would first start by specifying whether or not the control should be enabled in the EditItemTemplat e for for each TemplateField that is bound to your underlying data source.

          If, for some reason, you are unable to do this, then you need to implement a method that handles the GridView's OnRowDataBound event. In this method you will have to set each control accordingly.

          -Frinny
          I am not using any item template in this grid because grid are binded as per condition from different tables and having different fields.that's why i am not able to make a particular field read only.
          please help

          Comment

          • mfaisalpk
            New Member
            • May 2012
            • 1

            #6
            If the grid property is AutoGenerateCol umns = True and you also want to edit it but few Columns are not allowed for editing then you have to do very simple thing, put those columns in DataKeyNames only.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              I would handle the OnRowDataBound event and set the cell to disabled in that code.

              Comment

              Working...