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 add update


Posted Date: 22 Oct 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: Prasanth Kumar S.DMember Level: Gold    
Rating: 1 out of 5Points: 10



This code shows features of gridview and update method on it.


//place this in aspx page
private void ShowGrid()//display grid
{
DataTable dt = new DataTable();
DataColumn[] dcol = new DataColumn[] { new DataColumn("SlNo", typeof(int)), new DataColumn("Description", typeof(string)) };
dt.Columns.AddRange(dcol);
Session["Collateral"] = dt;
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
GridOverDue.DataSource = dt;
GridOverDue.DataBind();
GridOverDue.Rows[0].Visible = false;
dt.Rows[0].Delete();
}
//bind
private void BindData()
{
GridOverDue.DataSource = Session["Collateral"];
GridOverDue.DataBind();
}




Inserting row to a gridview

protected void GridOverDue_RowCommand(object sender, GridViewCommandEventArgs e)
{

if (e.CommandName == "ADD")
{
DataTable dt = new DataTable();
DataRow dr;


TextBox txt = new TextBox();
txt = (TextBox)GridOverDue.FooterRow.Cells[0].FindControl("Description");

dt = (DataTable)Session["Collateral"];
dr = dt.NewRow();
dr["SlNo"] = dt.Rows.Count+1;

if (txt.Text == "")
{

GeneralFunction.CreateMessageAlert(this, " Enter Descriptions Please ", "alertkey");
return;
}
dr["Description"] = txt.Text;
dt.Rows.Add(dr);
BindData();
Session["Collateral"] = dt;
}


How to update a row in gridview

protected void GridOverDue_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["Collateral"];
DataRow dr = dt.Rows[e.RowIndex];

TextBox txt = (TextBox)GridOverDue.Rows[GridOverDue.EditIndex].Cells[0].FindControl("txtEditDescription");
dr["Description"] = txt.Text;
dt.AcceptChanges();
Session["Collateral"] = dt;
GridOverDue.EditIndex = -1;
GridOverDue.ShowFooter = true;
BindData();

}
protected void GridOverDue_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridOverDue.EditIndex = -1;
GridOverDue.DataSource = (DataTable)Session["Collateral"];
GridOverDue.ShowFooter = true;
BindData();

}
protected void GridOverDue_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["Collateral"];
dt.Rows[e.RowIndex].Delete();
dt.AcceptChanges();
if (dt.Rows.Count > 0)
{
Session["Collateral"] = dt;
BindData();
}
else
{
ShowGrid();
}

}
protected void GridOverDue_RowEditing(object sender, GridViewEditEventArgs e)
{
GridOverDue.EditIndex = e.NewEditIndex;
GridOverDue.ShowFooter = false;
BindData();


}


}




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.
Gridview add update  .  

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: Custom validators : Make easy and Simple Client Validation
Previous Resource: Pass Multiple Query String using HyperLinkField in asp.net
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