Learn GridView - Part IV (Deleting Mode)

hi, now we are going to learn how we delete data from gridview


protected void GridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
int id = (int)GridView1.DataKeys[e.RowIndex].Value;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand("delete from sqltest where CustId='"+id+"'",con);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();

GridView1.EditIndex = -1;
BindData();
}

Learn GridView - Part III (Updating Mode)

now we are going to learn how we update the gridview

see this:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = (int)GridView1.DataKeys[e.RowIndex].Value;

GridViewRow row = GridView1.Rows[e.RowIndex];

string name = ((TextBox)row.FindControl("TextBox1")).Text;
string city = ((TextBox)row.FindControl("TextBox2")).Text;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand("update sqltest set Name='" + name + "',City='" + city + "' where CustId='" + id + "'",con);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();

GridView1.EditIndex = -1;
BindData();
}

please give your feedback..........

Learn GridView - Part III (Changing Mode)

hi friends once again i am with you.

now we are going to learn how we change the mode of the grid view.
please remember read my post from first to this to learn the grid view in simplest manner.

how you display the edit link on the gridview.
in Default.aspx click on the gridview now goes to property of it there is an AutoGenerateEditButton make it true.

please read and write the coding below :

protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData(); // please see the Part - I for this coding
}

protected void GridView1_RowCancelling(object sender, GridViewCancelEditEventArgs e)
{
GridView.EditIndex = -1;
BindData(); // please see the Part - I for this coding
}
hope this help you :)

Learn GridView - Part II (Changing PageIndex)

in this we learn how we change the page index of the grid view.

only write the coding in Default.aspx.cs

protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData(); // define the coding in previous post Learn GridView - Part-I
}

Learn GridView - Part I

hi this is zubair, i going to teach gridview with easiest way.

First Step: first i describe you about our database in sql server 2005.
Database name is publicDB, Table name is sqltest which have two columns Name and City.

Second Step:
Open the visual web developer 2005.
click on new tab -> select new website -> give it name as u wish.

Third Step:
now on the default.aspx page drag the gridview control from the control box.

write the code in web.config as follows:


connectionString="Server ='YourServerName'
Database='publicDb'
Trusted_Connection='True' "
providerName="System.Data.SqlClient" />


Fourth Step:

now we are going to write the coding in Default.aspx.cs
open the Default.aspx.cs and write the code below.

protected void Page_Load(object sender, EventArgs e)
{
BindData();
}

public void BindData()
{
SqlConnection con = new SqlConnection(ConfiguratonManager.ConnectionStrings["ConnectionString"].ConnectionString);

SqlDataAdapter ad = new SqlDataAdapter("Select * from sqltest",con);

DataSet ds = new DataSet();

ad.Fill(ds);

GridView1.DataSource = ds;
GridView1.DataBind();

}