private void AddDefaultFirstRecord() { //creating dataTable DataTable dt = new DataTable(); DataRow dr; dt.TableName = "ProjectReferral"; dt.Columns.Add(new DataColumn("ADPNO", typeof(int))); dt.Columns.Add(new DataColumn("ProjectCode", typeof(int))); dt.Columns.Add(new DataColumn("ProjectName", typeof(string))); dt.Columns.Add(new DataColumn("AssignTo", typeof(string))); dt.Columns.Add(new DataColumn("ReferralName", typeof(string))); dt.Columns.Add(new DataColumn("ReferralDesignation", typeof(string))); dt.Columns.Add(new DataColumn("ReferralContact", typeof(int))); dt.Columns.Add(new DataColumn("ApparentIssue", typeof(string))); dt.Columns.Add(new DataColumn("Attachment", typeof(string))); dr = dt.NewRow(); dt.Rows.Add(dr); //saving databale into viewstate ViewState["ProjectReferral"] = dt; GridViewReferral.AutoGenerateColumns = true; //bind Gridview GridViewReferral.DataSource = dt; GridViewReferral.DataBind(); }
private void AddNewRecordRowToGrid() { // check view state is not null if (ViewState["ProjectReferral"] != null) { //get datatable from view state DataTable dtCurrentTable = (DataTable)ViewState["ProjectReferral"]; DataRow drCurrentRow = null; if (dtCurrentTable.Rows.Count > 0) { for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) { //add each row into data table drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow["ADPNO"] = Convert.ToInt32(TextBoxadpno.Text); drCurrentRow["ProjectCode"] = Convert.ToInt32(TextBoxProjCode.Text); drCurrentRow["ProjectName"] = TextBoxProjName.Text; drCurrentRow["AssignTo"] = TextBoxAssignTo.Text; drCurrentRow["ReferralName"] = TextBoxName.Text; drCurrentRow["ReferralDesignation"] = TextBoxDesgination.Text; drCurrentRow["ReferralContact"] = Convert.ToInt32(TextBoxContactNo.Text); drCurrentRow["ApparentIssue"] = TextBoxApprentIssue.Text; drCurrentRow["Attachment"] = PicPath; } //Remove initial blank row if (dtCurrentTable.Rows[0][0].ToString() == "") { dtCurrentTable.Rows[0].Delete(); dtCurrentTable.AcceptChanges(); } //add created Rows into dataTable dtCurrentTable.Rows.Add(drCurrentRow); //Save Data table into view state after creating each row ViewState["ProjectReferral"] = dtCurrentTable; //Bind Gridview with latest Row GridViewReferral.DataSource = dtCurrentTable; GridViewReferral.DataBind(); Labelrecordno.Text = dtCurrentTable.Rows.Count.ToString(); } } }