Question

I have a template field with a button. I want to press the button and get the row Id so I can return the users selection. I have a regular select button which works fine but the user want the people who are not employee’s to hide the select button. I have this working but since it’s a template field it fires the RoW_command procedure and I cannot seem to get the row index since it is a template field and not the regular Select button. Or I cannot seem to name the Regular select button since it Command field does not have a Name or ID property?

Like I said this works hiding the template field called btnSelect Private Sub GridView3_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView3.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        If (DataBinder.Eval(e.Row.DataItem, "LegacyPersonType") <> "Employee") Then
            e.Row.ForeColor = Drawing.Color.Black
            e.Row.BackColor = Drawing.Color.Yellow ' This will make row back color yellow 
            e.Row.FindControl("btnSelect").Visible = False

        Else
            e.Row.ForeColor = Drawing.Color.Black
            e.Row.BackColor = Drawing.Color.White   ' the normal employees make white
        End If

    End If

End Sub

I need to find the Row index when I press btnSelect
Private Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand

    Dim index As Integer = Convert.ToInt32(e.CommandArgument) ‘ Error invalid 

‘ in other words when pressing the tem-plate field there is no e.CommandArgument ‘But clicking the regular select there is

    Dim row As GridViewRow = GridView3.Rows(index)
    Session("EnterpriseID") = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(2).Text)
    Dim EmployeeType As String = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(7).Text)
    Dim CommonName As String = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(1).Text)

    Dim EnterpriseID = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(6).Text)
Was it helpful?

Solution 2

Ok I figured it out myself, I made the default Select button on the grid a Template field. since it was the default button it had the necessary code to make it fire. This was the problem with making a template field from scratch. And by making it a template field it then also gave it a name. So in the RowDataBound code the FindControl method above hides it when a person is not an employee. – R2 Builder 1 min ago edit

OTHER TIPS

The GridViewRow object knows its row index via the RowIndex property in the RowCommand event, like this:

Private Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
    ' Get the row by finding the grandparent of the control (button) 
    ' that initiated the command
    Dim theGridViewRow As GridViewRow
    theGridViewRow = CType(CType(e.CommandSource, Button).Parent.Parent, GridViewRow)

    ' Get the index value from the grid view row
    Dim index As Integer = theGridViewRow.RowIndex
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top