Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using GridView in that when i click the insert button one row is adding and if i want to another row it is adding another row but this time what second row data i entered it coming in second and as well as in the first row also so please let me know the solution for this.

C#
public void AddNewRowToGrid()
        {
            int rowIndex = 0;
            

            if (ViewState["Sva_Medicines"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["Sva_Medicines"];
                DataRow drCurrentRow = null;               

                if (dtCurrentTable.Rows.Count > 0)
                {
                   // for (int i = 1; i <= drCurrentRow.Table.Columns.Count; i++)
                        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        //extract the TextBox values


                        DropDownList Presc_id = GridView1.FooterRow.FindControl("txtprescnum") as DropDownList;
                        object selectedValue = Presc_id.SelectedValue;
                        TextBox tmedname = GridView1.FooterRow.FindControl("txtmedname") as TextBox;
                        TextBox tmedbrand = GridView1.FooterRow.FindControl("txtmedbrand") as TextBox;
                        TextBox tmeduom = GridView1.FooterRow.FindControl("txtmeduom") as TextBox;
                        TextBox tmedprice = GridView1.FooterRow.FindControl("txtmedprice") as TextBox;
                        TextBox tmedcom = GridView1.FooterRow.FindControl("composition_txt") as TextBox;
                        TextBox medicine_name = GridView1.FooterRow.FindControl("txtmedname") as TextBox;
                        DropDownList Morningflag = GridView1.FooterRow.FindControl("morningflag_ddl") as DropDownList;
                        DropDownList Afternoonflag = GridView1.FooterRow.FindControl("afternoonflag_ddl") as DropDownList;
                        DropDownList Eveningflag = GridView1.FooterRow.FindControl("eveningflag_ddl") as DropDownList;
                        DropDownList Dosage = GridView1.FooterRow.FindControl("Dosage_ddl") as DropDownList;
                        DropDownList Smsreminder = GridView1.FooterRow.FindControl("smsremainder_ddl") as DropDownList;
                        DropDownList Monthlyrefill = GridView1.FooterRow.FindControl("Monthlyrefill_ddl") as DropDownList;
                        TextBox Purpose = GridView1.FooterRow.FindControl("Purpose_txt") as TextBox;
                        TextBox txtmedqty = GridView1.FooterRow.FindControl("txtmedqty") as TextBox;





                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;


                        dtCurrentTable.Rows[i - 1]["Medicine_name"] = tmedname;
                        
                        //dtCurrentTable.Rows[i - 1]["Presc_id"] = tprescnum.Text;

                        dtCurrentTable.Rows[i - 1]["Presc_num"] = Presc_id.SelectedItem.Text;
                        dtCurrentTable.Rows[i - 1]["Brand"] = tmedbrand;
                        dtCurrentTable.Rows[i - 1]["UOM"] = tmeduom.Text;
                        dtCurrentTable.Rows[i - 1]["Quantity"] = Convert.ToDouble(txtmedqty.Text); ;
                        dtCurrentTable.Rows[i - 1]["Price"] = tmedprice.Text;
                        dtCurrentTable.Rows[i - 1]["Composition"] = tmedcom.Text;
                        dtCurrentTable.Rows[i - 1]["Medicine_name"] = medicine_name.Text;
                        dtCurrentTable.Rows[i - 1]["Morning_Flag"] = Morningflag.Text;
                        dtCurrentTable.Rows[i - 1]["Afternoon_Flag"] = Afternoonflag.Text;
                        dtCurrentTable.Rows[i - 1]["Evening_Flag"] = Eveningflag.Text;
                        dtCurrentTable.Rows[i - 1]["Dosage"] = Dosage.Text;
                        dtCurrentTable.Rows[i - 1]["SMS_Remainder"] = Smsreminder.Text;
                        dtCurrentTable.Rows[i - 1]["Purpose"] = Purpose.Text;

                        
                        rowIndex++;
                    }
                        dtCurrentTable.Rows.Add(drCurrentRow);
                        ViewState["Sva_Medicines"] = dtCurrentTable;
                                     
                    GridView1.DataSource = dtCurrentTable;
                    GridView1.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }

            //Set Previous Data on Postbacks
            //SetPreviousData();

        }
        //Add Row Completed
Posted
Updated 29-Jun-15 21:44pm
v2
Comments
Arkadeep De 30-Jun-15 3:46am    
put a break point and check what value is present in Sva_Medicines viewstate....

1 solution

Why you have looped through all the rows present in the datatable already saved in the viewstate. You only need to append the data entered in the footer row to the existing datatable, isn't it?

As you are looping through all the rows and updating the existing values with the current values it was showing the same records for all the rows.

Try this-
C#
public void AddNewRowToGrid()
        {
            int rowIndex = 0;
            
 
            if (ViewState["Sva_Medicines"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["Sva_Medicines"];
                DataRow drCurrentRow = null;               
 
                if (dtCurrentTable.Rows.Count > 0)
                {
                        //extract the TextBox values 
                        DropDownList Presc_id = GridView1.FooterRow.FindControl("txtprescnum") as DropDownList;
                        object selectedValue = Presc_id.SelectedValue;
                        TextBox tmedname = GridView1.FooterRow.FindControl("txtmedname") as TextBox;
                        TextBox tmedbrand = GridView1.FooterRow.FindControl("txtmedbrand") as TextBox;
                        TextBox tmeduom = GridView1.FooterRow.FindControl("txtmeduom") as TextBox;
                        TextBox tmedprice = GridView1.FooterRow.FindControl("txtmedprice") as TextBox;
                        TextBox tmedcom = GridView1.FooterRow.FindControl("composition_txt") as TextBox;
                        TextBox medicine_name = GridView1.FooterRow.FindControl("txtmedname") as TextBox;
                        DropDownList Morningflag = GridView1.FooterRow.FindControl("morningflag_ddl") as DropDownList;
                        DropDownList Afternoonflag = GridView1.FooterRow.FindControl("afternoonflag_ddl") as DropDownList;
                        DropDownList Eveningflag = GridView1.FooterRow.FindControl("eveningflag_ddl") as DropDownList;
                        DropDownList Dosage = GridView1.FooterRow.FindControl("Dosage_ddl") as DropDownList;
                        DropDownList Smsreminder = GridView1.FooterRow.FindControl("smsremainder_ddl") as DropDownList;
                        DropDownList Monthlyrefill = GridView1.FooterRow.FindControl("Monthlyrefill_ddl") as DropDownList;
                        TextBox Purpose = GridView1.FooterRow.FindControl("Purpose_txt") as TextBox;
                        TextBox txtmedqty = GridView1.FooterRow.FindControl("txtmedqty") as TextBox;

 
                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count;
 

                        drCurrentRow["Medicine_name"] = tmedname;
                        drCurrentRow["Presc_num"] = Presc_id.SelectedItem.Text;
                        drCurrentRow["Brand"] = tmedbrand;
                        drCurrentRow["UOM"] = tmeduom.Text;
                        drCurrentRow["Quantity"] = Convert.ToDouble(txtmedqty.Text); ;
                        drCurrentRow["Price"] = tmedprice.Text;
                        drCurrentRow["Composition"] = tmedcom.Text;
                        drCurrentRow["Medicine_name"] = medicine_name.Text;
                        drCurrentRow["Morning_Flag"] = Morningflag.Text;
                        drCurrentRow["Afternoon_Flag"] = Afternoonflag.Text;
                        drCurrentRow["Evening_Flag"] = Eveningflag.Text;
                        drCurrentRow["Dosage"] = Dosage.Text;
                        drCurrentRow["SMS_Remainder"] = Smsreminder.Text;
                        drCurrentRow["Purpose"] = Purpose.Text;
 
                        
                        rowIndex++;
                        dtCurrentTable.Rows.Add(drCurrentRow);
                        ViewState["Sva_Medicines"] = dtCurrentTable;
                                     
                    GridView1.DataSource = dtCurrentTable;
                    GridView1.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }
 
            //Set Previous Data on Postbacks
            //SetPreviousData();

        }
        //Add Row Completed


What I have done here is, removed the for loop and assigned all the footer row values to the newly created datarow and finally added the row to the existing datatable.

I haven't tried it in my machine, but I am sure enough that it should work. In case it doesn't help, please let me know.

Hope, it helps :)
 
Share this answer
 
v2
Comments
Abdul Imran 30-Jun-15 7:13am    
Yes its working fine Thank you Mr.Shekher god bless you .you saved my day.
Suvendu Shekhar Giri 30-Jun-15 8:01am    
Glad that it helped :)
Please mark it as answer if it helped you so that others can take reference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900