开发者

Add new Records to GridView but DB should not be updated

开发者 https://www.devze.com 2023-01-23 03:58 出处:网络
i\'m having a gridview which is binded to a dataview, OnPageLoad i\'m populating gridview records. and i have a textbox and a button, So now i want to add more Records to GridView but this records sho

i'm having a gridview which is binded to a dataview, OnPageLoad i'm populating gridview records. and i have a textbox and a button, So now i want to add more Records to GridView but this records should not be get added into DB, they just added to the page and of course the default records which are coming from db stay.

<asp:GridView id="gvItems" runat="server">
 <Columns>
   <asp:TemplateField HeaderText="Item Code" SortExpression="ItemCode">
      <ItemTemplate>
           <asp:Label runat="server" ID="lblIItemCode" Text='<%# Bind("ItemCode") %>' />
      </ItemTemplate>
   </asp:TemplateField>
 </Columns>
</asp:GridView>

VB.NET Code:

dvSCart = data.GetSCart(Session("SCartId"), 0, varFilterClause, errorStr)
gvItems.DataSource = dvSCart
gvItems.DataBind()

Basically at first Data From DB comes to page then after that Textbox data sh开发者_如何学JAVAould get appended to the Gridview but Db should not get updated. i tried to do it by keeping DataView in ViewState or Session variable but later i came to know am going in a wrong way and that this won't work since Dataview can't be Serialized. i just need an idea or right path to do this, is this possible what am doing? may be i think jQuery will help, i googled for jQuery to do stuff but i failed or maybe i didn't understood since m a newbie to jQuery..

[my alternative option ]: create a temp db table and save it... but i don't want to use this option..


You should pull your data into a DataTable and/or DataSet and store it in a Session object. Then you can update this object with new rows and rebind ever time you make a change.

Example:

PageLoad

        if (!IsPostBack)
        {
            DataTable dt = new DataTable();

            using (SqlConnection con = new SqlConnection(conString))
            {
                SqlCommand cmd = new SqlCommand("SELECT Id, Name, Price FROM MyCart;", con);
                con.Open();

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
            }

            Session["MyData"] = dt;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

Now you have a datatable bound to your GridView1 and you have a copy of the source data in your session object. Now, lets add an item to the gridview and update our dt in our session object.

        DataTable dt = new DataTable();

        if (Session["MyData"] != null)
        {
            dt = (DataTable)Session["MyData"];
        }

        DataRow dr = dt.NewRow();
        dr["Id"] = dt.Rows.Count + 1;
        dr["Name"] = "My New Item";
        dr["Price"] = 19.99;

        dt.Rows.Add(dr);

        Session["MyData"] = dt;
        GridView1.DataSource = dt;
        GridView1.DataBind();

There you go, you've added an item to your datatable and rebinded it to your GridView.

If you want to do this all client side, you could easily add a table row using jQuery but you'd have to persist the table/data if you need it on multiple pages.


I'm still pretty new to .NET so take this with a "grain of salt"...

I think 'Bind' is a two way binding meaning data from the GridView's datasource is used to populate the grid and data entered into the GridView in edit mode will likewise populate the datasource. There is a one way binding directive called 'Eval' that I think might populate the grid without populating the datasource.

Another observation is that GridViews do not natively support insert operations so if you intend to allow new rows that do not come from the database you will have to add the insert controls and hook it up (or choose not to hook it up) to the data source yourself. I had a similar requirement where modifying the gridview was not supposed to modify the data source until an external 'Save' button was clicked. In my case I had an extra layer of business logic that served as the datasource. This business object data source represented a snapshot of the data base for the purposes of buffering changes made in the GridView until the 'Save' button was clicked. I was able to perserve this business object across postbacks by using the Session cache even though the business object was not serialiizable. I think it has to be serializable to store it in viewstate but maybe not session state. Maybe the viewstate is serialized and sent back and forth to the client whereas the session state is stored only on the server so doesn't need to be serialized.

Hope any of that helped.


You cannot mix bound records coming from your DB with new records in the way you describe.

To do this, use an unbound grid, which you populate from the DB and then add new records.


You can't manually populate server-side control GridView in non-server-side code, i.e. without post back.

You can join data from db with manual data and then data bind it onto GridView. But you will do this in code-behind, i.e. using post back.

If GridView.DataSource has no InsertCommand then no data will be inserted! Don't afraid post back!


One way of doing it.

1. get data from DB, put it in a List of object that represents rows/records.
2. bind this list to GridView, this way you are binding to list of objects and not DB records.
3. when adding new item, add to the list.
4. rebind GridView

0

精彩评论

暂无评论...
验证码 换一张
取 消