Binding Data to Gridview:
protected void BindData()
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString());
cn.Open();
string selectstatement = @"Select * from tblInstructors";
SqlCommand cmd = new SqlCommand(selectstatement, cn);
gvInstructors.DataSource = cmd.ExecuteReader();
gvInstructors.DataBind();
cn.Close();
}
Row Editing event of Gridview :
protected void gvInstructors_RowEditing(object sender, GridViewEditEventArgs e)
{
gvInstructors.EditIndex = e.NewEditIndex;
BindData();
}
Row Cancelling Edit event of Gridview :
protected void gvInstructors_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvInstructors.EditIndex = -1;
BindData();
}
Row Deleting Event of Gridview :
protected void gvInstructors_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblInstructorId = (Label)gvInstructors.Rows[e.RowIndex].FindControl("lblInstructorId");
try
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString());
cn.Open();
string deletestatement = @"delete from tblInstructors where InstructorId = @InstructorId";
SqlCommand cmd = new SqlCommand(deletestatement, cn);
cmd.Parameters.Add("@InstructorId", SqlDbType.Int);
cmd.Parameters["@InstructorId"].Value = lblInstructorId.Text;
cmd.ExecuteNonQuery();
cn.Close();
gvInstructors.EditIndex = -1;
BindData();
}
catch (Exception ex)
{
ex.ToString();
}
}
Row Updating Event of Gridview:
protected void gvInstructors_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblInstructorId = (Label)gvInstructors.Rows[e.RowIndex].FindControl("lblInstructorId");
TextBox txtInstructorName = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtName");
TextBox txtAddress = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtAddress");
TextBox txtPhone = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtPhone");
TextBox txtAge = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtAge");
TextBox txtGender = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtGender");
TextBox txtRegCar = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtRCar");
TextBox txtRegNumber = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtRegNum");
try
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString());
cn.Open();
string updatestatement = @"Update tblInstructors set Name =@Name, Phone=@Phone, Age=@Age, Gender=@Gender, RegisteredCar=@RegisteredCar, RegisteredNumber=@RegisteredNumber where instructorId=@InstructorId";
SqlCommand cmd = new SqlCommand(updatestatement, cn);
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 100);
cmd.Parameters.Add("@Phone", SqlDbType.BigInt);
cmd.Parameters.Add("@Age", SqlDbType.Int);
cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@RegisteredCar", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@RegisteredNumber", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@InstructorId", SqlDbType.Int);
cmd.Parameters["@Name"].Value = txtInstructorName.Text.Trim();
cmd.Parameters["@Phone"].Value = txtPhone.Text.Trim();
cmd.Parameters["@Age"].Value = Convert.ToInt32(txtAge.Text.Trim());
cmd.Parameters["@Gender"].Value = txtGender.Text;
cmd.Parameters["@RegisteredCar"].Value = txtRegCar.Text.Trim();
cmd.Parameters["@RegisteredNumber"].Value = txtRegNumber.Text.Trim();
cmd.Parameters["@InstructorId"].Value = lblInstructorId.Text;
cmd.ExecuteNonQuery();
cn.Close();
gvInstructors.EditIndex = -1;
BindData();
}
catch (Exception ex)
{
ex.ToString();
}
}
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = System.Web.UI.WebControls.SortDirection.Descending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
protected void gvUsers_SortCommand(object source, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
private void SortGridView(string sortExpression, string direction)
{
UsersBL objUser = new UsersBL();
DataView dv = (DataView)Session["UsersData"];
dv.Sort = sortExpression + direction;
gvUsers.DataSource = dv;
gvUsers.DataBind();
}
protected void BindData()
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString());
cn.Open();
string selectstatement = @"Select * from tblInstructors";
SqlCommand cmd = new SqlCommand(selectstatement, cn);
gvInstructors.DataSource = cmd.ExecuteReader();
gvInstructors.DataBind();
cn.Close();
}
Row Editing event of Gridview :
protected void gvInstructors_RowEditing(object sender, GridViewEditEventArgs e)
{
gvInstructors.EditIndex = e.NewEditIndex;
BindData();
}
Row Cancelling Edit event of Gridview :
protected void gvInstructors_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvInstructors.EditIndex = -1;
BindData();
}
Row Deleting Event of Gridview :
protected void gvInstructors_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblInstructorId = (Label)gvInstructors.Rows[e.RowIndex].FindControl("lblInstructorId");
try
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString());
cn.Open();
string deletestatement = @"delete from tblInstructors where InstructorId = @InstructorId";
SqlCommand cmd = new SqlCommand(deletestatement, cn);
cmd.Parameters.Add("@InstructorId", SqlDbType.Int);
cmd.Parameters["@InstructorId"].Value = lblInstructorId.Text;
cmd.ExecuteNonQuery();
cn.Close();
gvInstructors.EditIndex = -1;
BindData();
}
catch (Exception ex)
{
ex.ToString();
}
}
Row Updating Event of Gridview:
protected void gvInstructors_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblInstructorId = (Label)gvInstructors.Rows[e.RowIndex].FindControl("lblInstructorId");
TextBox txtInstructorName = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtName");
TextBox txtAddress = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtAddress");
TextBox txtPhone = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtPhone");
TextBox txtAge = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtAge");
TextBox txtGender = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtGender");
TextBox txtRegCar = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtRCar");
TextBox txtRegNumber = (TextBox)gvInstructors.Rows[e.RowIndex].FindControl("txtRegNum");
try
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString());
cn.Open();
string updatestatement = @"Update tblInstructors set Name =@Name, Phone=@Phone, Age=@Age, Gender=@Gender, RegisteredCar=@RegisteredCar, RegisteredNumber=@RegisteredNumber where instructorId=@InstructorId";
SqlCommand cmd = new SqlCommand(updatestatement, cn);
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 100);
cmd.Parameters.Add("@Phone", SqlDbType.BigInt);
cmd.Parameters.Add("@Age", SqlDbType.Int);
cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@RegisteredCar", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@RegisteredNumber", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@InstructorId", SqlDbType.Int);
cmd.Parameters["@Name"].Value = txtInstructorName.Text.Trim();
cmd.Parameters["@Phone"].Value = txtPhone.Text.Trim();
cmd.Parameters["@Age"].Value = Convert.ToInt32(txtAge.Text.Trim());
cmd.Parameters["@Gender"].Value = txtGender.Text;
cmd.Parameters["@RegisteredCar"].Value = txtRegCar.Text.Trim();
cmd.Parameters["@RegisteredNumber"].Value = txtRegNumber.Text.Trim();
cmd.Parameters["@InstructorId"].Value = lblInstructorId.Text;
cmd.ExecuteNonQuery();
cn.Close();
gvInstructors.EditIndex = -1;
BindData();
}
catch (Exception ex)
{
ex.ToString();
}
}
Sorting in Gridview:
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = System.Web.UI.WebControls.SortDirection.Descending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
protected void gvUsers_SortCommand(object source, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
private void SortGridView(string sortExpression, string direction)
{
UsersBL objUser = new UsersBL();
DataView dv = (DataView)Session["UsersData"];
dv.Sort = sortExpression + direction;
gvUsers.DataSource = dv;
gvUsers.DataBind();
}