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)
tcannonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
tcannonConnect With a Mentor Author Commented:
I decided to store the data that I needed to delete the row in query string, then pass that into my delete function. Kind of a round about way, but it got the job done.
0
 
cmhuntyCommented:
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)?
0
 
tcannonAuthor Commented:
I get the value for PID, but no value for the label CPN
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
cmhuntyCommented:
When you get to that line, does FindControl("lblCPN") find anything or is it equal to nothing?
0
 
tcannonAuthor Commented:
It is equal to nothing.
0
 
Vee_ModCommented:
Closed, 250 points refunded.
Vee_Mod
Community Support Moderator
0
All Courses

From novice to tech pro — start learning today.