Thursday 26 July 2012

Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List



using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            showgrid();
        }
    }
    public void showgrid()
    {
        using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
        {
            DataTable dt = new DataTable();
            sqlcon.Open();
            SqlDataAdapter sda = new SqlDataAdapter("select * from employee ", sqlcon);
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        showgrid();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView1.EditIndex = e.NewEditIndex;
        showgrid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        Label lb = (Label)GridView1.Rows[e.RowIndex].FindControl("Label6");
        DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
        RadioButtonList rbl = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList1");
        CheckBoxList chb = (CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("CheckBoxList2");
        TextBox tx1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
        TextBox tx2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
        TextBox tx3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
        using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
        {
            sqlcon.Open();
            string sql = "update employee set emp_name='" + tx1.Text + "',emp_address='" + tx2.Text + "',salary='" +
                tx3.Text + "',department='" + ddl.SelectedValue.ToString() + "',maritalstatus='" +
                rbl.SelectedValue.ToString() + "',Active_status='" + chb.SelectedValue.ToString() + "' where emp_id='" +
                lb.Text + "'";
            using (SqlCommand cmd = new SqlCommand(sql))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = sqlcon;
                cmd.ExecuteNonQuery();
                GridView1.EditIndex = -1;
                showgrid();
            }


        }

    }
    public DataTable load_department()
    {
        using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sd = new SqlDataAdapter("select DeparmentName,DeparmentId from department", sqlcon);
            sd.Fill(dt);
            return dt;

        }

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = e.Row.DataItem as DataRowView;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
             if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList dp = (DropDownList)e.Row.FindControl("ddlOutlet");
                DataTable dt = (DataTable)ViewState["OutletDrpDown"];
                dp.DataSource = dt;
                dp.DataTextField = "DeparmentName";
                dp.DataValueField = " DeparmentId";
                dp.DataBind();
  }
                RadioButtonList rbtnl = (RadioButtonList)e.Row.FindControl("RadioButtonList1");
                rbtnl.SelectedValue = drv[5].ToString();
                CheckBoxList chkb = (CheckBoxList)e.Row.FindControl("CheckBoxList2");
                chkb.SelectedValue = drv[6].ToString();
}
}


            }

        }
    }
}


1 comment:

  1. Thanks bro.. Really helpful, although google is there but practical example have their own meaning.. but you have missed .aspx page bind that page also your code is perfectly running..

    ReplyDelete