Saturday, July 4, 2009

Adding the Rows To Gridview From Footer



Fig1 :


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

/* This is For Displaying Grid With Headers When Page is load */

string[] cols = new string[] { "hid", "SlNo", "Issues", "Remarks" };

DataTable dt_part = new DataTable();

for (int a = 0; a <>

{

DataColumn dc = new DataColumn(cols[a]);

dt_part.Columns.Add(dc);

}

Session["dt_part"] = dt_part;

dt_part = (DataTable)Session["dt_part"];

if (dt_part.Rows.Count == 0)

{

DataRow dr = dt_part.NewRow();

dr[0] = "";

dr[1] = "";

dr[2] = "";

dr[3] = "";

dt_part.Rows.Add(dr);

grdparticipents.DataSource = dt_part;

grdparticipents.DataBind();

}

else

{

grdparticipents.DataSource = dt_part;

grdparticipents.DataBind();

}

}

}

protected void grdparticipents_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandArgument.ToString() == "Add")

{

addrecord();

}

}

public void addrecord()

{

DataTable dtt = (DataTable)Session["dt_part"];

DataRow dr = dtt.NewRow();

dr["SlNo"] = ((TextBox)grdparticipents.FooterRow.FindControl("txtslno")).Text;

dr["Issues"] = ((TextBox)grdparticipents.FooterRow.FindControl("txtissues")).Text;

dr["Remarks"] = ((TextBox)grdparticipents.FooterRow.FindControl("Remarks1")).Text;

dtt.Rows.Add(dr);

if (dtt.Rows.Count >= 2)

{

if (dtt.Rows[0][0].ToString() == "" && dtt.Rows[0][1].ToString() == "" && dtt.Rows[0][2].ToString() == "" && dtt.Rows[0][3].ToString() == "")

{

dtt.Rows[0].Delete();

}

}

grdparticipents.DataSource = dtt;

grdparticipents.DataBind();

}

protected void grdparticipents_RowEditing(object sender, GridViewEditEventArgs e)

{

DataTable dtt = (DataTable)Session["dt_part"];

dtt.Rows[e.NewEditIndex].Delete();

if (dtt.Rows.Count == 0)

{

DataRow dr = dtt.NewRow();

dr[0] = "";

dr[1] = "";

dr[2] = "";

dr[3] = "";

dtt.Rows.Add(dr);

}

grdparticipents.DataSource = dtt;

grdparticipents.DataBind();

}

}

1 comment :