|
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial
class _Default : System.Web.UI.Page
{
protected void
Page_Load(object sender,
EventArgs e)
{
if (!IsPostBack)
{
FillGridView();
}
}
///
<summary>
/// Fill record in
gridview
///
</summary>
public void
FillGridView()
{
try
{
string cnString =
ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
SqlConnection con =
new SqlConnection(cnString);
GlobalClass.adap =
new SqlDataAdapter("select * from gridvew", con);
SqlCommandBuilder bui =
new
SqlCommandBuilder(GlobalClass.adap);
GlobalClass.dt =
new DataTable();
GlobalClass.adap.Fill(GlobalClass.dt);
GridView1.DataSource = GlobalClass.dt;
GridView1.DataBind();
}
catch
{
}
}
protected void
editRecord(object sender, GridViewEditEventArgs
e)
{
GridView1.EditIndex = e.NewEditIndex;
FillGridView();
}
protected void
cancelRecord(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
FillGridView();
}
///
<summary>
/// New Row Add
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected void
AddNewRecord(object sender,
EventArgs e)
{
try
{
if (GlobalClass.dt.Rows.Count
> 0)
{
GridView1.EditIndex = -1;
GridView1.ShowFooter = true;
FillGridView();
}
else
{
GridView1.ShowFooter = true;
DataRow dr =
GlobalClass.dt.NewRow();
dr[1] = "0";
dr[2] = 0;
dr[3] = 0;
dr[4] = "0";
dr[5] = "0";
GlobalClass.dt.Rows.Add(dr);
GridView1.DataSource = GlobalClass.dt;
GridView1.DataBind();
GridView1.Rows[0].Visible = false;
}
}
catch
{
}
}
///
<summary>
/// New Record Cancel
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected void
AddNewCancel(object sender,
EventArgs e)
{
GridView1.ShowFooter = false;
FillGridView();
}
///
<summary>
/// Insert New Record
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected void
InsertNewRecord(object sender,
EventArgs e)
{
try
{
string strName =
GlobalClass.dt.Rows[0]["name"].ToString();
if (strName ==
"0")
{
GlobalClass.dt.Rows[0].Delete();
GlobalClass.adap.Update(GlobalClass.dt);
}
TextBox txtName = GridView1.FooterRow.FindControl("txtNewName") as
TextBox;
TextBox txtAge = GridView1.FooterRow.FindControl("txtNewAge") as
TextBox;
TextBox txtSalary = GridView1.FooterRow.FindControl("txtNewSalary") as
TextBox;
TextBox txtCountry = GridView1.FooterRow.FindControl("txtNewCountry") as
TextBox;
TextBox txtCity = GridView1.FooterRow.FindControl("txtNewCity") as
TextBox;
DataRow dr =
GlobalClass.dt.NewRow();
dr["name"] = txtName.Text.Trim();
dr["age"] = txtAge.Text.Trim();
dr["salary"] = txtSalary.Text.Trim();
dr["country"] =
txtCountry.Text.Trim();
dr["city"] = txtCity.Text.Trim();
GlobalClass.dt.Rows.Add(dr);
GlobalClass.adap.Update(GlobalClass.dt);
GridView1.ShowFooter = false;
FillGridView();
}
catch
{
}
}
///
<summary>
/// Record Updation
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected void
updateRecord(object sender,
GridViewUpdateEventArgs e)
{
try
{
TextBox txtName = GridView1.Rows[e.RowIndex].FindControl("txtName") as
TextBox;
TextBox txtAge = GridView1.Rows[e.RowIndex].FindControl("txtAge") as
TextBox;
TextBox txtSalary = GridView1.Rows[e.RowIndex].FindControl("txtSalary") as
TextBox;
TextBox txtCountry = GridView1.Rows[e.RowIndex].FindControl("txtCountry") as
TextBox;
TextBox txtCity = GridView1.Rows[e.RowIndex].FindControl("txtCity") as
TextBox;
GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["name"] = txtName.Text.Trim();
GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["age"] = Convert.ToInt32(txtAge.Text.Trim());
GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["salary"] = Convert.ToInt32(txtSalary.Text.Trim());
GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["country"] = txtCountry.Text.Trim();
GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex]["city"] = txtCity.Text.Trim();
GlobalClass.adap.Update(GlobalClass.dt);
GridView1.EditIndex = -1;
FillGridView();
}
catch
{
}
}
///
<summary>
/// Record Deletion
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected void
RowDeleting(object sender,
GridViewDeleteEventArgs e)
{
try
{
GlobalClass.dt.Rows[GridView1.Rows[e.RowIndex].RowIndex].Delete();
GlobalClass.adap.Update(GlobalClass.dt);
FillGridView();
}
catch
{
}
}
}
|