locked
How can I delete a row in GridView RowDataBound? RRS feed

  • Question

  • User126014556 posted

    In code page:

    Instead of setting the row to invisible, I would like to delete the row if possible.

    Imports System.Security.Principal
    
    Public Class _default
        Inherits System.Web.UI.Page
    
        Dim appNames As New List(Of String)
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    
            Try
    
                If e.Row.RowType = DataControlRowType.DataRow Then
    
                    Dim hLink As HyperLink = TryCast(e.Row.FindControl("hLnk"), HyperLink)
                    Dim lblApp As Label = TryCast(e.Row.FindControl("lblAppName"), Label)
                    Dim lblGrp As Label = TryCast(e.Row.FindControl("lblGroup"), Label)
                    Dim lblCod As Label = TryCast(e.Row.FindControl("lblCode"), Label)
    
                    If IsInGroup(lblGrp.Text.Trim()) Then
                        If IsDuplicate(lblCod.Text.Trim()) Then
    
                            e.Row.Visible = False
                        Else
                            e.Row.Visible = True
    
                        End If
    
                    Else
                        e.Row.Visible = False
    
                    End If
                End If
    
    
            Catch ex As Exception
    
            End Try
    
        End Sub
    
    
    
        Private Function IsInGroup(ByVal groupName As String) As Boolean
            Dim MyIdentity As WindowsIdentity = WindowsIdentity.GetCurrent()
            Return New WindowsPrincipal(MyIdentity).IsInRole(groupName)
            Return False
        End Function
    
    
        Private Function IsDuplicate(ByVal appName As String) As Boolean
            Dim result As Boolean
            result = (From x In appNames Where x = appName
                      Select x).Any()
            If Not result Then
                appNames.Add(appName)
            End If
            Return result
        End Function
    
    
    End Class

    ASP page:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="APPReps._default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Reports</title>
        
        <style type="text/css">
            td {
                border-width: thin;
                padding-right: 5px;
                padding-left: 5px;
                padding-bottom: 2px;
                border-top-style: solid;
                border-bottom-style: solid;
            }
            a:link {
                color: #FFFFFF;
                text-decoration: none;
            }
            a:visited {
                color: #FFFFFF;
            }
            a:hover {
                text-decoration: underline;
                color: #FFFFFF;
            }
        </style>
           <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
    </head>
    <body style="background-image: url('Images/LOGO.GIF')">
        <form id="form1" runat="server">
        <div>
        <h3 style="font-family:Verdana; color: #006699; font-size: medium; text-align:center">GIC Reports</h3>
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="RepLinkDS" ShowHeader="False" DataKeyNames="Rep_Code" HorizontalAlign="Center" BorderStyle="None" AllowPaging="True" PageSize="25">
                <Columns>
                    <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="NT_GROUP" SortExpression="NT_GROUP" Visible="False">
                        <ItemTemplate>
                            <asp:Label ID="lblGroup" runat="server" Text='<%# Eval("NT_GROUP") %>' Visible="True"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Rep_Code" SortExpression="Rep_Code" Visible="False">
                        <ItemTemplate>
                            <asp:Label ID="lblCode" runat="server" Text='<%# Eval("Rep_Code") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Url" SortExpression="Url">
                        <ItemTemplate>
                            <asp:LinkButton ID="hLnk" runat="server" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Url") %>' Target="_blank" Visible="true"></asp:LinkButton>
                        </ItemTemplate>
                        <ItemStyle BackColor="#006699" Font-Names="Verdana" Font-Size="small" Wrap="False"/>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Rep_Name" HeaderText="Report" SortExpression="Rep_Name" Visible="False">
                    <ItemStyle Font-Names="Verdana" Font-Size="small" ForeColor="Black" Wrap="False" />
                    </asp:BoundField>
                </Columns>
                <PagerSettings FirstPageText="First&amp;nbsp;" LastPageText="&amp;nbsp;Last" Mode="NextPreviousFirstLast" NextPageText="Next" PreviousPageText="Previous" />
                <PagerStyle ForeColor="Navy" HorizontalAlign="Center" Wrap="False" />
            </asp:GridView>
            <br />
    
           </div>
            <asp:SqlDataSource ID="RepLinkDS" runat="server" ConnectionString="<%$ ConnectionStrings:SystemsCS %>" SelectCommand="SELECT [NT_GROUP],[Rep_Code],[Rep_Name],RTRIM([Rep_Web_Link]) As Url FROM [Systems].[dbo].[VW_Rep_Links]"></asp:SqlDataSource>
        </form>
    </body>
    
    </html>
    

    I appreciate any help,

    Thanks in advance

    Sunday, March 31, 2019 10:59 AM

Answers

  • User-1716253493 posted

    Get the id value of the row to delete, use sqldatasource to delete.

    sqldatasource1.DeleteParameters("id").DefaultValue = id
    sqldatasource1.Delete()

    But, deleting duplicate values in that event is not good idea.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 31, 2019 12:00 PM

All replies

  • User-1716253493 posted

    Get the id value of the row to delete, use sqldatasource to delete.

    sqldatasource1.DeleteParameters("id").DefaultValue = id
    sqldatasource1.Delete()

    But, deleting duplicate values in that event is not good idea.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 31, 2019 12:00 PM
  • User126014556 posted

    Thank you for the response.

    I have mistakenly posted incorrect issue.

    Monday, April 1, 2019 4:01 AM