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();
} }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|