Link to home
Start Free TrialLog in
Avatar of dshi15
dshi15Flag for United States of America

asked on

gridview buttonfield invisible

Hi Expert,

I have a Gridview which show all customer name address etc, CustomerId is key, and a buttonfield called  "View Detail", I want "View Detail" buttonfield invisible if no order related this customerId, how I can do it?

Thanks in advance
Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India image

Created an idea to do ur task , Try below Code
u ve to use :
RowDataBound


<asp:GridView ID="GridView1" runat="server" 
AutoGenerateColumns="False" AllowPaging="True"  
PageSize="3" Width="324px" DataKeyNames="CategoryID"  OnRowDataBound="GridView1_RowDataBound"
>
<Columns>
<asp:BoundField DataField="orderid" />
<asp:BoundField DataField="orderstatus" />
<asp:TemplateField> 
<ItemTemplate>
   <asp:Label ID="lblName" runat="server"  text='<%# Bind("Name") %>'>
   </asp:Label>
   
</ItemTemplate>

</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
    <asp:Button ID="butTest" runat="server" Text="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            ' Display the company name in italics.
            Dim butTest As Button = DirectCast(e.Row.FindControl("butOrder"), Button)
            Dim _key As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString()
            Dim orderChk As String = e.Row.Cells(1).Text ' Assign ur order Status column 

            If orderChk = "" Then
                butTest.Visible = False
            End If

        End If

    End Sub

Open in new window

Avatar of dshi15

ASKER

Thanks, I used ButtonField

<asp:ButtonField Text="View" ButtonType="Button" CommandName="View Detail" >

Do I need change to TemplateField?
ASKER CERTIFIED SOLUTION
Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dshi15

ASKER

I got an error in this line


Dim CustomerId As String = GridView1.DataKeys(e.Row.RowIndex).Value.ToString()


"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

I need use this CustomerId to check Order table if no reture I can Set
e.Row.Cells(0).Visible = False
Avatar of dshi15

ASKER

thanks, i forgot this line

 If e.Row.RowType = DataControlRowType.DataRow Then

Avatar of dshi15

ASKER

but I put e.Row.Cells(0).Visible = False


this row not line up correct, it moved to one column left
is ur button invisible ?
Yes,Dat might happen when u use buttonfield since it doesnt got id so hiding a cell m8 cause dis problem
Using template field will make ur job easier . Where u r hiden a button only . Worth trying by dat way which is flexible .
Well i used some short form of message last time apologise for dat .

"Yes,that might happen when u use asp:buttonfield since it does not got id , so hiding a cell might cause dis problem
Using template field will make your job easier , Where u r hiding a button only on the cell . Worth trying by dat way which is flexible ."



Meeran03