allow textbox style editing within a GridView

Murray Brown
Murray Brown used Ask the Experts™
Hi. I use the following code to in my web app to allow the user to click a GridView cell that then loads the information
pertaining to that cell to other controls to allow editing. I would like to also make the editing possible in the gridview cell by turning
it into a text box. How would I do that? I cannot use Template columns and AutoGenerateColumns is true

   Private Sub GridView1_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowCreated

        Dim dtNew As DataTable = TryCast(Session("dtNew"), DataTable)
        If e.Row.RowType = DataControlRowType.DataRow Then
            'Following link shows how cells are made clickable
            For i As Integer = 0 To e.Row.Cells.Count - 1
                Dim cell As TableCell = e.Row.Cells(i)
                cell.Attributes("onmouseover") = "'pointer';'underline';"
                cell.Attributes("onmouseout") = "'none';"
                cell.ToolTip = "You can click this cell"
                cell.Attributes("onclick") = String.Format("document.getElementById('{0}').value = {1}; {2}", SelectedGridCellIndex.ClientID, i, Page.ClientScript.GetPostBackClientHyperlink(CType(sender, GridView), String.Format("Select${0}", e.Row.RowIndex)))
        End If

    End Sub

    Protected Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim grid = CType(sender, GridView)
        Dim selectedRow As GridViewRow = grid.SelectedRow
        Dim rowIndex As Integer = grid.SelectedIndex
        Dim selectedCellIndex As Integer = Integer.Parse(Me.SelectedGridCellIndex.Value)
        If selectedCellIndex > 1 Then
            Me.lblRowIndex.Text = selectedRow.RowIndex
            Me.lblColumnIndex.Text = selectedCellIndex.ToString
            Me.lblLineID.Text = grid.Rows(rowIndex).Cells(selectedCellIndex + 1).Text
            Me.lblDate.Text = grid.HeaderRow.Cells(selectedCellIndex).Text
            Me.lblHours.Text = grid.Rows(rowIndex).Cells(selectedCellIndex).Text
            Me.lblEmployeeName.Text = grid.Rows(rowIndex).Cells(1).Text
        End If
    End Sub

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer User Interface
I don’t think there is a way to make just the cell editable. It will enable the entire row for editing. In the selectedindexchanged event, set like this

grid.EditIndex = grid.SelectedIndex
Murray Developer



Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial