Link to home
Start Free TrialLog in
Avatar of tcannon
tcannonFlag for United States of America

asked on

How To Delete a Row in GridView?

How do I delete a row in a GridView?

Here's my code so far:
.aspx file

<asp:GridView id="gvProjCapitalNo"
      Runat="Server"
      AutoGenerateColumns="False"
      ShowFooter="True"
      Caption="<b></b>"
      CellPadding="0"
      BorderStyle="None"
      BorderWidth="1px"
      BackColor="White"
      Font-Size="10pt"
      EditRowStyle-HorizontalAlign="Left"
      HeaderStyle-BackColor="#707070"
      HeaderStyle-ForeColor="#FFFFFF"
      AlternatingRowStyle-ForeColor="#FFFFFF"
      AlternatingRowStyle-BackColor="#A0A0A0"
      FooterStyle-BackColor="#707070"
      FooterStyle-ForeColor="#FFFFFF"
      FooterStyle-Font-Size="7pt"
      AllowPaging="false"
      AllowSorting="true"
      Font-Names="Verdana"
      Width="350px"
      PageSize="25"
      BorderColor="#E7E7FF"
      GridLines="Horizontal"
      DataKeyNames="PROJ_REF_NBR">

  <Columns>
 
  <asp:CommandField ShowDeleteButton="true" />  
   
  <asp:BoundField
    DataField="PROJ_REF_NBR"
     Visible="false"
    HeaderText="Project ID">
      <FooterStyle BorderWidth="0px" />
  </asp:BoundField>
 
  <asp:TemplateField HeaderText="CAPITAL PROJECT NUMBER">
    <ItemTemplate>
        <asp:Label ID="lblCPN" runat="server"> <%#Container.DataItem("CAPITAL_PROJ_NBR")%></asp:Label>
    </ItemTemplate>    
  </asp:TemplateField>
 
  </Columns>
     <FooterStyle BackColor="#B5C7DE" Font-Size="7pt" ForeColor="#4A3C8C" />
     <HeaderStyle BackColor="#4A3C8C" ForeColor="#F7F7F7" Font-Bold="True" />
     <AlternatingRowStyle BackColor="#F7F7F7" ForeColor="#4A3C8C" />
     <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
     <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
     <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />

</asp:GridView>

.aspx.vb file code:

Protected Sub gvProjCapitalNo_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvProjCapitalNo.RowDeleting

        Dim PID As Integer = gvProjCapitalNo.DataKeys(0).Value
        Dim lCPN As Label = CType(gvProjCapitalNo.FindControl("lblCPN"), Label)

        ProjectDAC.ProjCapitalFundingDelete(lCPN.Text, PID)

    End Sub

ERROR MESSAGE IS: "Object reference not set to an instance of an object" on this function - ProjectDAC.ProjCapitalFundingDelete(lCPN.Text, PID)
Avatar of cmhunty
cmhunty

The error is in ProjectDAC.ProjCapitalFundingDelete or on the line that calls it?

Can you run the debugger and find the values of PID and ICPN.Text when you hit the line
ProjectDAC.ProjCapitalFundingDelete(lCPN.Text, PID)?
Avatar of tcannon

ASKER

I get the value for PID, but no value for the label CPN
When you get to that line, does FindControl("lblCPN") find anything or is it equal to nothing?
Avatar of tcannon

ASKER

It is equal to nothing.
ASKER CERTIFIED SOLUTION
Avatar of tcannon
tcannon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Closed, 250 points refunded.
Vee_Mod
Community Support Moderator