Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView.DeleteRow(row.index)

Status
Not open for further replies.

jrenae

Programmer
Jan 18, 2006
142
US
Hello,

I'm trying to delete a row from my gridview which seems to work fine, but it doesn't actually delete the row. The code below actually finds the correct row and does the DeleteRow method with no problem. But it doesn't refresh the grid. How can I refresh the grid? I did a DataBind() again, but then it had nothing in it. While I'm looping thru the rows and I actually call the DeleteRow method, I check the count of the rows and the count stays the same. What am I missing?

Thanks in advance.

Code:
foreach (GridViewRow row in GridView1.Rows)
{
  if (accountId == row.Cells[1].Text)
  { 
    GridView1.DeleteRow(row.RowIndex);
  }
}

protected void GridView1_OnRowDeleting(object sender, EventArgs e)
    {
        
    }

 
I might add that when I'm deleting the row, I'm not actually deleting it from the database, just the grid. I'll take care of the database in my business layer later on. I will try to Bind the grid again after retrieving the modified datasource and re-assigning it to the gridview.
 
Prior to now I have not used the DeleteRow method of a GridView. However, I was able to put together the following example which seems to be working as expected. There may be a better way to accomplish the same result, but this is how I was able to get it working... For my example I used a DataTable for the DataSource... Below is the markup and codebehind...

Markup...
Code:
<asp:GridView ID="gvRowDelete" runat="server" AutoGenerateColumns="false" OnRowDeleting="gvRowDelete_RowDeleting">
   <Columns>
      <asp:BoundField DataField="id" HeaderText="ID" />
      <asp:TemplateField>
         <ItemTemplate>
            <asp:Button ID="btnDelete" runat="server" Text="Delete" CommandArgument='<%#DataBinder.Eval(Container,"DataItem.id")%>' OnClick="btnDelete_Click" />
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>

CodeBehind...

Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("id");
        for (int i = 0; i < 5; i++)
        {
            DataRow dr = dt.NewRow();
            dr["id"] = i.ToString();
            dt.Rows.Add(dr);
        }
        gvRowDelete.DataSource = dt;
        gvRowDelete.DataBind();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Button b = (Button)sender;
        int id = int.Parse(b.CommandArgument);
        DeletedRows.Add(id);
        gvRowDelete.DeleteRow(id);
    }
    protected void gvRowDelete_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt = (DataTable)gvRowDelete.DataSource;
        foreach (int i in DeletedRows)
        {
            DataRow dr = FindRow(dt, i);
            dt.Rows.Remove(dr);
        }
        gvRowDelete.DataSource = dt;
        gvRowDelete.DataBind();
    }
    private DataRow FindRow(DataTable dt, int id)
    {
        DataRow dr = null;
        for (int i = 0; i < dt.Rows.Count; i++ )
        {
            DataRow row = dt.Rows[i];
            int rowId = int.Parse(row["id"].ToString());
            if (rowId == id)
                return row;
        }
        return dr;
    }
    public ArrayList DeletedRows
    {
        get
        {
            if (Session["DELETED_ROW_COLLECTION"] == null)
                Session["DELETED_ROW_COLLECTION"] = new ArrayList();
            return (ArrayList)Session["DELETED_ROW_COLLECTION"];
        }
    }

Hope this helps.

 
If you look up in VS Help:
GridView.DeleteRow Method

Deletes the record at the specified index from the data source.

It does not delete from the gridview. You have to delete from your datasource and rebind the grid.
 
Yes, I did read that and tried to rebind the grid. But after the page is reloaded, my datasource is null (disconnected I think?), which causes my grid to have nothing in it on a rebind. I suspect that this is because when I originally bind the grid I bind to a generic IList<UserDefinedType> where UserDefinedType is a business class (entity) for our app. So as a work around, I just make the grid row invisible. Then I deal with it in the business layer.

Thank you, and thanks KDavie for the comprehensive solution you came up with.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top