Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deselect a datagrid row

Status
Not open for further replies.
I'm putting the code in the same place where I put the code that selects the row. I'd like the user to be able to click the selected row again to deselect it,
Code:
Private Sub dg_ItemCommand(ByVal source As Object, _
  ByVal e As DataGridCommandEventArgs) Handles dg.ItemCommand

  Select Case e.CommandName
    Case "Select"
      If Me.dgLists.SelectedIndex = e.Item.ItemIndex Then
        Me.dgLists.SelectedIndex = -1
      Else
        Me.dgLists.SelectedIndex = e.Item.ItemIndex
      End If
  End Select
End Sub

Adam
 
I tried you code and get the same results. Seems strange to me. But it must have something to do with the internal workings of how it selects the row when you click the select button. I think because the SeletedIndexChanged event fires on every click of the button. But I see no where in that event to solve your problem. Let me know if you find a solution.

Jim
 
What are they selecting and why? What changed when selecting; deselecting? I dont think the selection does anything. Maybe post some HTML?

heres a cool hoverover IDB to add...also a click anywhere to launch the select button...works on any grid you attach the event to...
Code:
<asp:DataGrid id=dg runat=server [b]OnItemDataBound=dgResults_IDB[/b]></asp:DataGrid>

    Sub dgResults_IDB(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
        Select Case e.Item.ItemType
            Case ListItemType.Item, ListItemType.AlternatingItem
                e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='Silver';this.style.foreColor='White';this.style.cursor='hand'")
                Dim button As LinkButton = CType(e.Item.Cells(0).Controls(0), LinkButton)
                e.Item.Attributes("onclick") = Page.GetPostBackClientHyperlink(button, "")
                If e.Item.ItemType = ListItemType.Item Then
                    e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='White'")
                Else
                    e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='linen'")
                End If
        End Select
    End Sub
 
adamroof, I'm not sure what relevance your code has with deselecting a datagrid row.

jbenson001, Thanks for trying it out. I ended up just changing the commandname in the buttoncolumn and the event from "Select" to something the framework wouldn't be looking for and it worked fine.



Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top