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?
 
Kelvin McDanielConnect With a Mentor Sr. Developer/ConsultantCommented:
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
 
Kelvin McDanielSr. Developer/ConsultantCommented:
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
 
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
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.

All Courses

From novice to tech pro — start learning today.