How do I conditionally enable/disable a gridview's edit and delete button?

Depending on whether the user has the CanEditTx role, I would like to enable or disable the edit and delete buttons on a gridview.   Can somebody please give me a sample of how this might be achieved.

I am using VB.net.

Thanks.
sib_userAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Kelvin McDanielSr. DeveloperCommented:
Here's an example I use all the time...
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

                Dim record As {datasource_collection_type_such_as_Person} = e.Row.DataItem

                If record.{the_value_to_check} = {the_expected_value} Then

                        ' The challenging part is finding the right Cell and the right Control...
                        '    ... it's trial & error until you figure it out...
                        '    ... just set a break point on the line below and you'll figure it out.
                        Dim tb As Button = e.Row.Cells(0).Controls(0)
                        tb.Visible = False

                End If

        End If

End Sub

Open in new window

0
sib_userAuthor Commented:
azarc3,

I am a confused by the {datasource_collection_type_such_as_Person}.  

below is my gridview definition.  Perhaps you can give a little more guidance what I can substitute for
{datasource_collection_type_such_as_Person}.

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource3"
                AutoGenerateColumns="False">
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:CommandField ShowDeleteButton="True" />
                    <asp:BoundField DataField="FIRST_NAME" HeaderText="First Name"
                        SortExpression="FIRST_NAME" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                    <asp:BoundField DataField="LAST_NAME" HeaderText="Last Name"
                        SortExpression="LAST_NAME" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                    <asp:BoundField DataField="SSN" HeaderText="SSN" SortExpression="SSN" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                    <asp:BoundField DataField="TRANSACTION_DESCRIPTION"
                        HeaderText="Transaction Type" SortExpression="TRANSACTION_DESCRIPTION" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                    <asp:BoundField DataField="NUMBER_OF_UNITS" HeaderText="Quantity"
                        SortExpression="NUMBER_OF_UNITS" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                    <asp:BoundField DataField="TRANSACTION_AMOUNT" HeaderText="Amount"
                        SortExpression="TRANSACTION_AMOUNT" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Column1" HeaderText="Entry Date"
                        SortExpression="Column1" ReadOnly="True" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Column2" HeaderText="Tx Date"
                        SortExpression="Column2" ReadOnly="True" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                    <asp:BoundField DataField="ENTERED_BY" HeaderText="Entered By"
                        SortExpression="ENTERED_BY" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                    <asp:BoundField DataField="COMMENTS" HeaderText="Comments"
                        SortExpression="COMMENTS" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                    <asp:CheckBoxField DataField="APPROVED" HeaderText="Approved"
                        SortExpression="APPROVED" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:CheckBoxField>
                    <asp:BoundField DataField="APPROVED_BY" HeaderText="Approved By"
                        SortExpression="APPROVED_BY" >
                    <HeaderStyle ForeColor="Maroon" />
                    </asp:BoundField>
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource3" runat="server"
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                SelectCommand="SELECT     EMPLOYEE.FIRST_NAME, EMPLOYEE.LAST_NAME, EMPLOYEE.SSN, TRANSACTION_TYPES.TRANSACTION_DESCRIPTION,
                      TRANSACTIONS.NUMBER_OF_UNITS, TRANSACTIONS.TRANSACTION_AMOUNT, convert(varchar,TRANSACTIONS.ENTRY_DATE,106), convert(varchar,TRANSACTIONS.TRANSACTION_DATE,106),
                      TRANSACTIONS.ENTERED_BY, TRANSACTIONS.COMMENTS, TRANSACTIONS.APPROVED, TRANSACTIONS.APPROVED_BY
FROM         EMPLOYEE INNER JOIN
                      PAY_PERIODS INNER JOIN
                      TRANSACTIONS ON PAY_PERIODS.PERIOD_ID = TRANSACTIONS.PAY_PERIOD_ID INNER JOIN
                      TRANSACTION_TYPES ON TRANSACTIONS.TRANSACTION_TYPE = TRANSACTION_TYPES.TRANSACTION_TYPE ON
                      EMPLOYEE.EMPLOYEE_ID = TRANSACTIONS.EMPLOYEE_ID
WHERE     (PAY_PERIODS.FROM_DATE = @FROM_DATE) AND (PAY_PERIODS.TO_DATE = @TO_DATE) AND (PAY_PERIODS.DISTRICT = @DISTRICT)
ORDER BY EMPLOYEE.FIRST_NAME, EMPLOYEE.LAST_NAME, TRANSACTIONS.ENTRY_DATE DESC">
                <SelectParameters>
                    <asp:Parameter Name="FROM_DATE" />
                    <asp:Parameter Name="TO_DATE" />
                    <asp:Parameter Name="DISTRICT" />
                </SelectParameters>
     </asp:SqlDataSource>

Perhaps you can give a little more guidance what I can substitute for {datasource_collection_type_such_as_Person}.

0
Kelvin McDanielSr. DeveloperCommented:
Try this...

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

        Dim record As DataRowView = e.Row.DataItem

        If Not IsDBNull(record.Item("{field_name_to_look_for}")) Then

            Dim tb As LinkButton = e.Row.Cells(0).Controls(0)
            tb.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
sib_userAuthor Commented:
Works like a charm, azarc3.  I used it to disable both my edit and delete buttons for users who don't have the appropriate permissions.

Thank you very much!
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
ASP.NET

From novice to tech pro — start learning today.