This is how my gridview looks like :
Basically , I select "you" as the correct answer .
But after I click the "+" button , the selection of radio button got removed .
How do I keep my selection of radio button for every row of gridview?
This is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
dropActivity(); // ignore this , this is for drop down list
dropTask(); // ignore this , this is for drop down list
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Question", typeof(string)));
dt.Columns.Add(new DataColumn("Hints No.1", typeof(string)));
dt.Columns.Add(new DataColumn("Hints No.2", typeof(string)));
dr = dt.NewRow();
dr["Question"] = string.Empty;
dr["Hints No.1"] = string.Empty;
dr["Hints No.2"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
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++)
{
//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 = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Hints No.1"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Hints No.2"] = 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 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[3].FindControl("TextBox3");
RadioButton radiobtn1 = (RadioButton)GridView1.Rows[i].Cells[2].FindControl("RadioButton1");
RadioButton radiobtn2 = (RadioButton)GridView1.Rows[i].Cells[3].FindControl("RadioButton2");
//Setting previous text to the respective textboxes based on columns.
box1.Text = dt.Rows[i]["Question"].ToString();
box2.Text = dt.Rows[i]["Hints No.1"].ToString();
box3.Text = dt.Rows[i]["Hints No.2"].ToString();
if (radiobtn1.Checked == true)
{
ViewState["RadioButtonStatus"] = radiobtn1.Checked;
}
if (radiobtn2.Checked == true)
{
ViewState["RadioButtonStatus"] = radiobtn2.Checked;
}
Session["Question1"] = box1.Text;
rowIndex++;
}
}
}
}
Basically I need to keep the selection of radio button for every row I have created.
Consider changing the "+" button to trigger an event on the client side instead of posting to the server for a new row. You could then just loop through the collection when you post it to your "Save" event handler on the server.
Currently what seems to be happening is a postback from you hitting the "+" button. A postback would result in the clearing of those radio buttons. I see you're trying to retain the status via the ViewState but you're saving the checked status for the same ViewState property.
See the difference in the code below.
if (radiobtn1.Checked == true)
{
ViewState["RadioButtonStatus"] = radiobtn1.Checked;
}
if (radiobtn2.Checked == true)
{
ViewState["DifferentRadioButtonStatus"] = radiobtn2.Checked;
}
Here are some updates to your AddNewRow method. There's a lot of stuff going on here, this may be a little bit over engineered. Hope it helps you move towards the right direction.
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
var drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows.Add(drCurrentRow);
// Updated from > 0 to > 1 since the first action you're taking is in GridView1.Rows[1] via the for loop below
if (dtCurrentTable.Rows.Count > 1)
{
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[3].FindControl("TextBox3");
dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Hints No.1"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Hints No.2"] = box3.Text;
rowIndex++;
}
ViewState["CurrentTable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
// Set Previous Data on Postbacks
SetPreviousData();
}
Related
I have created dynamic textboxs and dropdownlist in gridview. It is working perfectly. Further I want to fire event in dynamic dropdownlist and do some action.
private ArrayList GetDummyData() {
ArrayList arr = new ArrayList();
arr.Add(new ListItem("Item1", "1"));
arr.Add(new ListItem("Item2", "2"));
arr.Add(new ListItem("Item3", "3"));
arr.Add(new ListItem("Item4", "4"));
arr.Add(new ListItem("Item5", "5"));
return arr;
}
private void FillDropDownList(DropDownList ddl) {
ArrayList arr = GetDummyData();
foreach (ListItem item in arr) {
ddl.Items.Add(item);
}
}
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)));//for TextBox value
dt.Columns.Add(new DataColumn("Column2", typeof(string)));//for TextBox value
dt.Columns.Add(new DataColumn("Column3", typeof(string)));//for DropDownList selected item
dt.Columns.Add(new DataColumn("Column4", typeof(string)));//for DropDownList selected item
dr = dt.NewRow();
dr["RowNumber"] = 1;
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();
//After binding the gridview, we can then extract and fill the DropDownList with Data
DropDownList ddl1 = (DropDownList)Gridview1.Rows[0].Cells[3].FindControl("DropDownList1");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[4].FindControl("DropDownList2");
FillDropDownList(ddl1);
FillDropDownList(ddl2);
}
private 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;
//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState for future reference
ViewState["CurrentTable"] = dtCurrentTable;
for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++) {
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2");
dtCurrentTable.Rows[i]["Column1"] = box1.Text;
dtCurrentTable.Rows[i]["Column2"] = box2.Text;
//extract the DropDownList Selected Items
DropDownList ddl1 = (DropDownList)Gridview1.Rows[i].Cells[3].FindControl("DropDownList1");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[i].Cells[4].FindControl("DropDownList2");
// Update the DataRow with the DDL Selected Items
dtCurrentTable.Rows[i]["Column3"] = ddl1.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column4"] = ddl2.SelectedItem.Text;
}
//Rebind the Grid with the current data to reflect changes
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 box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2");
DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList1");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("DropDownList2");
//Fill the DropDownList with Data
FillDropDownList(ddl1);
FillDropDownList(ddl2);
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();
//Set the Previous Selected Items on Each DropDownList on Postbacks
ddl1.ClearSelection();
ddl1.Items.FindByText(dt.Rows[i]["Column3"].ToString()).Selected = true;
ddl2.ClearSelection();
ddl2.Items.FindByText(dt.Rows[i]["Column4"].ToString()).Selected = true;
}
rowIndex++;
}
}
}
}
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
SetInitialRow();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e) {
AddNewRowToGrid();
}
protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
DataTable dt = (DataTable)ViewState["CurrentTable"];
LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
if (lb != null) {
if (dt.Rows.Count > 1) {
if (e.Row.RowIndex == dt.Rows.Count - 1) {
lb.Visible = false;
}
}
else {
lb.Visible = false;
}
}
}
}
protected void LinkButton1_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 and reset row number
dt.Rows.Remove(dt.Rows[rowID]);
ResetRowID(dt);
}
}
//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();
}
private void ResetRowID(DataTable dt) {
int rowNumber = 1;
if (dt.Rows.Count > 0) {
foreach (DataRow row in dt.Rows) {
row[0] = rowNumber;
rowNumber++;
}
}
}
Might be a duplicate of question:
how to add an OnClick event on DropDownList's ListItem that is added dynamically?
Please check the answer given there.
DropDownList changesList = new DropDownList();
ListItem item;
item = new ListItem();
item.Text = "go to google.com";
item.Value = "http://www.google.com";
changesList.Items.Add(item);
changesList.Attributes.Add("onChange", "location.href = this.options[this.selectedIndex].value;");
Also have you tried adding a handler to the event SelectedIndexChanged dynamically when adding the new row to the grid?
I have a strange problem with my code
I can't add row because NullReferenceException in my gridview after delete a row.
My logic is when Add new row, i have the other function to SetPreviousData.
When I didn't delete a row, my SetPreviousData is don't raise any exception, but when after deleting a row and create new row, my SetPreviousData raise a exception that NullReferenceException.
This is my code for 'AddNewRow()'
private void AddNewRowToGrid()
{
if (ViewState["vsCurrent"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["vsCurrent"];
DataRow drNewRow = dtCurrentTable.NewRow();
drNewRow["SALES_SO_LITEM_ID"] = Convert.ToInt32(dtCurrentTable.Rows.Count.ToString()+"101");
drNewRow["ITEM_NAME"] = string.Empty;
drNewRow["QUANTITY"] = 0;
drNewRow["PRICE"] = 0;
dtCurrentTable.Rows.Add(drNewRow);
ViewState["vsCurrent"] = dtCurrentTable;
}
else
{
ViewState["vsCurrent"] = SetInitialRow();
}
BindDataGrid();
SetPreviousData(); //Set Previous Data on Postbacks
}
and this function for SetPreviousData
public void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["vsCurrent"] != null)
{
DataTable dt = (DataTable)ViewState["vsCurrent"];
if (dt.Rows.Count > 0)
{
int max_rowindex = gvDetailSales.Rows.Count - 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i].RowState != DataRowState.Deleted)
{
Label box1 = (Label)gvDetailSales.Rows[rowIndex].Cells[1].FindControl("lblItemName");
// raise exception because null
Label box2 = (Label)gvDetailSales.Rows[rowIndex].Cells[2].FindControl("lblQuantity");
Label box3 = (Label)gvDetailSales.Rows[rowIndex].Cells[3].FindControl("lblPrice");
box1.Text = dt.Rows[i]["ITEM_NAME"].ToString();
box2.Text = dt.Rows[i]["QUANTITY"].ToString();
box3.Text = dt.Rows[i]["PRICE"].ToString();
rowIndex++;
}
}
}
}
}
How I can handle it ? Thank You
======================EDITED================================================
This is my function for SetInitialRow
public DataTable SetInitialRow()
{
DataTable dtInitial = new DataTable();
DataRow drInitial = null;
dtInitial.Columns.Add(new DataColumn("SALES_SO_LITEM_ID", typeof(string)));
dtInitial.Columns.Add(new DataColumn("ITEM_NAME", typeof(string)));
dtInitial.Columns.Add(new DataColumn("QUANTITY", typeof(int)));
dtInitial.Columns.Add(new DataColumn("PRICE", typeof(float)));
drInitial = dtInitial.NewRow();
drInitial["SALES_SO_LITEM_ID"] = Convert.ToInt32(dtInitial.Rows.Count.ToString() + "101");
drInitial["ITEM_NAME"] = string.Empty;
drInitial["QUANTITY"] = 0;
drInitial["PRICE"] = 0.0;
dtInitial.Rows.Add(drInitial);
return dtInitial;
}
This is my function for BindDataGrid
public void BindDataGrid()
{
gvDetailSales.DataSource = ViewState["vsCurrent"] as DataTable;
gvDetailSales.DataBind();
}
protected void ddlbranchdate_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DataTable dtgetvalues = new DataTable();
//objRetailPL.date = Convert.ToDateTime(hdndate.Value);
objRetailPL.branchdate = ddlbranchdate.SelectedItem.ToString();
dtgetvalues = objRetailBAL.getbradisdate(objRetailPL);
foreach(GridViewRow row in gvMeatDispatch.Rows)
{
DataTable dtpr = new DataTable();
DropDownList ddl1 = (DropDownList)row.FindControl("ddlpartyname");
objRetailPL.branch = dtgetvalues.Rows[0]["branch"].ToString();
dtpr = objRetailBAL.getbran(objRetailPL);
ddl1.DataSource = dtpr;
ddl1.DataTextField = "partyname";
ddl1.DataValueField = "sno";
ddl1.DataBind();
ddl1.Items.Add(new ListItem("--Select--", "0"));
ddl1.SelectedIndex = ddl1.Items.Count - 1;
}
}
}
why i am getting this exception?..
i am using Dynamic Gridview and adding Rows Dynamically with the help of Button Control..
Outside i am declare another dropdown list ..if user selected some value from this Dropdownlist
then automatically bind the values to Dynamic Gridview ,Dropdown list values..
This is my Setprevious Data in Dynamic control Method..Here i called Dropdownlist selectedindexchanged event ..like..
private void SetPreviousData()
{
try
{
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 ddlpname = (DropDownList)gvMeatDispatch.Rows[rowIndex].Cells[1].FindControl("ddlpartyname");
DropDownList ddlbt = (DropDownList)gvMeatDispatch.Rows[rowIndex].Cells[2].FindControl("ddlbirdtype");
TextBox txttotwt = (TextBox)gvMeatDispatch.Rows[rowIndex].Cells[3].FindControl("txttotwt");
TextBox txttransbirds1 = (TextBox)gvMeatDispatch.Rows[rowIndex].Cells[4].FindControl("txtrateforkg");
TextBox txtmort1 = (TextBox)gvMeatDispatch.Rows[rowIndex].Cells[5].FindControl("txtdcno");
// Dropdownlist selectedIndex Changed event
ddlbranchdate_SelectedIndexChanged(null, null);
ddlpname.SelectedValue = dt.Rows[i]["Col1"].ToString();
ddlbt.SelectedValue = dt.Rows[i]["Col2"].ToString();
txttotwt.Text = dt.Rows[i]["col3"].ToString();
txttransbirds1.Text = dt.Rows[i]["Col4"].ToString();
txtmort1.Text = dt.Rows[i]["Col5"].ToString();
rowIndex++;
}
}
}
}
Here I am using GridView Control.. My output screen is this..
My Coding is..
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("S.No", typeof(string)));
dt.Columns.Add(new DataColumn("Article No", typeof(string)));
dt.Columns.Add(new DataColumn("Item Description", typeof(string)));
dt.Columns.Add(new DataColumn("Order Quantity", typeof(string)));
dt.Columns.Add(new DataColumn("UOM", typeof(string)));
dt.Columns.Add(new DataColumn("Rate", typeof(string)));
dt.Columns.Add(new DataColumn("Currency", typeof(string)));
dt.Columns.Add(new DataColumn("Amount", typeof(string)));
DataRow dr = dt.NewRow();
dr["S.No"] = 1;
dr["Article No"] = string.Empty;
dr["Item Description"] = string.Empty;
dr["Order Quantity"] = 0;
dr["UOM"] = string.Empty;
dr["Rate"] = 0.00;
dr["Currency"] = string.Empty;
dr["Amount"] = 0.00;
dt.Rows.Add(dr);
ViewState["currenttable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
DropDownList box2 = (DropDownList)GridView1.Rows[0].Cells[1].FindControl("DropDownList1");
box2.DataSource = SIMBL.ShowSalesOrderArticleNumberInfo();
box2.DataTextField = "Port_Number";
box2.DataValueField = "Description";
box2.DataBind();
box2.Items.Insert(0, " ");
DropDownList box12 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("DropDownList3");
box12.DataSource = SIMBL.ShowSimpleUnitItemInfo();
box12.DataTextField = "Name";
box12.DataValueField = "Symbol";
box12.DataBind();
box12.Items.Insert(0, " ");
DropDownList box13 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("DropDownList4");
box13.DataSource = SIMBL.ShowSimpleUnitItemInfo();
box13.DataTextField = "Name";
box13.DataValueField = "Symbol";
box13.DataBind();
box13.Items.Insert(0, " ");
DropDownList box14 = (DropDownList)GridView1.Rows[0].Cells[4].FindControl("DropDownList5");
box14.DataSource = SIMBL.ShowSimpleUnitItemInfo();
box14.DataTextField = "Name";
box14.DataValueField = "Symbol";
box14.DataBind();
box14.Items.Insert(0, " ");
}
}
public void addnewrow()
{
int rowIndex = 0;
if (ViewState["currenttable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["currenttable"];
DataRow drCurrentRow = null;
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[0].FindControl("TextBox6");
DropDownList box2 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox8");
TextBox box4 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox9");
TextBox box5 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox10");
TextBox box6 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox11");
Label box7 = (Label)GridView1.Rows[rowIndex].Cells[3].FindControl("Label6");
TextBox box8 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox13");
TextBox box9 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox14");
TextBox box10 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox15");
Label box11 = (Label)GridView1.Rows[rowIndex].Cells[4].FindControl("Label7");
DropDownList box12 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList3");
DropDownList box13 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList4");
DropDownList box14 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList5");
Label box15 = (Label)GridView1.Rows[rowIndex].Cells[5].FindControl("Label8");
TextBox box16 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox17");
TextBox box17 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox18");
TextBox box18 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox19");
Label box19 = (Label)GridView1.Rows[rowIndex].Cells[6].FindControl("Label9");
DropDownList box20 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList7");
DropDownList box21 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList8");
DropDownList box22 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList9");
Label box23 = (Label)GridView1.Rows[rowIndex].Cells[7].FindControl("Label0");
TextBox box24 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox21");
TextBox box25 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox22");
TextBox box26 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox23");
TextBox box27 = (TextBox)GridView1.FooterRow.Cells[7].FindControl("TextBox24");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["S.No"] = i + 1;
dtCurrentTable.Rows[i - 1]["Article No"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Item Description"] = box3.Text + "," + box4.Text + "," + box5.Text + "," + box6.Text;
dtCurrentTable.Rows[i - 1]["Order Quantity"] = box8.Text + "," + box9.Text + "," + box10.Text;
dtCurrentTable.Rows[i - 1]["UOM"] = box12.Text + "," + box13.Text + "," + box14.Text;
dtCurrentTable.Rows[i - 1]["Rate"] = box16.Text + "," + box17.Text + "," + box18.Text;
dtCurrentTable.Rows[i - 1]["Currency"] = box20.Text + "," + box21.Text + "," + box22.Text;
dtCurrentTable.Rows[i - 1]["Amount"] = box24.Text + "," + box25.Text + "," + box26.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["currenttable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
for (int i = 0; i <= dtCurrentTable.Rows.Count - 1; i++)
{
DropDownList box2 = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("DropDownList1");
box2.DataSource = SIMBL.ShowSalesOrderArticleNumberInfo();
box2.DataTextField = "Port_Number";
box2.DataValueField = "Description";
box2.DataBind();
box2.Items.Insert(0, " ");
DropDownList box12 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList3");
box12.DataSource = SIMBL.ShowSimpleUnitItemInfo();
box12.DataTextField = "Name";
box12.DataValueField = "Symbol";
box12.DataBind();
box12.Items.Insert(0, " ");
DropDownList box13 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList4");
box13.DataSource = SIMBL.ShowSimpleUnitItemInfo();
box13.DataTextField = "Name";
box13.DataValueField = "Symbol";
box13.DataBind();
box13.Items.Insert(0, " ");
DropDownList box14 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList5");
box14.DataSource = SIMBL.ShowSimpleUnitItemInfo();
box14.DataTextField = "Name";
box14.DataValueField = "Symbol";
box14.DataBind();
box14.Items.Insert(0, " ");
}
}
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 - 1; i++)
{
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[0].FindControl("TextBox6");
DropDownList box2 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox8");
TextBox box4 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox9");
TextBox box5 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox10");
TextBox box6 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox11");
Label box7 = (Label)GridView1.Rows[rowIndex].Cells[3].FindControl("Label6");
TextBox box8 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox13");
TextBox box9 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox14");
TextBox box10 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox15");
Label box11 = (Label)GridView1.Rows[rowIndex].Cells[4].FindControl("Label7");
DropDownList box12 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList3");
DropDownList box13 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList4");
DropDownList box14 = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("DropDownList5");
Label box15 = (Label)GridView1.Rows[rowIndex].Cells[5].FindControl("Label8");
TextBox box16 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox17");
TextBox box17 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox18");
TextBox box18 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox19");
Label box19 = (Label)GridView1.Rows[rowIndex].Cells[6].FindControl("Label9");
DropDownList box20 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList7");
DropDownList box21 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList8");
DropDownList box22 = (DropDownList)GridView1.Rows[rowIndex].Cells[6].FindControl("DropDownList9");
Label box23 = (Label)GridView1.Rows[rowIndex].Cells[7].FindControl("Label0");
TextBox box24 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox21");
TextBox box25 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox22");
TextBox box26 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("TextBox23");
TextBox box27 = (TextBox)GridView1.FooterRow.Cells[7].FindControl("TextBox24");
box1.Text = dt.Rows[i]["S.No"].ToString();
box2.Text = dt.Rows[i]["Article No"].ToString();
if (Convert.ToString(dt.Rows[i]["Item Description"]).Trim() != "")
{
string[] strDesc = Convert.ToString(dt.Rows[i]["Item Description"]).Trim().Split(',');
box3.Text = strDesc[0].ToString();
box4.Text = strDesc[1].ToString();
box5.Text = strDesc[2].ToString();
box6.Text = strDesc[3].ToString();
}
if (Convert.ToString(dt.Rows[i]["Order Quantity"]).Trim() != "")
{
string[] strDesc = Convert.ToString(dt.Rows[i]["Order Quantity"]).Trim().Split(',');
box8.Text = strDesc[0].ToString();
box9.Text = strDesc[1].ToString();
box10.Text = strDesc[2].ToString();
}
if (Convert.ToString(dt.Rows[i]["UOM"]).Trim() != "")
{
string[] strDesc = Convert.ToString(dt.Rows[i]["UOM"]).Trim().Split(',');
box12.Text = strDesc[0].ToString();
box13.Text = strDesc[1].ToString();
box14.Text = strDesc[2].ToString();
}
if (Convert.ToString(dt.Rows[i]["Rate"]).Trim() != "")
{
string[] strDesc = Convert.ToString(dt.Rows[i]["Rate"]).Trim().Split(',');
box16.Text = strDesc[0].ToString();
box17.Text = strDesc[1].ToString();
box18.Text = strDesc[2].ToString();
}
if (Convert.ToString(dt.Rows[i]["Currency"]).Trim() != "")
{
string[] strDesc = Convert.ToString(dt.Rows[i]["Currency"]).Trim().Split(',');
box20.Text = strDesc[0].ToString();
box21.Text = strDesc[1].ToString();
box22.Text = strDesc[2].ToString();
}
if (Convert.ToString(dt.Rows[i]["Amount"]).Trim() != "")
{
string[] strDesc = Convert.ToString(dt.Rows[i]["Amount"]).Trim().Split(',');
box24.Text = strDesc[0].ToString();
box25.Text = strDesc[1].ToString();
box26.Text = strDesc[2].ToString();
}
rowIndex++;
}
}
}
}
//DropDownList Selection Change..
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["currenttable"];
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
TextBox box3 = (TextBox)GridView1.Rows[i].Cells[2].FindControl("TextBox8");
DropDownList box2 = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("DropDownList1");
box3.Text = box2.SelectedItem.Value;
}
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["currenttable"];
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
DropDownList box12 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList3");
box12.Text = box12.SelectedItem.Value;
}
}
protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["currenttable"];
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
DropDownList box13 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList4");
box13.Text = box13.SelectedItem.Value;
}
}
protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["currenttable"];
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
DropDownList box14 = (DropDownList)GridView1.Rows[i].Cells[4].FindControl("DropDownList5");
box14.Text = box14.SelectedItem.Value;
}
}
protected void DropDownList7_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["currenttable"];
decimal a = 0.00M;
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
DropDownList box20 = (DropDownList)GridView1.Rows[i].Cells[6].FindControl("DropDownList7");
box20.Text = box20.SelectedItem.Value;
TextBox box24 = (TextBox)GridView1.Rows[i].Cells[7].FindControl("TextBox21");
TextBox box16 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("TextBox17");
TextBox box8 = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox13");
TextBox box27 = (TextBox)GridView1.FooterRow.Cells[7].FindControl("TextBox24");
box24.Text = (Convert.ToDecimal(box16.Text) * Convert.ToDecimal(box8.Text)).ToString();
if (a != null)
{
box27.Text = (a + Convert.ToDecimal(box24.Text)).ToString();
a = Convert.ToDecimal(box27.Text);
}
else
{
a = 0.00M;
}
}
}
protected void DropDownList8_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["currenttable"];
decimal b = 0.00M;
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
DropDownList box21 = (DropDownList)GridView1.Rows[i].Cells[6].FindControl("DropDownList8");
box21.Text = box21.SelectedItem.Value;
TextBox box25 = (TextBox)GridView1.Rows[i].Cells[7].FindControl("TextBox22");
TextBox box17 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("TextBox18");
TextBox box9 = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox14");
TextBox box27 = (TextBox)GridView1.FooterRow.Cells[7].FindControl("TextBox24");
box25.Text = (Convert.ToDecimal(box17.Text) * Convert.ToDecimal(box9.Text)).ToString();
if (b != null)
{
box27.Text = (Convert.ToDecimal(box27.Text) + Convert.ToDecimal(box25.Text)).ToString();
b = Convert.ToDecimal(box27.Text);
}
else
{
b = 0.00M;
}
}
}
protected void DropDownList9_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["currenttable"];
decimal c = 0.00M;
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
DropDownList box22 = (DropDownList)GridView1.Rows[i].Cells[6].FindControl("DropDownList9");
box22.Text = box22.SelectedItem.Value;
TextBox box26 = (TextBox)GridView1.Rows[i].Cells[7].FindControl("TextBox23");
TextBox box18 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("TextBox19");
TextBox box10 = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox15");
TextBox box27 = (TextBox)GridView1.FooterRow.Cells[7].FindControl("TextBox24");
box26.Text = (Convert.ToDecimal(box18.Text) * Convert.ToDecimal(box10.Text)).ToString();
if (c != null)
{
box27.Text = (Convert.ToDecimal(box27.Text) + Convert.ToDecimal(box26.Text)).ToString();
c = Convert.ToDecimal(box27.Text);
}
else
{
c = 0.00M;
}
}
}
//For Button Click...
protected void Button1_Click1(object sender, EventArgs e)
{
addnewrow();
}
Here When I am entering the first row 3 "Amount" cell control in the row is working fine..here I can reduce the cell count values in the row..
when I am entering the next rows I need to enter more than cell count which is in previous row cell count.. otherwise I am getting a error like "Input string was not in correct format"..
And the total also calculated based on the previous row cells..
If entering all the 3 cell values in every row means its working well..
if I am getting the error at the screen shot stage means it's calculating only 1'st cell values in all the rows..
If i have 1'st row 3 values and 2'nd row 2 values means it's calculating the 2 cell values in the each previous rows..
Based on this manner it's working.. how to do this? anyone help me to solve this problem...
Thanks in advance..
Please change code as like below within for loop of all three dropdown events
DropDownList7_SelectedIndexChanged
DropDownList8_SelectedIndexChanged
DropDownList9_SelectedIndexChanged
to get Total of Amount Column....
Replace your below code in above three event...
TextBox box26 = (TextBox)GridView1.Rows[i].Cells[7].FindControl("TextBox23");
TextBox box18 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("TextBox19");
TextBox box10 = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox15");
TextBox box27 = (TextBox)GridView1.FooterRow.Cells[7].FindControl("TextBox24");
box26.Text = (Convert.ToDecimal(box18.Text) * Convert.ToDecimal(box10.Text)).ToString();
if (c != null)
{
box27.Text = (Convert.ToDecimal(box27.Text) + Convert.ToDecimal(box26.Text)).ToString();
c = Convert.ToDecimal(box27.Text);
}
else
{
c = 0.00M;
}
with below code...
TextBox box24 = (TextBox)GridView1.Rows[i].Cells[7].FindControl("TextBox21");
TextBox box16 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("TextBox17");
TextBox box8 = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox13");
TextBox box25 = (TextBox)GridView1.Rows[i].Cells[7].FindControl("TextBox23"); // Second Control of Amount Column
TextBox box26 = (TextBox)GridView1.Rows[i].Cells[7].FindControl("TextBox24"); // Third Control of Amount Column
TextBox box17 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("TextBox18"); // Second control of Rate Column
TextBox box9 = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox14"); // Second control of Order quantity Column
TextBox box18 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("TextBox19"); // Third control of Rate Column
TextBox box10 = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox15"); // Third control of Order quantity Column
TextBox box27 = (TextBox)GridView1.FooterRow.Cells[7].FindControl("TextBox24");
box24.Text = (Convert.ToDecimal(box16.Text) * Convert.ToDecimal(box8.Text)).ToString();
box25.Text = (Convert.ToDecimal(box17.Text) * Convert.ToDecimal(box9.Text)).ToString();
box26.Text = (Convert.ToDecimal(box18.Text) * Convert.ToDecimal(box10.Text)).ToString();
if (a != null)
{
box27.Text = (a + Convert.ToDecimal(box24.Text) + Convert.ToDecimal(box25.Text) + Convert.ToDecimal(box26.Text)).ToString();
a = Convert.ToDecimal(box27.Text);
}
else
{
a = 0.00M;
}
I have this code here :
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Question", typeof(string)));
dt.Columns.Add(new DataColumn("Answer", typeof(string)));
dr = dt.NewRow();
dr["Question"] = string.Empty;
dr["Answer"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
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++)
{
//extract the TextBox values
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Answer"] = box2.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 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");
box1.Text = dt.Rows[i]["Question"].ToString();
box2.Text = dt.Rows[i]["Answer"].ToString();
Session["Question1"] = box1.Text;
rowIndex++;
}
}
}
}
protected void btnAdd_Click1(object sender, EventArgs e)
{
AddNewRowToGrid();
}
And now i have another button call btnCreate . I click the add button to add rows in Grid View , means adding one row and so on every click . After i click create button , i want the rows number back to one , for now , the rows will be stuck at the number of times i click the add button until i refresh the page. I need the number of rows to be reseted to 1 after i have click on the create button .
You can direcly call SetInitialRow() method in your btnCreate click event, that means you are binding a single row to gridview which mean number of rows reseted to 1.
You can add this to the btnAdd_Click1 method:
dtCurrentTable.Rows(1).Selected = True
I have tried your same code and it works in mine.
Like this.
protected void Page_Load(object sender, EventArgs e)
{
SetInitialRow();
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Question", typeof(string)));
dt.Columns.Add(new DataColumn("Answer", typeof(string)));
dr = dt.NewRow();
dr["Question"] = string.Empty;
dr["Answer"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
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++)
{
//extract the TextBox values
TextBox box1 = (TextBox)GridView1.Rows[i].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView1.Rows[i].FindControl("TextBox2");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Answer"] = box2.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 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].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].FindControl("TextBox2");
box1.Text = dt.Rows[i]["Question"].ToString();
box2.Text = dt.Rows[i]["Answer"].ToString();
Session["Question1"] = box1.Text;
rowIndex++;
}
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void prevRow_Click(object sender, EventArgs e)
{
SetPreviousData();
}
I am sory sory if my answer is on wrong track depending on your requirement. Comment and queries are welcome. thank you