GridView Control

GRIDVIEW CONTROL 

 

Displays the values of a data source in a table where each column represents a field and each row represents a record. The GridView control enables you to select, sort, and edit these items.
Namespace:  System.Web.UI.WebControls


Globally Gridview Binding

public void bindgrid(GridView gv, string query)
{
ds.Clear();
cmd.CommandText = query;
adp.Fill(ds, "vt1");
gv.DataSource = ds.Tables["vt1"];
gv.DataBind();
}
}


// How to call this global function in managecourse.aspx.cs file //

public partial class managecourse : System.Web.UI.Page
{
connection c = new connection();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
c.bindgrid(GridView1, "select * from student");
}
}
}

Gridview edit,delete,cancel.paging

// coding in gridview.aspx source file //
//CODING IN GRIDVIEW
public partial class admin_manageAttendance : System.Web.UI.Page
{
connection c = new connection();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
c.cmd.commandText="Select * from student";
c.adp.Fill(c.ds,"vt");
GridView1.DataSource=c.ds.Tables["vt"];
GridView1.DataBind();
}

protected void delete(object sender, GridViewDeleteEventArgs e)
{
c.con.Open();
c.cmd.CommandText = "delete from student where sid='"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";
c.cmd.ExecuteNonQuery();
c.con.Close();
GridView1.EditIndex = -1;
bind();
}

protected void cancel(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void edit(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
} protected void update(object sender, GridViewUpdateEventArgs e)
{
Label l=new Label();
TextBox t1 = new TextBox();
TextBox t2 = new TextBox();
GridViewRow gr= GridView1.Rows[e.RowIndex];
t1 = (TextBox)gr.Cells[1].Controls[0];
t2 = (TextBox)gr.Cells[2].Control[0];
c.con.Open();
c.cmd.CommandText = "update student set sname='"+ t1.Text+ "', address='"+ t2.Text+"' where sid='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
c.cmd.ExecuteNonQuery();
c.con.Close();
GridView1.EditIndex = -1;
bind();
} protected void paging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}

1 comment: