且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我的GridView在所有行中显示相同的数据。

更新时间:2023-12-01 11:05:34

为什么要遍历已保存在视图状态中的数据表中的所有行。您只需要将在页脚行中输入的数据附加到现有数据表中,不是吗?



当您循环遍历所有行并更新现有数据时当前值的值显示所有行的相同记录。



试试这个 -

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)
                {
                        //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





我在这里做的是,删除for循环并将所有页脚行值分配给新创建的数据行,最后将行添加到现有数据表中。



我没有在我的机器上试过它,但我确信它应该可行。如果它没有帮助,请告诉我。



希望,它有帮助:)



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 :)