Abinash Behura

Abinash Behura

  • NA
  • 21
  • 3.6k

How to adding dynamic rows in gridview when page load in asp

Jan 27 2016 6:22 AM
In my Form have two gridviews are there.In first gridview footer part a Add Button is present when add submit the the data store in temp table & it hold multiple data for a same id and below the gridview another gridview is present it have also same functionality.then next button in the page footer for final save. How to do it...What ever i told is it right or wrong..Pls Suggest me

private void BindserviceperticularsGridview()//grd1
{
DataTable dt = new DataTable();
dt.Columns.Add("rowid", typeof(int));
dt.Columns.Add("NameOfInstitute", typeof(string));
dt.Columns.Add("Block", typeof(string));
dt.Columns.Add("DOB", typeof(string));
dt.Columns.Add("PresentStatus", typeof(string));
dt.Columns.Add("DateOfRelif", typeof(string));
dt.Columns.Add("DurationOfService", typeof(string));
DataRow dr = dt.NewRow();
dr["rowid"] = 1;
dr["NameOfInstitute"] = string.Empty;
dr["Block"] = string.Empty;
dr["DOB"] = string.Empty;
dr["PresentStatus"] = string.Empty;
dr["DateOfRelif"] = string.Empty;
dr["DurationOfService"] = string.Empty;
//dr["diff"] = string.Empty;
dt.Rows.Add(dr);
ViewState["Curtbl"] = dt;
grdservice.DataSource = dt;
grdservice.DataBind();
}


private void AddNewRow()
{
int rowIndex = 0;

if (ViewState["Curtbl"] != null)
{
DataTable dt = (DataTable)ViewState["Curtbl"];
DataRow drCurrentRow = null;
if (dt.Rows.Count > 0)
{
for (int i = 1; i <= dt.Rows.Count; i++)
{
TextBox txtinstitute = (TextBox)grdservice.Rows[rowIndex].Cells[1].FindControl("txtinstitute");
DropDownList drpBlock = (DropDownList)grdservice.Rows[rowIndex].Cells[2].FindControl("drpBlock");
TextBox txtdoj = (TextBox)grdservice.Rows[rowIndex].Cells[3].FindControl("txtdoj");
DropDownList drpstatus = (DropDownList)grdservice.Rows[rowIndex].Cells[4].FindControl("drpstatus");
TextBox txtRelif = (TextBox)grdservice.Rows[rowIndex].Cells[5].FindControl("txtRelif");
TextBox txtDurationService = (TextBox)grdservice.Rows[rowIndex].Cells[6].FindControl("txtDurationService");
drCurrentRow = dt.NewRow();
drCurrentRow["rowid"] = i + 1;
dt.Rows[i - 1]["NameOfInstitute"] = txtinstitute.Text;
dt.Rows[i - 1]["Block"] = drpBlock.SelectedValue;
dt.Rows[i - 1]["DOB"] = txtdoj.Text;
dt.Rows[i - 1]["PresentStatus"] = drpstatus.SelectedValue;
dt.Rows[i - 1]["DateOfRelif"] = txtRelif.Text;
dt.Rows[i - 1]["DurationOfService"] = txtDurationService.Text;
rowIndex++;
}
dt.Rows.Add(drCurrentRow);
ViewState["Curtbl"] = dt;
grdservice.DataSource = dt;
grdservice.DataBind();
}
}
else
{
Response.Write("ViewState Value is Null");
}
SetOldData();
}
private void SetOldData()
{
int rowIndex = 0;
if (ViewState["Curtbl"] != null)
{

DataTable dt = (DataTable)ViewState["Curtbl"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox txtinstitute = (TextBox)grdservice.Rows[rowIndex].Cells[1].FindControl("txtinstitute");
DropDownList drpBlock = (DropDownList)grdservice.Rows[rowIndex].Cells[2].FindControl("drpBlock");
TextBox txtdoj = (TextBox)grdservice.Rows[rowIndex].Cells[3].FindControl("txtdoj");
DropDownList drpstatus = (DropDownList)grdservice.Rows[rowIndex].Cells[4].FindControl("drpstatus");
TextBox txtRelif = (TextBox)grdservice.Rows[rowIndex].Cells[5].FindControl("txtRelif");
TextBox txtDurationService = (TextBox)grdservice.Rows[rowIndex].Cells[6].FindControl("txtDurationService");

txtinstitute.Text = dt.Rows[i]["NameOfInstitute"].ToString();
drpBlock.Text = dt.Rows[i]["Block"].ToString();
txtdoj.Text = dt.Rows[i]["DOB"].ToString();
drpstatus.SelectedValue = dt.Rows[i]["PresentStatus"].ToString();
txtRelif.Text = dt.Rows[i]["DateOfRelif"].ToString();

txtDurationService.Text = dt.Rows[i]["DurationOfService"].ToString();
rowIndex++;
BindGrdLeaveParticulars();

}
}
}
}

If I add 2nd row the previous temp datatable created and its value column comes


Answers (1)