Ron
Ron

Reputation: 1931

Rowdeleteing event in Gridview

I have been trying to write this simple logic of deleting a row from the gridview and from the SQL database of the selected row, but keep getting a null reference to cell, which has the primary key [ID] in a field.

Here's my html:

  <asp:GridView ID="grvInventoryEdit" runat="server" BackColor="White" onrowdeleting="grvInventoryEdit_RowDeleting" 
                onrowediting="grvInventoryEdit_RowEditing" 
                onrowupdating="grvInventoryEdit_RowUpdating">
<Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:CommandField ShowDeleteButton="True" /><asp:TemplateField HeaderText="Id">
                       <ItemTemplate>
                           <%#Eval("No")%>
                       </ItemTemplate>
                       <EditItemTemplate>
                           <asp:TextBox runat="server" ID="txtEditNo" ReadOnly="True" Text='<%#Eval("No")%>'></asp:TextBox>
                       </EditItemTemplate>
                   </asp:TemplateField>........ </Columns> </asp:GridView>

And my back-end code for rowdeleting event is :

protected void grvInventoryEdit_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
      TextBox id = (TextBox)grvInventoryEdit.Rows[e.RowIndex].FindControl("txtEditNo");

      Asset asset = db.Assets.Single(a => a.No == Convert.ToInt32(id));            

        db.Assets.DeleteOnSubmit(asset);
        db.SubmitChanges();

        binddata();
    }

and when the event fires, this is what i am seeing while debugging:Picture Below

I am not sure why i am getting a null value ,though, there is a value for that cell.

Could you tell me what i am doing wrong ?

Regards,

Upvotes: 0

Views: 332

Answers (4)

Ron
Ron

Reputation: 1931

I guess, in my HTML, i have only the value that's bound to the item, is being displayed, and there are no textboxes in my <ItemTemplate>, hence, it pulls null value. Whereas, in my <EditItemTemplate> there is a textbox, which can pull the value from the cell.

So I changed my HTML to this:

<asp:TemplateField HeaderText="Id">
   <ItemTemplate>`<asp:label runat="server" ID="lblEditNo"  ReadOnly="True" Text='<%#Eval("No")%>'></asp:label>
   </ItemTemplate>
   <EditItemTemplate>
     <asp:TextBox runat="server" ID="txtEditNo" ReadOnly="True" Text='<%#Eval("No")%>'></asp:TextBox>
    </EditItemTemplate>
 </asp:TemplateField>    

and no change in my codebehind, and this resolved the issue.

Upvotes: 0

fnostro
fnostro

Reputation: 4591

  1. why are you adding a second commandfield instead of just enabling the delete button on the existing one.
  2. if you are using a command field you should be supplying an compatible datasource that provides Delete functionality
  3. if you're "rolling your own" delete functionality then just use a regular Button control and supply a CommandName and CommandArgument, such as CommandName="MyDelete" CommandArgument=<row number> where <row number> is supplied via GridView RowDataBound() event.
  4. Regardless of how you choose to implement Delete you should be placing the key field in the GridView DataKeys Property and not as a field within each row. This will make obtaining the PK far easier than what you are trying to do

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

You are passing TextBox object instead instead of Text property of TextBox

Asset asset = db.Assets.Single(x=>x.No == Convert.ToInt32(id.Text));

and your TextBox is also coming null means it's unable to find it in GridView, try like this:

TextBox id = e.Row.FindControl("txtEditNo");

Also see this CodeProject article to understand how to use ItemTemplate and EditItemTemplate

Upvotes: 1

Ali Adravi
Ali Adravi

Reputation: 22833

Might be it is due to the readonly property of textbox, not suer.

If you want to use the image button for edit and delete then use

protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
{
    GridViewRow gvRow = (GridViewRow)((ImageButton)sender).NamingContainer;
    Int32 UserId = Convert.ToInt32(gvUsers.DataKeys[gvRow.RowIndex].Value);
    // delete and hide the row from grid view
    if (DeleteUserByID(UserId))
        gvRow.Visible = false;
}

For complete code see

Upvotes: 1

Related Questions