Inserting and Editing data when using a in-memory datatable

7 posts, 0 answers
  1. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 28 Dec 2010 Link to this post

    I am creating a grid dynamically and its datasource is a datatable. I am not using need data source event since I am creating the grid dynamically.
    I cannot insert an item or update an item. The datasource does not reflect the changes.
    Also, when I click on 'Insert' link  the grid remains in edit mode.
    Can someone tell me how to insert and update to an im-memory datatable ( not database)?

    Thanks
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 Dec 2010 Link to this post

    Hello Sunil,

    Here is a sample application which shows how to insert and update data into datatable.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="true" OnNeedDataSource="RadGrid1_NeedDataSource"
        OnInsertCommand="RadGrid1_InsertCommand" OnUpdateCommand="RadGrid1_UpdateCommand">
        <MasterTableView ShowHeadersWhenNoRecords="true" CommandItemDisplay="Top">
            <Columns>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
        DataTable dtValues;
       protected void Page_Load(object sender, EventArgs e)
       {
       }
       protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
       {
           GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
           TextBox txt1 = (TextBox)insertItem["Items"].Controls[0];
           TextBox txt2 = (TextBox)insertItem["Rate"].Controls[0];
          
           dtValues = (DataTable)Session["Table"];
           DataRow drValues = dtValues.NewRow();
           drValues["Items"] = txt1.Text;
           drValues["Rate"] = txt2.Text;
     
           dtValues.Rows.Add(drValues);//adding new row into datatable
           dtValues.AcceptChanges();
           Session["Table"] = dtValues;
           RadGrid1.Rebind();
          
       }
       protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
       {
           //creating datatable
           dtValues = new DataTable();
           dtValues.Columns.Add("Items");
           dtValues.Columns.Add("Rate");
           if (Session["Table"] != null)
           {
               dtValues = (DataTable)Session["Table"];
           }
           RadGrid1.DataSource = dtValues;//populate RadGrid with datatable
           Session["Table"] = dtValues;
       }
       protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
       {
           GridEditFormItem updateItem = (GridEditFormItem)e.Item;
           int rowindex=updateItem.ItemIndex;
           TextBox txt1 = (TextBox)updateItem["Items"].Controls[0];
           TextBox txt2 = (TextBox)updateItem["Rate"].Controls[0];
     
           dtValues = (DataTable)Session["Table"];
           dtValues.Rows[rowindex]["Items"] = txt1.Text;
           dtValues.Rows[rowindex]["Rate"] = txt2.Text;
           dtValues.AcceptChanges();//updating datatable with new values
           Session["Table"] = dtValues;
           RadGrid1.Rebind();
       }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 29 Dec 2010 Link to this post

    Hi Princy,

    Thanks for the response. The limitation in my case is that I cannot use need datasource event and the complete grid is created in code-behind. There is nothing in html file about the radgrid. I think because of this limitation there is something that I am missing. The example you gave works perfectly but it does not help me see what I am missing in my case.

    Thanks
    Sunil
  5. Margo Noreen
    Margo Noreen avatar
    25 posts
    Member since:
    Mar 2010

    Posted 29 Dec 2010 Link to this post

    I have been testing some code similar to this... I need a grid that is initially bound to a DataTable then lets the user insert and edit rows "at will"... Only after they click a separate "Save" button do I commit those changes back to the db.  The changes are saved to the DataTable and the DataTable is stashed in and retrieved from ViewState (since we have users popping up multiple browser windows, and in that means Session state really doesn't work in that case!).

    The test code I have works, but there's a lot of code!  In particular, there's a lot of work around inserting rows and keeping track of that with a unique value for the DataKey value...

    Any chance Telerik can create a version of the grid that supports this use case and hides the sheer amount of hand coding that's going on? 

    I need to use this on many, many pages in our web-based application and it concerns me to have so much hand coding going on just to be able to handle an "in memory" binding (or "manual binding" or ... whatever the best way to describe it is).  I would think this is a very common use case.  I don't want to bind to the database itself until AFTER all the inserts and edits are finalized by the user, then and only then commit the changes.
  6. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 30 Dec 2010 Link to this post

    The missing part in my case was that I was not saving the data source in viewstate or session, between postbacks. Since I was using an in-memory data source, I had to save it somewhere so when the page posted back, I could get the data for rebinding the grid.

    Thanks
    Sunil
  7. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 30 Dec 2010 Link to this post

    Margo,

    I agree with you. I hope automatic inserts, updates and deletes worked for an in-memory data-table so there was no need to write so much code.
    I had to write quite a bit of code for doing this.

    Thanks
    Sunil
  8. Vasssek
    Vasssek avatar
    143 posts
    Member since:
    Aug 2010

    Posted 27 Jan 2011 Link to this post

    Hello,

    for those who want to marshal multiple rows of data from a client application to SQL Server without multiple round trips, here is the link where you can find how to send datatable via sql parameter into stored procedure.

    http://msdn.microsoft.com/en-us/library/bb675163(v=VS.100).aspx

    One disadvantage is that this could be used only in SQL server 2008 version...

    Have a nice day.

    Vasssek
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017