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.
Related
I am trying to use a checkbox control in gridview. Basically the user would enter an Item Description, the Quantity of the Item used, and if it has already been "Sold" out of inventory. The checkbox when checked would indicate the item was sold. Unchecked it can be null or no. I can store the value in the database as 1/2, true/false, yes/no. It doesn't matter.
I have tried using bit type, int, and varchar in my database. And have tried multiple ways of getting the value for the check in C# and can't seem to get it to work.
I've searched all over and have found examples on how to use it to save or delete multiple rows. But I need it to actually store a value in my db.
Here's code I've scavenged from other posts and almost have something working.
public partial class WebForm1 : System.Web.UI.Page
{
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);
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
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++)
{
TextBox itemDesc = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtItemDesc");
TextBox quantity = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtQuantity");
CheckBox sold = (CheckBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("alreadySold");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = itemDesc.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = quantity.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = sold.Checked.ToString();
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 itemDesc = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtItemDesc");
TextBox quantity = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtQuantity");
CheckBox sold = (CheckBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("alreadySold");
itemDesc.Text = dt.Rows[i]["Column1"].ToString();
quantity.Text = dt.Rows[i]["Column2"].ToString();
sold.Checked = dt.Rows[i]["Column3"].ToString().ToUpperInvariant() == "TRUE";
rowIndex++;
}
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void Save(object sender, System.EventArgs e)
{
if (ViewState["CurrentTable"] != null)
{
DataTable dt = new DataTable();
dt = ViewState["CurrentTable"] as DataTable;
if (dt.Rows.Count > 0)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
SqlCommand cmd = null;
foreach (GridViewRow row in Gridview1.Rows)
{
cmd = new SqlCommand("INSERT INTO SampleTest (ItemDesc, Quantity, Sold) VALUES (#Column1, #Column2, #Column3)", con);
string itemDesc = (row.FindControl("txtItemDesc") as TextBox).Text;
string quantity = (row.FindControl("txtQuantity") as TextBox).Text;
CheckBox sold = (row.FindControl("alreadySold") as CheckBox);
if (sold == null)
{
sold.Checked = false;
}
else
{
sold.Checked = true;
}
cmd.Parameters.AddWithValue("#Column1", itemDesc);
cmd.Parameters.AddWithValue("#Column2", quantity);
cmd.Parameters.AddWithValue("#Column3", sold);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
//cmd = new SqlCommand("SELECT Column1,Column2,Column3 FROM SampleTest", con);
//SqlDataAdapter da = new SqlDataAdapter(cmd);
//DataTable dt1 = new DataTable();
//da.Fill(dt1);
//this.gvSample.DataSource = dt1;
//this.gvSample.DataBind();
}
}
}
}
}
In your save, instead of CheckBox sold try something like:
bool sold = (row.FindControl("alreadySold") as CheckBox)?.Checked ?? false;
So a little explanation on the above:
as CheckBox can return null.
the '?' in CheckBox)? is a null conditional check that will prevent a null exception being thrown if it is null.
the '??' is saying that if the code preceding that is null, instead of assigning null, it assigns 'false'. Now if the code preceding the ?? is not null, then it will set the bool variable value to whatever .Checked actually is (true or false).
Now if your DB allows NULLs in that column and you are fine with that, you can also do:
bool? sold = (row.FindControl("alreadySold") as CheckBox)?.Checked;
^ bool? is a what is called a Nullable value type. Value types can't be null, but nullable value types wrap around a value type and allow it be null.
I'm an idiot. Thank you B.O.B. for explaining what was happening with the null value and the checkbox control. It really helped me understand what was happening. The reason the other things didn't work is because I had a type-o on the checkbox control on the HTML page. Once I fixed that everything works perfect.
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 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 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.