Monday, August 17, 2009

Gridview row_editing, row_deleting, row_updating and row_command events






public partial class doctors : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["hmsCon"]);

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvDataBind();
}
}

public void gvDataBind()
{
con.Open();
string selQuery = "select * from doctorsDetails order by DNO";
SqlDataAdapter da = new SqlDataAdapter(selQuery, con);
DataSet ds = new DataSet();
da.Fill(ds);
ViewState["ds"] = ds;
con.Close();
gvDoctorDetails.DataSource = ds;
gvDoctorDetails.DataBind();
}



This is for gridview RowEditing event

protected void gvDoctorDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvDoctorDetails.EditIndex = e.NewEditIndex;
gvDataBind();
}



This is user Interface


This is for gridview row updating event

protected void gvDoctorDetails_RowUpdating(object sender,GridViewUpdateEventArgs e)
{

int dno = Convert.ToInt32(gvDoctorDetails.DataKeys[e.RowIndex].Values[0].ToString());

TextBox txtDoctor = (TextBox)gvDoctorDetails.Rows[e.RowIndex].FindControl("txtDoctorName");
DropDownList ddlSpecialization = (DropDownList)gvDoctorDetails.Rows[e.RowIndex].FindControl("ddlSpecialization");
TextBox txtPnoneNumber = (TextBox)gvDoctorDetails.Rows[e.RowIndex].FindControl("txtDoctorPhoneNumber");
TextBox txtAddress = (TextBox)gvDoctorDetails.Rows[e.RowIndex].FindControl("txtAddress");
DropDownList ddlAvailability = (DropDownList)gvDoctorDetails.Rows[e.RowIndex].FindControl("ddlAvailability");

string doctorName = txtDoctor.Text;
string specialization = ddlSpecialization.SelectedValue;
string phoneNumber = txtPnoneNumber.Text;
string address = txtAddress.Text;
string availability = ddlAvailability.SelectedValue;

string updQuery = "update DoctorsDetails set doctorName='" + doctorName + "',specialization='" + specialization + "',doctorPhoneNumber='" + phoneNumber + "',address='" + address + "',availabilityDays='" + availability + "' where DNO=" + dno;
con.Open();
SqlCommand cmd = new SqlCommand(updQuery, con);
cmd.ExecuteNonQuery();
con.Close();
gvDoctorDetails.EditIndex = -1;
gvDataBind();
}

This is for gridview Row deleting event

protected void gvDoctorDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int dno = Convert.ToInt32(gvDoctorDetails.DataKeys[e.RowIndex].Values[0].ToString());
con.Open();
string delQuery = "delete from DoctorsDetails where DNO=" + dno;
SqlCommand cmd = new SqlCommand(delQuery, con);
cmd.ExecuteNonQuery();
con.Close();
gvDataBind();
}

This is for gridview row deleting events

protected void gvDoctorDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvDoctorDetails.EditIndex = -1;
gvDataBind();
}

This is for gridview page index changing event

protected void gvDoctorDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvDoctorDetails.PageIndex = e.NewPageIndex;
gvDataBind();
}



This is for gridview Row command event

protected void gvDoctorDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
con.Open();
if (e.CommandName.Equals("AddNewDoctor"))
{
TextBox txtNewDoctorName = (TextBox)gvDoctorDetails.FooterRow.FindControl("txtNewDoctorName");
DropDownList ddlNewSpecialization = (DropDownList)gvDoctorDetails.FooterRow.FindControl("ddlNewSpecialization");
TextBox txtNewDoctorPhoneNumber = (TextBox)gvDoctorDetails.FooterRow.FindControl("txtNewDoctorPhoneNumber");
TextBox txtNewAddress = (TextBox)gvDoctorDetails.FooterRow.FindControl("txtNewAddress");
DropDownList ddlNewAvailability = (DropDownList)gvDoctorDetails.FooterRow.FindControl("ddlNewAvailability");

string insertDoctorDetails = "insert into DoctorsDetails(doctorName,specialization,doctorPhoneNumber,address,availabilityDays) values ('" + txtNewDoctorName.Text.Trim();
insertDoctorDetails += "','" + ddlNewSpecialization.SelectedValue + "','" + txtNewDoctorPhoneNumber.Text.Trim() + "','" + txtNewAddress.Text.Trim() + "','" + ddlNewAvailability.SelectedValue + "')";

SqlCommand cmd = new SqlCommand(insertDoctorDetails, con);
cmd.ExecuteNonQuery();
con.Close();
gvDataBind();

}

}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}

No comments:

Post a Comment