Make Project in GridView and use of Footer in Asp.net
This Article explain the how to use GridView Control and footer in Asp.net program
#Make new project in asp.net and add webform then add GridView
#Connection String
<connectionStrings>
<add name="psg" connectionString="server=DESKTOP-.\SQLEXPRESS;
database=database name; integrated security=true"/>
</connectionStrings>
|
#Databinding
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Grid_Bind();
}
}
private void Grid_Bind()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from employee", ConfigurationManager.ConnectionStrings["ps"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
|
*Autogenerated column=False
*Show footer = True
*Columns – Empno, Name, Address, Salary, Edit,
Delete
#Edit Template (Empno)
*Item template
Label > Data source binding > Eval(“empno”)
*EditItem Template
Label > Data source binding > Eval(“empno”)
*Footer template
Label
# Column (Name)
*Item template
Label > Data source binding > Eval(“ename”)
*EditItem Template
Text Box > Data source binding > Eval(“ename”)
*Footer template
Text Box
# Column (Address)
*Item template
Label > Data source binding > Eval(“eadd”)
*EditItem Template
Text Box > Data source binding > Eval(“eadd”)
*Footer template
Text Box
# Column (Salary)
*Item template
Label > Data source binding > Eval(“esal”)
*EditItem Template
Text Box > Data source binding > Eval(“esal”)
*Footer template
Text Box
# Column (Edit)
*Item template
Link Button > Properties>Text > Edit >
Command name>edit
*EditItem Template
Link Button > Properties>Text > Update >
Command name>update
Link Button > Properties>Text > Cancel >
Command name>cancel
*Footer template
Link Button > Properties>Text > Save>
Command name>save
# Column (Delete)
*Item template
Link Button > Properties>Text > Delete >
Command name>delete
#Coding for autoincrement of empno
public Int32 GetAuto()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ps"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select
isnull(max(empno),0) from employee";
cmd.Connection = con;
Int32 k=Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
return k + 1;
}
|
#Html Source
Empno>footer template
<FooterTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#GetAuto()%>'></asp:Label>
</FooterTemplate>
|
#Custom Command for save
protected void GridView1_RowCommand(object
sender, GridViewCommandEventArgs e)
{
if (e.CommandName=="save")
{
int empno, esal;
string ename, eadd;
empno =
Convert.ToInt32(((Label)(GridView1.FooterRow.FindControl("Label2"))).Text);
ename =
((TextBox)GridView1.FooterRow.FindControl("TextBox2")).Text;
eadd =
((TextBox)GridView1.FooterRow.FindControl("TextBox4")).Text;
esal =
Convert.ToInt32(((TextBox)GridView1.FooterRow.FindControl("TextBox6")).Text);
SqlConnection con = new SqlConnection();
con.ConnectionString =
ConfigurationManager.ConnectionStrings["ps"].ConnectionString;
// con.ConnectionString =
ConfigurationManager.ConnectionStrings["psg"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("insemp",con);
cmd.CommandType =
CommandType.StoredProcedure;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = empno;
cmd.Parameters.Add("@en", SqlDbType.VarChar,50).Value = ename;
cmd.Parameters.Add("@ed", SqlDbType.VarChar, 50).Value = eadd;
cmd.Parameters.Add("@es", SqlDbType.Int).Value = esal;
cmd.ExecuteNonQuery();
cmd.Dispose();
Grid_Bind();
}
|
#Editing coding
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.ShowFooter = false;
Grid_Bind();
}
|
#Cancel Coding
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.ShowFooter = false;
Grid_Bind();
}
|
# Update Coding
protected void GridView1_RowUpdating(object
sender, GridViewUpdateEventArgs e)
{
int empno, esal;
string ename, eadd;
empno =
Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("Label6"))).Text);
ename = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
eadd = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;
esal =
Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5")).Text);
SqlConnection con = new SqlConnection();
//
con.ConnectionString =
ConfigurationManager.ConnectionStrings["psg"].ConnectionString;
con.ConnectionString= ConfigurationManager.ConnectionStrings["ps"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("updemp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = empno;
cmd.Parameters.Add("@en", SqlDbType.VarChar, 50).Value = ename;
cmd.Parameters.Add("@ed", SqlDbType.VarChar, 50).Value = eadd;
cmd.Parameters.Add("@es", SqlDbType.Int).Value = esal;
cmd.ExecuteNonQuery();
cmd.Dispose();
GridView1.EditIndex = -1;
GridView1.ShowFooter = true;
Grid_Bind();
}
|
#Delete Coding
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
int empno;
empno =
Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("Label1"))).Text);
SqlConnection con = new SqlConnection();
// con.ConnectionString =
ConfigurationManager.ConnectionStrings["psg"].ConnectionString;
con.ConnectionString = ConfigurationManager.ConnectionStrings["ps"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("delemp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = empno;
cmd.ExecuteNonQuery();
cmd.Dispose();
GridView1.EditIndex = -1;
GridView1.ShowFooter = true;
Grid_Bind();
}
|
No comments:
Post a Comment