Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Gridview in asp.Net c#.

Dear folks, Welcome to dotnetpgm. This is my first post. This is a dotnet blog. I will share my work experience and ideas in Microsoft .Net Technologies.

Gridview in asp.net c# with sample

Grid view control in asp.net 2.0 is the replacement of Data grid in asp.net 1.1. Grid view is used to bind the data source. The whole database can be viewed in one gridview with colorful and formatted. We can easily view, edit, update and delete the data from this Grid view control. Default Paging is available in this Grid view control. In Asp.net grid view is a very simple and superb control. It reduce our work and perform good.


//Code for binding the data

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString.ToString());
if (!IsPostBack)
{
BindGridview();
}
}

In Page load event the grid view function will call in not is post back,

What is IsPostBack?
The page load event is called when each and every time the page is loaded. If the page is already loaded and waiting for a result means, that time the page could not load again because its not a fresh page. We already give the data in that page.

public void BindGridview()
{
sqlDataAdapter adap=new sqlDataAdapter("Select * from tablename",con);
DataTable dtcommon=new DataTable();
adap.Fill(dtcommon);
Gridview1.DataSource=dtcommon;
Gridview1.DataBind();
}

//Row editing in Grid view

This is for editing a particular row in grid view, if you click the edit it shows textbox box for all the field in a row.

protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e)
{
Gridview1.EditIndex = e.NewEditIndex;
BindGridview();
}

//Row updating in grid view

After clicking the edit button it shows textbox for all the field in a row. In that textbox edit the fields you want.

protected void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int key=Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Value);
TextBox txtuname=(TextBox)Gridview1.Rows[e.RowIndex].Cells[0].Controls[0];
TextBox txtpass = (TextBox)Gridview1.Rows[e.RowIndex].Cells[1].Controls[0];
SqlCommand cmd=new SqlCommand("update tblname set username='"+txtuname.Text+"', password='"+txtpass.Text+"' where userid='"+key.ToString()+"'",con);
con.open();
cmd.ExecuteNonQuery();
con.close();
BindGridview();
}

//Row cancel editing in grid view

If the edit is not need means just click the cancel button, it shows the edit button after clicking

protected void Gridview1_RowCancelingEdit(object sender, GridViewEditEventArgs e)
{
Gridview1.EditIndex = -1;
BindGridview();
}

//Row delete in grid view

If you need to delete a particular row just use this code to delete

protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int key=Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Value);
SqlCommand cmd=new SqlCommand("delete from where userid='"+key.ToString()+"'",con);
con.open();
cmd.ExecuteNonQuery();
con.close();
BindGridview();
}

//Paging in grid view

The Paging in grid view is an easier way to do the paging, first set the allowpaging property to true and give the page size in gridview.

protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Gridview1.PageIndex = e.NewPageIndex;
BindGridview();
}

//Gridview on mouse over the colour will differentiate the row

When mouse over on a row it highlight the current row with gray color and mouseout on a row it shows a normal white color.

protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e)
{
string onmouseoverStyle = "this.style.backgroundColor='gray'";
string onmouseoutStyle = "this.style.backgroundColor='white'";
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowState == DataControlRowState.Alternate)
{
e.Row.Attributes.Add("onmouseover", onmouseoverStyle);
e.Row.Cells[0].ToolTip ="Click Here To see Enquiry Details";
e.Row.Attributes.Add("onmouseout", onmouseoutStyle);
}
}

these are the sample code for gridview.


This post first appeared on Dotnet, please read the originial post: here

Share the post

Gridview in asp.Net c#.

×

Subscribe to Dotnet

Get updates delivered right to your inbox!

Thank you for your subscription

×