locked
how do i delete a row in GridView using the delete hyperlink colum

    Obecná diskuse

  • Hi everyone. Am developing a web app in VB and am having a problem with the gridview tool

    I have a gridview that contains data from a database somewhere in a sql server, so far so good, what i want to be able to do next is delete a row when i press the delete...Am new to asp.net and vb.net and really apreciate ur help

    i will send the code and a printscree ofthe software:

    <%@ Control Language="VB" AutoEventWireup="false" CodeFile="Admin.ascx.vb" Inherits="Admin" %>
    002	<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
    003	<link href="Styles/main.css" rel="stylesheet" type="text/css" />
    004	<link href="Styles/Site.css" rel="stylesheet" type="text/css" />
    005	<style type="text/css">
    006	    .formulario > .campo > .descr { width:187px; }
    007	     
    008	    .formulario > .campo > input { height: 18px;
    009	                                   margin: 0;
    010	                                   padding: 0;
    011	                                   border: 1px solid #7FC514;
    012	                                   background-color: transparent;}
    013	    .formulario > .campo > select { height:20px;
    014	                                    margin:0;
    015	                                    padding:0;
    016	                                    border:1px solid #7FC514;
    017	                                    background-color:transparent;
    018	                                    font-family:Verdana;
    019	                                    font-size:11px;
    020	                                    color:#666666; }
    021	    .input
    022	    {
    023	        width: 397px;
    024	        height: 82px;
    025	    }
    026	    .formulario_spacer
    027	    {
    028	        width: 170px;
    029	    }
    030	    .campo
    031	    {
    032	        width: 304px;
    033	    }
    034	    .formulario
    035	    {
    036	        width: 476px;
    037	        margin-right: 175px;
    038	        height: 393px;
    039	    }
    040	    #Button2
    041	    {
    042	        height: 23px;
    043	        width: 759px;
    044	    }
    045	    #Button3
    046	    {
    047	        height: 27px;
    048	        width: 322px;
    049	        margin-top: 0px;
    050	    }
    051	    .simular
    052	    {
    053	        width: 320px;
    054	    }
    055	    .campo2
    056	    {
    057	        height: 93px;
    058	        width: 425px;
    059	    }
    060	</style>
    061	 <asp:ScriptManager ID="ScriptManager1" runat="server">
    062	              </asp:ScriptManager>
    063	 
    064	 
    065	                <asp:SqlDataSource ID="MyDataSource1" runat="server"
    066	 
    067	       ConnectionString="<%$ ConnectionStrings:BdCvesConnectionString %>" 
    068	 
    069	         
    070	    SelectCommand="SELECT [nome], [apelido], [area_interesse], [Estado] FROM [candidato]">
    071	 
    072	     </asp:SqlDataSource>
    073	 
    074	 
    075	<asp:Menu ID="Menu1" runat="server" BackColor="#F7F6F3"
    076	        DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em"
    077	        ForeColor="#7C6F57" Orientation="Horizontal" StaticSubMenuIndent="10px">
    078	        <DynamicHoverStyle BackColor="#7C6F57" ForeColor="White" />
    079	        <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
    080	        <DynamicMenuStyle BackColor="#F7F6F3" />
    081	        <DynamicSelectedStyle BackColor="#5D7B9D" />
    082	        <DynamicItemTemplate>
    083	            <%# Eval("Text") %>
    084	        </DynamicItemTemplate>
    085	        <Items>
    086	            <asp:MenuItem Text="Home" Value="Home"></asp:MenuItem>
    087	            <asp:MenuItem Text="File" Value="File">
    088	                <asp:MenuItem Text="sair" Value="New Item"></asp:MenuItem>
    089	            </asp:MenuItem>
    090	            <asp:MenuItem Text="Edit" Value="Edit">
    091	                <asp:MenuItem Text="Estado" Value="Estado"></asp:MenuItem>
    092	                <asp:MenuItem Text="Apagar" Value="Apagar"></asp:MenuItem>
    093	            </asp:MenuItem>
    094	            <asp:MenuItem Text="View" Value="View">
    095	                <asp:MenuItem Text="Nome" Value="Nome"></asp:MenuItem>
    096	                <asp:MenuItem Text="Apelido" Value="Apelido"></asp:MenuItem>
    097	                <asp:MenuItem Text="Sexo" Value="Sexo"></asp:MenuItem>
    098	                <asp:MenuItem Text="Cidade" Value="Cidade"></asp:MenuItem>
    099	                <asp:MenuItem Text="Provincia" Value="Provincia"></asp:MenuItem>
    100	            </asp:MenuItem>
    101	            <asp:MenuItem Text="Help" Value="Help">
    102	                <asp:MenuItem Text="Suport" Value="Suport"></asp:MenuItem>
    103	                <asp:MenuItem Text="About" Value="About"></asp:MenuItem>
    104	            </asp:MenuItem>
    105	        </Items>
    106	        <StaticHoverStyle BackColor="#7C6F57" ForeColor="White" />
    107	        <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
    108	        <StaticSelectedStyle BackColor="#5D7B9D" />
    109	        <StaticItemTemplate>
    110	            <%# Eval("Text") %>
    111	        </StaticItemTemplate>
    112	    </asp:Menu>
    113	 
    114	<asp:Panel ID="Panel1" runat="server" Height="300px" Width="582px"
    115	    HorizontalAlign="Center">
    116	    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
    117	        DataSourceID="MyDataSource1" Height="83px" Width="546px"
    118	        AllowSorting="True">
    119	        <Columns>
    120	            <asp:BoundField DataField="nome" HeaderText="nome" SortExp<b></b>ression="nome" />
    121	            <asp:BoundField DataField="apelido" HeaderText="apelido"
    122	                SortExp<b></b>ression="apelido" />
    123	            <asp:BoundField DataField="area_interesse" HeaderText="area_interesse"
    124	                SortExp<b></b>ression="area_interesse" />
    125	            <asp:BoundField DataField="Estado" HeaderText="Estado"
    126	                SortExp<b></b>ression="Estado" ReadOnly="True" />
    127	            <asp:CommandField ShowDeleteButton="True"
    128	                SortExp<b></b>ression=Apagar DeleteText="Apagar" DeleteImageUrl="~/images/1335535123_Remove-Male-User.png"
    129	                HeaderImageUrl="~/images/1335535123_Remove-Male-User.png" />
    130	        </Columns>
    131	    </asp:GridView>
    132	 
    133	                    
    134	 
    135	    
    136	     
    137	</asp:Panel>

    Imports System.Data.SqlClient
    Imports System.Data
    Imports System
    Imports Microsoft.VisualBasic
    Imports System.Web.Security
    Imports System.Security
    Imports System.Text.RegularExpressions
    
    
    Partial Class Admin
        Inherits System.Web.UI.UserControl
        Public SqlCon As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("Ligacao"))
    
        Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
    
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemClick
    
        End Sub
    
    
        Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
            'Dim connstring As String = "Data Source=10.2.24.29;Persist Security Info=True;User ID=sa;Password=Pr0dUn1C0$qL;database=BdCves"
            'Dim conn As New SqlConnection(connstring)
            'conn.Open()
            'SqlCon.Open()
            'Dim intrans As SqlClient.SqlTransaction = conn.BeginTransaction
            'Try
            '    Dim Cmd As SqlCommand = Nothing
            '    ' Dim InsStr As String = " delete from candidato
            '    '    Cmd = New SqlCommand(InsStr)
            '    Cmd.Transaction = intrans
            '    Cmd.Connection = conn
            '    Cmd.ExecuteNonQuery()
    
            '    intrans.Commit()
            '    MsgBox("Deletado")
            'Catch ex As Exception
            '    MsgBox(ex.Message())
            '    intrans.Rollback()
            'Finally
            '    SqlCon.Close()
            '    conn.Close()
            'End Try
    
    
            Dim row As DataRow = tabela.Rows(index)
            tabela.Rows.Remove(row)
        End Sub
    End Class
    

    30. dubna 2012 9:07