Next


GridView in-depth

• This includes with generating “Select” / “Delete” / “Edit” buttons in the “GridView” control. GridView Note: In the Properties of “GridView1”, select “Columns” property. There, add “Edit, Update, Cancel”, “Select” and “Delete” buttons by clicking on “Add” button. Finally click on OK.
Note: At run time, whenever the user clicks on the command field buttons (like Edit, Update, Cancel, Select, Delete), then the respective events will be executed as follows:r>
GridViewindepth

1) “Select” – “SelectedIndexChanged” event.
2) “Edit”  “RowEditing” event
3) “Update”  “RowUpdating” event.
4) “Cancel”  “RowCancelingEdit” event.
5) “Delete”  “RowDeleting” event.



using System.Data.SqlClient;
    private void ShowData()
    {
        string cnstr = "data source=.;user id=sa;password=123;initial catalog=sample";
        SqlConnection cn = new SqlConnection(cnstr);
        string sqlstr = "select * from students";
        SqlDataAdapter adp = new SqlDataAdapter(sqlstr, cn);
        DataSet ds = new DataSet();
        DataTable dt;
        cn.Open();
        adp.Fill(ds);
        dt = ds.Tables[0];
        GridView1.DataSource = dt;
        GridView1.DataBind();
        cn.Close();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
            ShowData();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int n = GridView1.SelectedIndex;
        lblSelectedStudent.Text = "Selected Student: " + GridView1.Rows[n].Cells[4].Text;
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        ShowData();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string cnstr = "data source=.;user id=sa;password=123;initial catalog=sample";
        SqlConnection cn = new SqlConnection(cnstr);
        TextBox txtBox1 = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
        TextBox txtBox2 = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
        TextBox txtBox3 = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0];
        string sqlstr = "update students set name='" + txtBox2.Text + "',marks=" + txtBox3.Text + " where studentid=" + txtBox1.Text;
        SqlCommand cmd = new SqlCommand(sqlstr, cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
        GridView1.EditIndex = -1;
        ShowData();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        ShowData();        
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string cnstr = "data source=.;user id=sa;password=123;initial catalog=sample";
        SqlConnection cn = new SqlConnection(cnstr);
        int stuid = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[3].Text);
        string sqlstr = "delete from students where studentid=" + stuid;
        SqlCommand cmd = new SqlCommand(sqlstr, cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
        ShowData();
    }