14 Replies - 2350 Views - Last Post: 15 July 2012 - 03:57 PM Rate Topic: -----

#1 Memsey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-July 12

Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 12:41 PM

I would like to be able to show the delete button for rows that satisfy a certain condition. I can only find c examples of how to do this online. I would like to know how to write the following in vb.net:

protected void gvInvolves_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
{ 
    
    if (e.Row.RowType == DataControlRowType.DataRow) { 
        if (string.IsNullOrEmpty(e.Row.Cells(e.Row.Cells.Count - 1).Text)) { 
            e.Row.Cells(0).Visible = true; 
            e.Row.Cells(1).Visible = false; 
        } 
        else { 
            e.Row.Cells(0).Visible = false; 
            e.Row.Cells(1).Visible = true; 
        } 
        
    } 
} 



any help or guidance would be appreciated! thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Allowing 'delete' only in certain rows of grid view

#2 Memsey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-July 12

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 12:48 PM

I basically want to allow users to delete their own comments on a blog I've made.

My pseudo code is:

  Protected Sub GridViewComments_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewComments.RowDataBound

        If GridViewComments.Columns(1) = Session("UserID") Then
            show delete button for row
        Else
            do not show delete button for row
        End If
    End Sub


Was This Post Helpful? 0
  • +
  • -

#3 Memsey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-July 12

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 01:58 PM

I have the following code but am getting the error "Input string in wrong format"

   Protected Sub GridViewComments_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewComments.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            If Convert.ToInt16(e.Row.Cells(1).Text) = Session("UserID") Then

                e.Row.Cells(6).Visible = True

            Else

                e.Row.Cells(6).Visible = False

            End If

        End If

    End Sub



any ideas? I feel like I'm pretty close to getting this one. Would just appreciate some guidance from anyone who knows more on the subject. Thanks again :)
Was This Post Helpful? 0
  • +
  • -

#4 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 02:32 PM

I think you are trying to convert value that can't be converted to Int16:
Convert.ToInt16(e.Row.Cells(1).Text)
If e.Row.Cells(1).Text is empty or it has some other value that can't be converted to Int16 then you will get error.

I recommend you to use Int32.TryParse Method
Was This Post Helpful? 0
  • +
  • -

#5 Memsey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-July 12

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 02:42 PM

no luck with tryparse :(
Was This Post Helpful? 0
  • +
  • -

#6 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 02:46 PM

Quote

no luck with tryparse

What is happening now? did you get the same error?
Was This Post Helpful? 0
  • +
  • -

#7 Memsey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-July 12

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 02:59 PM

won't compile

"Error 2 Overload resolution failed because no accessible 'TryParse' accepts this number of arguments."

I've also tried converting the UserID to string but no luck there either :S
Was This Post Helpful? 0
  • +
  • -

#8 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 03:01 PM

Quote

Error 2 Overload resolution failed because no accessible 'TryParse' accepts this number of arguments.
you are doing something wrong. Post your code.
Was This Post Helpful? 0
  • +
  • -

#9 Memsey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-July 12

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 03:13 PM

<asp:GridView ID="GridViewComments" runat="server" 
        style="position:absolute; top: 186px; left: 517px;" 
        AutoGenerateColumns="False" EmptyDataText="No Comments" 
        CssClass="wrap" Width="350px" DataKeyNames="CommentID" 
        DataSourceID="datasrcComments" OnRowDataBound="GridViewComments_RowDatabound">
        <RowStyle CssClass="bottomBorderGray"/>
        <Columns>
            <asp:BoundField DataField="ProfileID" HeaderText="ProfileID" 
                SortExpression="ProfileID" Visible="False" />
            <asp:BoundField DataField="UserID" HeaderText="UserID" 
                SortExpression="UserID" Visible="False" />
            <asp:BoundField DataField="CommentID" HeaderText="CommentID" 
                SortExpression="CommentID" InsertVisible="False" ReadOnly="True" 
                Visible="False" />
            <asp:BoundField DataField="Username" 
                SortExpression="Username" >
            <ItemStyle Width="100px" />
            </asp:BoundField>
            <asp:BoundField DataField="Comment" HeaderText="Comments" 
                SortExpression="Comment" >
            <ItemStyle Width="200px" />
            </asp:BoundField>
            <asp:BoundField DataField="Timestamp" SortExpression="Timestamp">
            <ItemStyle Width="50px" />
            </asp:BoundField>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="btnDeleteComment" runat="server" CausesValidation="False" 
                        CommandName="Delete" Text="Delete"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle CssClass="headerBorder" />
        <AlternatingRowStyle CssClass="bottomBorderGray" />
    </asp:GridView>



That is the code for my grid view. I will post up the code for my data source too

<asp:SqlDataSource ID="datasrcComments" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT tblComments.ProfileID, tblComments.UserID, tblComments.CommentID, tblUsers.Username, tblComments.Comment, tblComments.Timestamp
FROM tblUsers, tblComments
WHERE tblUsers.UserID = tblComments.UserID
AND
ProfileID = @ProfileID
ORDER BY tblComments.Timestamp ASC" DeleteCommand="DELETE FROM tblComments
WHERE (CommentID = @CommentID)">
        <DeleteParameters>
                <asp:ControlParameter ControlID="GridViewComments" Name="CommentID" 
                    PropertyName="SelectedValue"/>
            </DeleteParameters>
        <SelectParameters>
            <asp:SessionParameter Name="ProfileID" 
                SessionField="ProfileID" />
        </SelectParameters>
    </asp:SqlDataSource>



I just don't understand how to manipulate the cells of my grid view at run time in order to only show the delete link button on the comments that belong to the logged in user :S thanks for taking time to look at this! much appreciated!
Was This Post Helpful? 0
  • +
  • -

#10 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 03:17 PM

no I want to see the code where you using TryParse method. Because you use it wrong.
Was This Post Helpful? 0
  • +
  • -

#11 Memsey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-July 12

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 03:21 PM

Oh right - sorry.

I just used

Protected Sub GridViewComments_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewComments.RowDataBound

     If e.Row.RowType = DataControlRowType.DataRow Then
         If Int16.TryParse(e.Row.Cells(1).Text) = Session("UserID") Then
             e.Row.Cells(6).Visible = True
         Else
             e.Row.Cells(6).Visible = False
         End If
     End If
 End Sub



Was This Post Helpful? 0
  • +
  • -

#12 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 03:34 PM

here you can find example how to use TryParse method.
Was This Post Helpful? 0
  • +
  • -

#13 Memsey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-July 12

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 03:48 PM

thats the same link you posted to me earlier?
Was This Post Helpful? 0
  • +
  • -

#14 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 03:50 PM

View PostMemsey, on 15 July 2012 - 03:48 PM, said:

thats the same link you posted to me earlier?
yes
Was This Post Helpful? 0
  • +
  • -

#15 Memsey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-July 12

Re: Allowing 'delete' only in certain rows of grid view

Posted 15 July 2012 - 03:57 PM

I've found a way around the issue that works perfectly. In the end I just used a string type on the right hand side of the equals:

If (e.Row.Cells(3).Text) = Session("Username") Then



where Session("Username") is a string value.

Thanks for taking the time to look at this issue sela007! appreciated
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1