private void AddNewRowToGrid() { int rowIndex = 0; if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; if (Gridview1.Rows.Count > 0) { int intRow = Gridview1.Rows.Count; DropDownList val1 = (DropDownList)Gridview1.Rows[intRow - 1].Cells[1].FindControl("txtItemCode"); TextBox val2 = (TextBox)Gridview1.Rows[intRow - 1].Cells[2].FindControl("txtDescription"); TextBox val3 = (TextBox)Gridview1.Rows[intRow - 1].Cells[3].FindControl("txtUOM"); TextBox val4 = (TextBox)Gridview1.Rows[intRow - 1].Cells[4].FindControl("txtRequestQty"); // TextBox val5 = (TextBox)Gridview1.Rows[intRow - 1].Cells[5].FindControl("txtAmount"); for (int i = 0; i < Gridview1.Rows.Count; i++) { //extract the TextBox values //Label lblSno = (Label)Gridview1.Rows[rowIndex].Cells[0].FindControl("lblSno"); DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtItemCode"); TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtDescription"); TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtUOM"); TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txtRequestQty"); // TextBox box5= (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txtAmount"); //dtCurrentTable.Rows[i][strSno] = lblSno.Text; dtCurrentTable.Rows[i][ItemCode] = box1.Text; dtCurrentTable.Rows[i][strDescription] = box2.Text; dtCurrentTable.Rows[i][UOM] = box3.Text; dtCurrentTable.Rows[i][RequestQty] = box4.Text; // dtCurrentTable.Rows[i][Amount] = (Convert.ToInt32(box3.Text.ToString().Trim()) * Convert.ToInt32(box4.Text.ToString().Trim())); rowIndex++; } drCurrentRow = dtCurrentTable.NewRow(); //add new row to DataTable dtCurrentTable.Rows.Add(drCurrentRow); for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) { // DataRow dr = null; //dr[strSno] = i; dtCurrentTable.Rows[i - 1][strSno] = i; } dtCurrentTable.AcceptChanges(); //Store the current data to ViewState ViewState["CurrentTable"] = dtCurrentTable; //Rebind the Grid with the current data Gridview1.DataSource = dtCurrentTable; Gridview1.DataBind(); } } else { Response.Write("ViewState is null"); } } protected void ButtonAdd_Click(object sender, EventArgs e) { AddNewRowToGrid(); }
AddNewRowToGrid()
DataRow
AddNewRowToGrid
null
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)