Conditionally display Select Button in Gridview

Last post 06-18-2007 4:15 AM by Allen Chen – MSFT. 3 replies.

Sort Posts:

  • Conditionally display Select Button in Gridview

    06-13-2007, 10:25 AM
    • Member
      56 point Member
    • abev
    • Member since 02-13-2007, 3:12 PM
    • Posts 29

    I have a Gridview with 2 gridviews nested inside of it. All data displays correctly.

    In Gridview 3 I have a Select button. I would like the conditionally display the Select button based on the value in the first column in the main gridview. Please no C# code ;)
     

    Filed under:
  • Re: Conditionally display Select Button in Gridview

    06-13-2007, 2:11 PM
    • Member
      56 point Member
    • abev
    • Member since 02-13-2007, 3:12 PM
    • Posts 29

    I kind of thought I could use this code

    Dim sTime As String = GridView1.DataKeys(0).Values(0)

    on the GridView3_OnRowCreated but I cannot find a way to increment the index as each row is created to display or hide the button.  

  • Re: Conditionally display Select Button in Gridview

    06-14-2007, 8:56 AM
    • Member
      56 point Member
    • abev
    • Member since 02-13-2007, 3:12 PM
    • Posts 29

    I guess a few things are possible: Either I make no sense or I am the only person in the world who has ever wanted to refer to the parent gridview from a child gridview.

     If anyone has any thoughts to get me closer I would appreciate it.
     

  • Re: Conditionally display Select Button in Gridview

    06-18-2007, 4:15 AM
    Answer

    Hi:

      I wrote some code here, hope it can shed some light:

      Protected Sub GridView2_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView2.PreRender
            For Each gr As GridViewRow In GridView2.Rows
                If (gr.RowType = DataControlRowType.DataRow) Then
                    Dim l As Label = gr.FindControl("Label1")
                    Dim g As GridView = gr.FindControl("GridView3")

                    For Each r As GridViewRow In g.Rows
                        Dim l2 As Label = r.FindControl("Label2")

                        If l2.Text = l.Text Then 'Change condition here
                            r.FindControl("LinkButton1").Visible = False
                        End If

                    Next
                End If
            Next
     
        End Sub

            <asp:GridView  AutoGenerateColumns="false" ID="GridView2" runat="server" DataSourceID="SqlDataSource1" >
            <Columns>
            <asp:TemplateField><ItemTemplate>
                <asp:Label Visible="false" ID="Label1" runat="server" Text='<%#Eval("theID") %>'></asp:Label>
                <asp:GridView  ID="GridView3" DataSourceID="SqlDataSource1"  runat="server">
                <Columns>
                <asp:TemplateField><ItemTemplate>
                    <asp:Label Visible="false" ID="Label2" runat="server" Text='<%#Eval("theID") %>'></asp:Label>
                    <asp:LinkButton ID="LinkButton1" CommandName="Select" CommandArgument='<%#(CType(Container,GridViewRow)).RowIndex%>' runat="server">Select</asp:LinkButton>
                </ItemTemplate></asp:TemplateField>
                </Columns></asp:GridView>    
            </ItemTemplate></asp:TemplateField></Columns>
            </asp:GridView>

     

    If it doesn't work, please inform us.

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (4 items)