[RESOLVED] rebinding a GridView after delete

 

using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;

namespace Maine_Innkeeper_Site_2010.cms.member
{
    public partial class industry_supplier : System.Web.UI.Page
    {

        // establish the db connection string
        string cnstr = common.dbConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ViewState["SelectedText"] = "All";
                CreateAlphaPagings();
                BindGrid(this.ViewState["SelectedText"].ToString());
            }
        }

        //alphabetical paging
        private void CreateAlphaPagings()
        {
            //create a new datatable to hold the paging content
            DataTable dt = new DataTable();

            if (this.ViewState["Paging"] == null)
            {
                SqlDataAdapter da = new SqlDataAdapter("paging_sproc_suppliers", cnstr);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.Add("@OptionId", SqlDbType.Int).Value = 2;
                da.Fill(dt);
                this.ViewState["Paging"] = dt;
            }
            else
                dt = (DataTable)this.ViewState["Paging"];

            DataList1.DataSource = dt;
            DataList1.DataBind();
        }

        private void BindGrid(string StartAlpha)
        {
            string sql = "";

            if (StartAlpha == "All")
                sql = "SELECT id, organization_name, date_added FROM industry_suppliers ORDER BY organization_name";
            else
                sql = "SELECT id, organization_name, date_added FROM industry_suppliers WHERE organization_name LIKE '" + StartAlpha + "%' ORDER BY organization_name";

            SqlDataAdapter da = new SqlDataAdapter(sql, cnstr);
            DataTable dtSelect = new DataTable();
            da.Fill(dtSelect);

            GridView1.DataSource = dtSelect;
            GridView1.DataBind();
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            LinkButton lbkbtnPaging = (LinkButton)e.CommandSource;
            BindGrid(e.CommandArgument.ToString());
            this.ViewState["SelectedText"] = e.CommandArgument.ToString();
            CreateAlphaPagings();
        }

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                if (this.ViewState["SelectedText"] != null)
                {
                    LinkButton lbkbtnPaging = (LinkButton)e.Item.FindControl("lnkbtnPaging");
                    if (this.ViewState["SelectedText"].ToString() == lbkbtnPaging.Text)
                        lbkbtnPaging.Enabled = false;
                }
            }
        }

        //delete the row selected from the database table
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //first declare a new instance of the TableAdapter
            App_Code.DataAccessLayer.MaineInnsDataset maineinnsDataset = new App_Code.DataAccessLayer.MaineInnsDataset();
            App_Code.DataAccessLayer.MaineInnsDatasetTableAdapters.industry_suppliersTableAdapter Industry_SuppliersTableAdapter =
            new App_Code.DataAccessLayer.MaineInnsDatasetTableAdapters.industry_suppliersTableAdapter();
            
            //establish the id parameter from the GridView row
            int id = (int)GridView1.DataKeys[e.RowIndex].Value;
            
            //now delete the record
            Industry_SuppliersTableAdapter.DeleteSupplier(id);
        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            // rebind the GridView
            GridView1.DataBind();

            //display a "success" message
            Label1.Text = "Supplier deleted.";
            Label1.Visible = true;
        }
    }
}

I created a GridView that uses a custom paging method that allows for alphabetical paging. I seem to be having some problems now rebinding the GridView after a delete occurs. The row is deleted, it's just that the rebinding doesn't happen so to the user it looks like the delete failed. Been over and over my code and can't understand why. See the final block which is supposed to fire on the GridView1_ItemDeleted event.

 

Looks like you're not using a data source control. 

Provide the DataSource before you execute .DataBind() 

* like you've done with the BindGrid() method, OR call it instead

Thanks. So you mean, instead of using this in that last code block:

// rebind the GridView
GridView1.DataBind();

Do this:

BindGrid(string StartAlpha);

Is that correct? So in effect I'm calling the "BindGrid" method again after the RowDeleted event fires.

Apparently I'm not calling the BindGrid method correctly, as Visual Studio is telling me "invalid expression term 'String'"

How do I correctly call BindGrid here? Thanks.

Mister Ed

BindGrid(string StartAlpha);

right, but you need to pass the value, which value you're storing in the ViewState variable 

see, as:

BindGrid(this.ViewState["SelectedText"].ToString());

Thank you very much.

It's still not rebinding after the delete occurs (and I can see that the row is deleted from the db table), and I notice that the "success" message is also not appearing. Which leads me to suspect my code is somehow not firing with the RowDeleted event. I've gone over it several times and everything appears correct, though--or am I missing something obvious here?

The Gridview attributes specify a method to employ for the OnRowDeleted event:

OnRowDeleted="GridView1_RowDeleted"

And then the method in Code behind is this:

 protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            // rebind the GridView
            BindGrid(this.ViewState["SelectedText"].ToString());

            //display a "success" message
            Label1.Text = "Supplier deleted.";
            Label1.Visible = true;
        }

Why isn't this firing?

thoughts on "[RESOLVED] rebinding a GridView after delete"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>