C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Gridview in Asp.net


Posted Date: 30 Jun 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: ElzMember Level: Gold    
Rating: 1 out of 5Points: 5



Abstract:


This Code shows how to bind,update and delete columns in gridview.

JavaScript code :

script language="javascript" type="text/javascript"
function click()
{
return confirm("Are u sure u want to delete this record");
}


Code for .aspx page:

asp:GridView ID="GridView1" runat="server"
onpageindexchanging="GridView1_PageIndexChanging"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" AutoGenerateColumns="False"
DataKeyNames="ID"

PagerSettings FirstPageText="Next" LastPageText="Previous"
NextPageText="Next" PreviousPageText="Previous" Mode="NumericFirstLast"

Columns
asp:TemplateField
ItemTemplate
asp:CheckBox ID="chk" runat="server"

ItemTemplate
asp:TemplateField
asp:CommandField ShowEditButton="True"
asp:BoundField DataField="NAME" HeaderText="Name"
asp:BoundField DataField="NUMBER" HeaderText="Number"
asp:BoundField DataField="Total" HeaderText="Tasks"
Columns

asp:GridView

asp:Button ID="delete" runat="server" Text="Delete" onclick="delete_Click" OnClientClick="javascript:returnclick();"


Code for code-behind page:

using System.Data.Sql;
using System.Data.SqlClient;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}

public void bind()
{
SqlConnection con=new SqlConnection(@"server=xx;dbname=xx;user id=xx;password=xx");
SqlCommand cmd=new SqlCommand("Select * from tablename",con);
SqlDataAdpter ada=new SqlDataAdpter(cmd);
DataSet ds=new DataSet();
con.Open();
ada.Fill(ds);
con.Close();
gridView1.DataSource=ds;
gridView1.DataBind();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox Name;
TextBox Number;
TextBox total;

int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
Name =(TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
Number = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];

str = "update tbname set NAME='" + Name.Text + "',NUMBER="+Number.Text+" where ID="+id+"";
inserttotable(str);
GridView1.EditIndex = -1;
bind();
Response.Write("Data get updated successfully");
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}

protected void delete_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox checkbox = (CheckBox)row.FindControl("chk");
if (checkbox.Checked)
{
int no = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
str = "delete from tbname where ID="+no+"";
}
}
bind();
}

public void inserttotable(string str)
{
con.open();
cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.close();
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Paging  .  Gridview bind  .  Edit  .  Delete functionality  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sorting
Previous Resource: Gridview
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use