Monday, March 21, 2011

Grid view with coding

First you need to make the database as we discuss in earlier lessons

1) Design of Form view to see HTML code click here.....















2) Properties for Grid view















3) Events for Grid view













4) Code for .aspx


*********************** .aspx code ********************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class grdvw_wc : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
con.Open();
if (!Page.IsPostBack)
sunny();
}
public void sunny()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tblbook", con);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) //Event of grid view
{
GridView1.PageIndex = e.NewPageIndex;
sunny();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) //Event of grid view
{
GridView1.EditIndex = -1;
sunny();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) //Event of grid view
{
GridView1.EditIndex = e.NewEditIndex;
sunny();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) //Event of grid view
{
Int32 prc,bid;
string tit, aut, pub,img;
img = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtimg"))).Text;
tit = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txttit"))).Text;
aut = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtaut"))).Text;
pub = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtpub"))).Text;
prc = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtprc"))).Text);
bid = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("lblbid"))).Text);
SqlCommand cmd = new SqlCommand("update tblbook set booktit=@tit, bookaut=@aut,bookimg=@img,bookpub=@pub,bookprc=@prc where bookid=@bid", con);
cmd.Parameters.Add("tit", SqlDbType.VarChar).Value = tit;
cmd.Parameters.Add("aut", SqlDbType.VarChar).Value = aut;
cmd.Parameters.Add("img", SqlDbType.VarChar).Value = img;
cmd.Parameters.Add("pub", SqlDbType.VarChar).Value = pub;
cmd.Parameters.Add("prc", SqlDbType.Int).Value = prc;
cmd.Parameters.Add("bid", SqlDbType.Int).Value = bid;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
GridView1.EditIndex = -1;
sunny();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) //Event of grid view
{
GridView1.EditIndex = -1;
sunny();
}
}


***************************** Web config file ***************************************

5) Running interface



No comments:

Post a Comment