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
LVL 1
dshi15Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rajar AhmedConsultantCommented:
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

0
dshi15Author Commented:
Thanks, I used ButtonField

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

Do I need change to TemplateField?
0
Rajar AhmedConsultantCommented:
Just Hide the cell ,Give the exact cell no which holds ur buttonfield .
If orderChk = "" Then e.Row.Cells(3).Visible = False



<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:ButtonField Text="View" ButtonType="Button" CommandName="View Detail" >
</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
                e.Row.Cells(3).Visible = False
            End If

        End If

    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

dshi15Author Commented:
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
0
dshi15Author Commented:
thanks, i forgot this line

 If e.Row.RowType = DataControlRowType.DataRow Then

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


this row not line up correct, it moved to one column left
0
Rajar AhmedConsultantCommented:
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 .
0
Rajar AhmedConsultantCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.