I am able to add a row dynamically in a GridView through the following code but I need to do the same to be captured and displayed in Application every time when I open the application with the records I have added previously in the GridView.
Any Ideas??
Coding:
![enter image description here][1] protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
dr = dt.NewRow();
//Store the DataTable in ViewState
// ViewState["CurrentTable"] = dt;
Application["CurrentTable"]=dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
//if (ViewState["CurrentTable"] != null)
if (Application["CurrentTable"] != null)
{
//DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataTable dtCurrentTable1 = (DataTable)Application["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable1.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable1.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
drCurrentRow = dtCurrentTable1.NewRow();
drCurrentRow["RowNumber"] = i + 1;
/*dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;*/
dtCurrentTable1.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable1.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable1.Rows[i - 1]["Column3"] = box3.Text;
rowIndex++;
}
dtCurrentTable1.Rows.Add(drCurrentRow);
//ViewState["CurrentTable"] = dtCurrentTable;
Application["CurrentTable"] = dtCurrentTable1;
Gridview1.DataSource = dtCurrentTable1;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
//if (ViewState["CurrentTable"] != null)
if (Application["CurrentTable"] != null)
{
//DataTable dt = (DataTable)ViewState["CurrentTable"];
DataTable dt1 = (DataTable)Application["CurrentTable"];
if (dt1.Rows.Count > 0)
{
for (int i = 0; i < dt1.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
/*box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();*/
box1.Text = dt1.Rows[i]["Column1"].ToString();
box2.Text = dt1.Rows[i]["Column2"].ToString();
box3.Text = dt1.Rows[i]["Column3"].ToString();
rowIndex++;
}
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
}
Since in your question you are talking about when the application is opened, what you will need to do is have a method to save the data in the grid view somewhere, i.e. Database, Text File etc.
This is because the Application variable is lost when the application is closed, so to re-open at the last point you will need to read the data you saved either in a Database or a Text file and re-create the DataTable object from that.
Related
I want to create a dynamic gridview that contains rownumber, two textboxes, and a linkbutton to delete a row. At page load, the grid is created by 6 rows.
I want to hide the link button for delete for the first 4 rows only. and set the previous data when the user deletes one of the last two rows.
These are the functions I'm using for setting previous data and deleting the row.
private void SetInitialRows() {
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));//for TextBox value
dt.Columns.Add(new DataColumn("Column2", typeof(string)));//for TextBox value
dr = dt.NewRow();
for(int i = 1; i < 7; i++)
{
dr["RowNumber"] = i;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dt.Rows.Add(dr);
}
//Store the DataTable in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Bind the Gridview
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private void SetPreviousData() {
int rowIndex = 0;
if (ViewState["CurrentTable"] != null) {
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0) {
for (int i = 0; i < dt.Rows.Count; i++) {
TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2");
if (i < dt.Rows.Count - 1) {
//Assign the value from DataTable to the TextBox
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
}
rowIndex++;
}
}
}
}
protected void LinkDelete_Click(object sender, EventArgs e) {
LinkButton lb = (LinkButton)sender;
GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
int rowID = gvRow.RowIndex;
if (ViewState["CurrentTable"] != null) {
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 1) {
if (gvRow.RowIndex < dt.Rows.Count - 1) {
//Remove the Selected Row data
dt.Rows.Remove(dt.Rows[rowID]);
}
}
//Store the current data in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Re bind the GridView for the updated data
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
//Set Previous Data on Postbacks
SetPreviousData();
}
One way would be to copy the changed data to another DataTable. If you need to save changes or do validation, this makes it simple to do without affecting the original data.
I have created an editable gridview and I wan to add rows as many rows as I want at runtime. It is working fine but I'm only able to add 9 rows after that I get a Index out of bound exception, where as I'm not setting the limit anywhere in my code.
private void SetInitialRow()
{
DataTable dtable = new DataTable();
DataRow dr = null;
dtable.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dtable.Columns.Add(new DataColumn("Column1", typeof(string)));
dtable.Columns.Add(new DataColumn("Column2", typeof(string)));
dtable.Columns.Add(new DataColumn("Column3", typeof(string)));
dtable.Columns.Add(new DataColumn("Column4", typeof(string)));
dtable.Columns.Add(new DataColumn("Column5", typeof(string)));
dtable.Columns.Add(new DataColumn("Column6", typeof(string)));
dtable.Columns.Add(new DataColumn("Column7", typeof(string)));
dtable.Columns.Add(new DataColumn("Column8", typeof(string)));
dr = dtable.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dr["Column5"] = string.Empty;
dr["Column6"] = string.Empty;
dr["Column7"] = string.Empty;
dr["Column8"] = string.Empty;
dtable.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dtable;
GridView1.DataSource = dtable;
GridView1.DataBind();
}
private void AddNewRowToGrid()
{
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++)
{
//extract the TextBox values
TextBox Gtb_ffcode = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("Gtb_ffcode");
TextBox Gtb_terr = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("Gtb_terr");
DropDownList GddlTeam = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("GddlTeam");
TextBox Gtb_fromdt = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("Gtb_fromdt");
TextBox Gtb_todt = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("Gtb_todt");
DropDownList Gddl_desg = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("Gddl_desg");
DropDownList Gddl_role = (DropDownList)GridView1.Rows[rowIndex].Cells[7].FindControl("Gddl_role");
TextBox Gtb_manager = (TextBox)GridView1.Rows[rowIndex].Cells[8].FindControl("Gtb_manager");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = Gtb_ffcode.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = Gtb_terr.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = GddlTeam.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = Gtb_fromdt.Text;
dtCurrentTable.Rows[i - 1]["Column5"] = Gtb_todt.Text;
dtCurrentTable.Rows[i - 1]["Column6"] = Gddl_desg.Text;
dtCurrentTable.Rows[i - 1]["Column7"] = Gddl_role.Text;
dtCurrentTable.Rows[i - 1]["Column8"] = Gtb_manager.Text;
drCurrentRow["Column1"] = Gtb_ffcode.Text = maxcode; // write ff code to the new generating row
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox Gtb_ffcode = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("Gtb_ffcode");
TextBox Gtb_terr = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("Gtb_terr");
DropDownList GddlTeam = (DropDownList)GridView1.Rows[rowIndex].Cells[3].FindControl("GddlTeam");
TextBox Gtb_fromdt = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("Gtb_fromdt");
TextBox Gtb_todt = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("Gtb_todt");
DropDownList Gddl_desg = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("Gddl_desg");
DropDownList Gddl_role = (DropDownList)GridView1.Rows[rowIndex].Cells[7].FindControl("Gddl_role");
TextBox Gtb_manager = (TextBox)GridView1.Rows[rowIndex].Cells[8].FindControl("Gtb_manager");
Gtb_ffcode.Text = dt.Rows[i]["Column1"].ToString();
Gtb_terr.Text = dt.Rows[i]["Column2"].ToString();
GddlTeam.Text = dt.Rows[i]["Column3"].ToString();
Gtb_fromdt.Text = dt.Rows[i]["Column4"].ToString();
Gtb_todt.Text = dt.Rows[i]["Column5"].ToString();
Gddl_desg.Text = dt.Rows[i]["Column6"].ToString();
Gddl_role.Text = dt.Rows[i]["Column7"].ToString();
Gtb_manager.Text = dt.Rows[i]["Column8"].ToString();
rowIndex++;
}
}
}
}
I am having a Gridview with 1 checkbox column and 3 other columns. To 1 colums I am adding values from a TextBox on ButtonClick.
I want that duplicate values should not displayed in the gridview for which I have added code in Rowdatabound.
The code works perfectly when paging is disabled but when paging is enabled code works only if a duplicate value is added to the same page but if the value being added is to the next page the value is accepted.
protected void GVRequest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//switch for first row
if (e.Row.RowIndex == 1)
{
GridViewRow Gprev = GVRequest.Rows[e.Row.RowIndex - 1];
if (Gprev.Cells[1].Text.Equals(e.Row.Cells[1].Text))
{
e.Row.Cells[1].Text = "";
e.Row.Visible = false;
}
}
//now continue with the rest of the rows
if (e.Row.RowIndex > 1)
{
//set reference to the row index and the current value
int intC = e.Row.RowIndex;
string lookfor = e.Row.Cells[1].Text;
//now loop back through checking previous entries for matches
do
{
GridViewRow Gprev = GVRequest.Rows[intC - 1];
if (Gprev.Cells[1].Text.Equals(e.Row.Cells[1].Text))
{
//e.Row.Cells[0].Text = "";
//e.Row.Cells[1].Text = "";
//e.Row.Cells[2].Text = "";
//e.Row.Cells[3].Text = "";
e.Row.Visible = false;
//Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('ID already added for processing');", true);
}
intC = intC - 1;
} while (intC > 0);
}
}
}
protected void GVRequest_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
//DataRow drCurrentRow = null;
GVRequest.PageIndex = e.NewPageIndex;
GVRequest.DataSource = dtCurrentTable;
//SetPreviousData();
//GVRequest.DataBind();
DataBind();
}
//Add new Row
private void AddNewRowToGrid()
{
if (ViewState["CurrentTable"] != null)
{
try
{
string RequestID = Request.QueryString["RequestID"];
SqlConnection conn = new SqlConnection(GetConnectionString());
StringBuilder sb = new StringBuilder(string.Empty);
const string SQLFileRetrievalReq = "SELECT DeptFileID, FileStatus, FileID, RequestID FROM FileInwardMaster WHERE (FileID = #FileID) AND (RequestID = #RequestID)";
SqlCommand cmdFileRetrievalReq = new SqlCommand(SQLFileRetrievalReq, conn);
cmdFileRetrievalReq.Parameters.Add("#RequestID", SqlDbType.VarChar);
cmdFileRetrievalReq.Parameters["#RequestID"].Value = RequestID;
cmdFileRetrievalReq.Parameters.Add("#FileID", SqlDbType.VarChar);
cmdFileRetrievalReq.Parameters["#FileID"].Value = TextBoxFileIds.Text.ToString();
conn.Open();
SqlDataReader reader = cmdFileRetrievalReq.ExecuteReader();
reader.Read();
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
drCurrentRow = dtCurrentTable.NewRow();
//drCurrentRow["SlNo"] = dtCurrentTable.Rows.Count + 1;
drCurrentRow["FileID"] = TextBoxFileIds.Text.ToUpper();
drCurrentRow["RequestID"] = RequestID;
drCurrentRow["DeptFileID"] = reader["DeptFileID"].ToString();
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
GVRequest.DataSource = dtCurrentTable;
GVRequest.DataBind();
}
catch
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('ID Entered/Scanned not valid for Request. Please Enter/Scan a valid ID to proceed');", true);
SetPreviousData();
//SetInitialRow();
//GVRequest.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
//Get Previous Data
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["FileID"].ToString();// = txtRcptHdNo.Text;
dt.Rows[i]["RequestID"].ToString();// = ddlRcptDtlsItem.SelectedItem.Text;
dt.Rows[i]["DeptFileID"].ToString();// = ddlRcptDtlsItem.SelectedItem.Text;
rowIndex++;
}
}
}
}
private void SetInitialRow()
{
string RequestID = Request.QueryString["RequestID"];
DataTable dt = new DataTable();
//DataRow dr = null;
dt.Columns.Add(new DataColumn("RequestID", typeof(string)));
dt.Columns.Add(new DataColumn("FileID", typeof(string)));
dt.Columns.Add(new DataColumn("DeptFileID", typeof(string)));
// dr = dt.NewRow();
// dr["RequestID"] = string.Empty;
//dr["FileID"] = string.Empty;
//dr["DeptFileID"] = string.Empty;
//dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
GVRequest.DataSource = dt;
GVRequest.DataBind();
}
protected void BtnAddFileID_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
//GenerateUniqueData(0);
TextBoxFileIds.Text = "";
}
I have a Grid view containing 3 drop down lists and 3 text boxes each and a button. my drop down lists are populating data from data base. When i click on the button to add new row after filling the initial row. it sets the first row values fine and adds a new row. but the new row has no items in drop down lists this is my problem. secondly when i click the button again for adding third row it even looses the value of first row filled and same problem comes with first row that there are no items in drop down lists.
void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dt.Columns.Add(new DataColumn("Column5", typeof(string)));
dt.Columns.Add(new DataColumn("Column6", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dr["Column5"] = string.Empty;
dr["Column6"] = string.Empty;
dt.Rows.Add(dr);
//dr = dt.NewRow();
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
void AddNewRowToGrid()
{
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++)
{
//extract the TextBox and dropdown lists values
DropDownList list1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
DropDownList list2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
DropDownList list3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("DropDownList3");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = list1.SelectedItem.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = list2.SelectedItem.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column5"] = list3.SelectedItem.Text;
dtCurrentTable.Rows[i - 1]["Column6"] = box3.Text;
Session["list1"] = dtCurrentTable.Rows[i - 1]["Column1"];
Session["list2"] = dtCurrentTable.Rows[i - 1]["Column2"];
Session["list3"] = dtCurrentTable.Rows[i - 1]["Column5"];
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList list1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
DropDownList list2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
DropDownList list3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("DropDownList3");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox3");
list1.DataSource = bindddl();
list1.DataValueField = "Item_name";
list1.DataTextField = "Item_name";
list1.DataBind();
list1.Items.Insert(0, new ListItem("--Select--", "0"));
list2.DataSource = binddd2();
list2.DataValueField = "Sub_item";
list2.DataTextField = "Sub_item";
list2.DataBind();
list2.Items.Insert(0, new ListItem("--Select--", "0"));
list3.Items.Insert(0, new ListItem("--Select--", "0"));
list3.Items.Insert(1, new ListItem("Yes", "1"));
list3.Items.Insert(2, new ListItem("No", "2"));
box1.Text = dt.Rows[i]["Column3"].ToString();
box2.Text = dt.Rows[i]["Column4"].ToString();
box3.Text = dt.Rows[i]["Column6"].ToString();
list1.Text = Session["list1"].ToString();
list2.Text = Session["list2"].ToString();
box1.Text = dt.Rows[i]["Column3"].ToString();
box2.Text = dt.Rows[i]["Column4"].ToString();
list3.Text = Session["list3"].ToString();
box3.Text = dt.Rows[i]["Column6"].ToString();
// Response.Write(Session["list1"]);
rowIndex++;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
Fillcombo();
Fillcombo1();
Fillcombo2();
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
Fillcombo();
Fillcombo1();
Fillcombo2();
}
Fillcombo,Fillcombo1 are functions which are populating Drop down lists 1,2 through data base Fillcombo2 is populating it has two items yes' and 'no'.
My Fillcombo functions are like this
try
{
string connString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
using (var conn = new SqlConnection(connString))
using (var cmd = conn.CreateCommand())
{conn.Open();
SqlCommand cmdstr = new SqlCommand("SELECT [Sub_item] FROM [ADS].[dbo].[Accounts_heads_data]", conn);
// SqlCommand cmdstr = new SqlCommand("SELECT * FROM [ADS].[dbo].[Accounts_heads_data] ", conn);
SqlDataAdapter da2 = new SqlDataAdapter(cmdstr);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
DropDownList DropDownList2 = (DropDownList)Gridview1.Rows[0].FindControl("DropDownList2");
DropDownList2.DataValueField = ds2.Tables[0].Columns["Sub_item"].ToString();
DropDownList2.DataTextField = ds2.Tables[0].Columns["Sub_item"].ToString();
DropDownList2.DataSource = ds2.Tables[0];
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));
ViewState["fc1"] = ds2;
conn.Close();
}
}
catch (Exception ex)
{
Label1.Visible = true;
}
}
check by comenting
SetPreviousData(); line from
AddNewRowToGrid function
OR
in AddNewRowToGrid function only set data for new row and call SetPreviousData(); function in first row of AddNewRowToGrid function.
I compiled your code with some test data and changed the code a bit. Try following and see.
void AddNewRowToGrid()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
for (int i = 0; i < dtCurrentTable.Rows.Count-1; i++)
{
DropDownList list1 = (DropDownList)Gridview1.Rows[i].Cells[1].FindControl("DropDownList1");
DropDownList list2 = (DropDownList)Gridview1.Rows[i].Cells[2].FindControl("DropDownList2");
TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[4].FindControl("TextBox2");
DropDownList list3 = (DropDownList)Gridview1.Rows[i].Cells[5].FindControl("DropDownList3");
TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[6].FindControl("TextBox3");
dtCurrentTable.Rows[i]["Column1"] = list1.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column2"] = list2.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column3"] = box1.Text;
dtCurrentTable.Rows[i]["Column4"] = box2.Text;
dtCurrentTable.Rows[i]["Column5"] = list3.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column6"] = box3.Text;
}
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList list1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
DropDownList list2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DropDownList2");
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox2");
DropDownList list3 = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("DropDownList3");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox3");
Fillcombo();
Fillcombo1();
Fillcombo2();
box1.Text = dt.Rows[i]["Column3"].ToString();
box2.Text = dt.Rows[i]["Column4"].ToString();
box3.Text = dt.Rows[i]["Column6"].ToString();
if (i < dt.Rows.Count - 1)
{
list1.ClearSelection();
list1.Items.FindByText(dt.Rows[i]["Column1"].ToString()).Selected = true;
list2.ClearSelection();
list2.Items.FindByText(dt.Rows[i]["Column2"].ToString()).Selected = true;
list3.ClearSelection();
list3.Items.FindByText(dt.Rows[i]["Column5"].ToString()).Selected = true;
}
rowIndex++;
}
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
i have a grid view that , user will have to type something in the textboxes and wadever the user type will be inserted into the database , i have this 2 problem or issues whereby , everytime i click the "+" button ( adds news row ) , one of the textbox of the previous row data will be removed .. And the radiobutton selected will be removed as well. Heres is the screenshot what the problem is :
After clicking the " + " button , it will turn to be like this :
This is my code behind :
private void AddNewRowToGrid()
{
int rowIndex = 0;
CheckBox chkbox1 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox1");
CheckBox chkbox2 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox2");
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++)
{
//extract the TextBox values
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Hints"] = box3.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
// Set Previous Data on Postbacks
SetPreviousData();
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Question", typeof(string)));
dt.Columns.Add(new DataColumn("Hints", typeof(string)));
dr = dt.NewRow();
dr["Question"] = string.Empty;
dr["Hints"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3");
//Setting previous text to the respective textboxes based on columns.
box1.Text = dt.Rows[i]["Question"].ToString();
box2.Text = dt.Rows[i]["Hints"].ToString();
box3.Text = dt.Rows[i]["Hints"].ToString();
Session["Question1"] = box1.Text;
rowIndex++;
}
}
}
}
I can easily do this when only there is one textbox on each columns .. but now there is 2 textboxes on each columns ... because i can only append text to the textboxes based on columns names ... Also , how do i maintain my radiobutton help is appreciated , thanks !
private void AddNewRowToGrid()
{
int rowIndex = 0;
CheckBox chkbox1 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox1");
CheckBox chkbox2 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox2");
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++)
{
//extract the TextBox values
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Hints"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Hints1"] = box3.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
// Set Previous Data on Postbacks
SetPreviousData();
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Question", typeof(string)));
dt.Columns.Add(new DataColumn("Hints", typeof(string)));
dt.Columns.Add(new DataColumn("Hints1", typeof(string)));
dr = dt.NewRow();
dr["Question"] = string.Empty;
dr["Hints"] = string.Empty;
dr["Hints1"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox3");
//Setting previous text to the respective textboxes based on columns.
box1.Text = dt.Rows[i]["Question"].ToString();
box2.Text = dt.Rows[i]["Hints"].ToString();
box3.Text = dt.Rows[i]["Hints1"].ToString();
Session["Question1"] = box1.Text;
rowIndex++;
}
}
}
}
I just added another column rest of code was already working fine.