Monday, February 6, 2012

Delete Edit and Update Gridview Row in Codebehind

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 NSLogger;

public partial class Publishers_Controls_ctrl_UpdateDeleteProduct : System.Web.UI.UserControl
{
    clsAddupdatepublisher searchpublisher = new clsAddupdatepublisher();
    clsUpdatedeleteproducts Updatedeleteproduct = new clsUpdatedeleteproducts();
    clsUtilty error = new clsUtilty();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            Filldropdown();
        }
    }

    protected void imgDelete_Click(object sender, EventArgs e)
    {


        int deleteProductID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
        Updatedeleteproduct.DeleteProduct(deleteProductID);
        GridViewPublisher.DataBind();

    }

    protected void GridViewPublisher_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            int updateProductID = Convert.ToInt32(GridViewPublisher.DataKeys[e.NewEditIndex].Value);
            Session["Updateproductid"] = updateProductID;
            Response.Redirect("CreateProduct.aspx");
        }
        catch (Exception ex)
        {
            Logger.WriteLog(ex.Message, LogLevel.DBLOG);
        }
    }

    protected void GridViewPublisher_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
    }
    protected void GridViewPublisher_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        try
        {
            GridViewPublisher.EditIndex = -1;
            DataSet dtUser = Updatedeleteproduct.SearchProduct(txtproduct.Text, txtDescription.Text, int.Parse(SupplierDropdown.SelectedValue));
            GridViewPublisher.DataSource = dtUser.Tables[0];
            GridViewPublisher.DataBind();
        }
        catch (Exception ex)
        {
            Logger.WriteLog(ex.Message, LogLevel.DBLOG);
        }
    }

    protected void GridViewPublisher_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    }

    protected void GridViewPublisher_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {

            DataSet dtUser = Updatedeleteproduct.SearchProduct(txtproduct.Text, txtDescription.Text, int.Parse(SupplierDropdown.SelectedValue));
            GridViewPublisher.DataSource = dtUser.Tables[0];
            GridViewPublisher.DataBind();
        }
        catch (Exception ex)
        {
            Logger.WriteLog(ex.Message, LogLevel.DBLOG);
        }
    }

    protected void btnAddnew_Click(object sender, EventArgs e)
    {
        Response.Redirect("CreateProduct.aspx?Mode=A");
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
           
            DataSet dtUser = Updatedeleteproduct.SearchProduct(txtproduct.Text, txtDescription.Text, int.Parse(SupplierDropdown.SelectedValue));
            GridViewPublisher.DataSource = dtUser.Tables[0];
            GridViewPublisher.DataBind();
        }
        catch (Exception ex)
        {
            Logger.WriteLog(ex.Message, LogLevel.DBLOG);
        }
    }

    private void Filldropdown()
    {
        try
        {
            DataSet ds = Updatedeleteproduct.dropdownget();
            SupplierDropdown.DataSource = ds.Tables[0];
            SupplierDropdown.DataTextField = "AmpsPublisherCode";
            SupplierDropdown.DataValueField = "pk_SuppID";
            SupplierDropdown.DataBind();
            //DropdownRole.SelectedIndex = 1;
        }
        catch (Exception ex)
        {
            Logger.WriteLog(ex.Message, LogLevel.DBLOG);
        }
    }
}

No comments:

Post a Comment