protected void DeleteRow(object sender, GridViewDeleteEventArgs e) { SetRowData(); if (ViewState["CurrentTable"] != null) { DataTable dt = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; int rowIndex = Convert.ToInt32(e.RowIndex); if (dt.Rows.Count > 1) { dt.Rows.Remove(dt.Rows[rowIndex]); drCurrentRow = dt.NewRow(); ViewState["CurrentTable"] = dt; NewTest_Questions_GridView.DataSource = dt; NewTest_Questions_GridView.DataBind(); for (int i = 0; i < NewTest_Questions_GridView.Rows.Count; ++i) { NewTest_Questions_GridView.Rows[i].Cells[0].Text = (i + 1).ToString(); } SetPreviousData(); } } }
private void GenerateEmptyGridView() { DataTable dt = new DataTable(); DataRow dr = null; dt.Columns.Add(new DataColumn("RowNumber", typeof(string))); dt.Columns.Add(new DataColumn("Col1", typeof(string))); dt.Columns.Add(new DataColumn("Col2", typeof(string))); dr = dt.NewRow(); dr["RowNumber"] = 1; dr["Col1"] = string.Empty; dr["Col2"] = string.Empty; dt.Rows.Add(dr); ViewState["CurrentTable"] = dt; NewTest_Questions_GridView.DataSource = dt; NewTest_Questions_GridView.DataBind(); }
protected void NewTest_AddQuestion_Click(object sender, EventArgs e) { int rowIndex = 0; if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; if (dtCurrentTable.Rows.Count > 0) { for (int i = 1; i <= dtCurrentTable.Rows.Count; ++i) { TextBox Question = (TextBox)NewTest_Questions_GridView.Rows[rowIndex].Cells[1].FindControl("question_"); TextBox Choices = (TextBox)NewTest_Questions_GridView.Rows[rowIndex].Cells[2].FindControl("choices_"); drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow["RowNumber"] = i + 1; dtCurrentTable.Rows[i - 1]["Col1"] = Question.Text; dtCurrentTable.Rows[i - 1]["Col2"] = Choices.Text; ++rowIndex; } dtCurrentTable.Rows.Add(drCurrentRow); ViewState["CurrentTable"] = dtCurrentTable; NewTest_Questions_GridView.DataSource = dtCurrentTable; NewTest_Questions_GridView.DataBind(); for (int i = 0; i < NewTest_Questions_GridView.Rows.Count; ++i) { NewTest_Questions_GridView.Rows[i].Cells[0].Text = (i + 1).ToString(); } } } SetPreviousData(); }