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-
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)
{
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");
}
}
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 :)