Try Now
More in this section

Forums / Developing with Sitefinity / Storing DataTable in ViewState

Storing DataTable in ViewState

2 posts, 0 answered
  1. Al
    Al avatar
    33 posts
    28 Dec 2008
    05 Aug 2010
    Link to this post
    Hi all,

    This question isnt necessarily related to Sitefinity, although it is occuring whilst i'm building a WebUITypeEditor so hopefully you can help.

    I have a scenario where I have two RadGrids. When a row is selected in Grid 1, that row is copied to a datatable and then the datasource of Grid 2 is set. When I run the code I get the error:

    Exception Details: System.ArgumentException: This row already belongs to another table.
    Source Error:
    Line 211:           dr["DocumentName"] = oItem["DocumentName"].Text;
    Line 212:           dr["DocumentExtension"] = oItem["DocumentExtension"].Text;
    Line 213:           this.oDataTable.Rows.Add(dr);
    Line 214:
    Line 215:       }

    My code is :

    public class DynamicDocumentSelectorWebUITypeEditor : Telerik.Cms.Web.UI.WebUITypeEditor<string>
        public DynamicDocumentSelectorWebUITypeEditor()
            // TODO: Add constructor logic here
        private System.Data.DataTable _oDataTable;
        public System.Data.DataTable getTable() {
            System.Data.DataTable oDataTable = new System.Data.DataTable();
            oDataTable.Columns.Add(new System.Data.DataColumn("DocumentID", typeof(string)));
            oDataTable.Columns.Add(new System.Data.DataColumn("DocumentName", typeof(string)));
            oDataTable.Columns.Add(new System.Data.DataColumn("DocumentExtension", typeof(string)));
            return oDataTable;
        public System.Data.DataTable oDataTable {
            get {
                object o = this.ViewState["DataTable"];
                if(o == null) {
                    return getTable();
                return (System.Data.DataTable)o;
            set {
                this._oDataTable = value;
                this.ViewState["DataTable"] = value;
    //when a row in Grid 1 is selected the following code will be executed
        protected void GridDocumentsInLibrary_SelectedIndexChanged(object sender, EventArgs e) {
            //loop through each selected row
            foreach (Telerik.Web.UI.GridDataItem oItem in GridDocumentsInLibrary.SelectedItems) {
                System.Data.DataRow dr = this.oDataTable.NewRow();
                dr["DocumentID"] = oItem["DocumentID"].Text;
                dr["DocumentName"] = oItem["DocumentName"].Text;
                dr["DocumentExtension"] = oItem["DocumentExtension"].Text;
            //set datasource of second grid
            GridSelectedDocuments.DataSource = this.oDataTable;

    Can anyone help me figure out why this happening?

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    25 Oct 2016
    10 Aug 2010
    Link to this post
    Hi Al,

    You could try to clone the database by using Clone(); and then use removeat or/and ImportRow method.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered