Skip to content

Infragistics Community Forum / Web / Ultimate UI for ASP.NET Web Forms / Error "Key:id must be unique." with Manual Load On Demand

Error "Key:id must be unique." with Manual Load On Demand

New Discussion
Leland
Leland asked on Dec 16, 2011 5:24 PM

Hello.  Due to the relative complexities of the application I am trying to build, I am going to need to do all of my data binding of the WebHierarchicalDataGrid dynamically.  This is my first time using infragistics and I am finding that to do anything not with the quickmenu or with markup gets a bit finnicky.

 

I have spent lots of time trying to find out what is going wrong here, but i simply cannot find anything…  Here is the relevant code I am using so far:

 

protected void Page_Load(object sender, EventArgs e)

        {

            _Grid.RowIslandsPopulating += new ContainerRowCancelEventHandler(_Grid_RowIslandsPopulating);

            _Grid.InitializeRow += new InitializeRowEventHandler(_Grid_InitializeRow);

            _Grid.PreRender += new EventHandler(_Grid_PreRender);

            LoadGrid();

        }




void _Grid_PreRender(object sender, EventArgs e)

        {

            if (!(IsPostBack))

            {

                foreach (ContainerGridRecord row in _Grid.GridView.Rows)

                {

                    row.IsEmptyParent = true;

                }

            }

        }




        void _Grid_InitializeRow(object sender, RowEventArgs e)

        {

            ((ContainerGridRecord)e.Row).IsEmptyParent = true;

        }







void _Grid_RowIslandsPopulating(object sender, ContainerRowCancelEventArgs e)

        {

            e.Cancel = true;




            Int32 cscpID = (int)e.Row.DataKey[0];




            SqlDataAdapter dA = new SqlDataAdapter(QueryCommand("cscpID:" + cscpID.ToString() + " view:WP"));

            DataSet ds = new DataSet();

            dA.Fill(ds, "CHILD");




            ContainerGrid childGrid = new ContainerGrid();

            e.Row.RowIslands.Add(childGrid);




            childGrid.Key = "CHILD";

            childGrid.Level = 1;

            childGrid.DataKeyFields = "cscpID";

            childGrid.DataSource = ds;

            childGrid.DataBind();

        }







void LoadGrid()

        {

            _Grid.InitialDataBindDepth = 0;

            _Grid.AutoGenerateColumns = false;

            _Grid.AutoGenerateBands = false;

            _Grid.InitialExpandDepth = 0;




            _Grid.Width = 1000;




            _Grid.ExpandCollapseAnimation.SlideOpenDuration = 300;

            _Grid.ExpandCollapseAnimation.SlideCloseDuration = 300;




            InitRootBand(ref _Grid);

            InitChildBand(ref _Grid);

            InitDataViews(ref _Grid, ref _dataSource);

            InitDataRelations(ref _Grid, ref _dataSource);




            _Grid.DataMember = "PARENT";

            _Grid.DataKeyFields = "id";

            _Grid.DataSource = _dataSource;

            _Grid.DataBind();







        }

        private void InitDataRelations(ref WebHierarchicalDataGrid grid, ref WebHierarchicalDataSource src)

        {

            Infragistics.Web.UI.DataSourceControls.DataRelation drMain = new Infragistics.Web.UI.DataSourceControls.DataRelation();




            drMain.ParentDataViewID = "PARENT";

            drMain.ChildDataViewID = "CHILD";

            drMain.ParentColumns = new string[] { "id" };

            drMain.ChildColumns = new string[] { "cscpID" };

            src.DataRelations.Add(drMain);

        }




        private void InitDataViews(ref WebHierarchicalDataGrid grid, ref WebHierarchicalDataSource src)

        {




            Infragistics.Web.UI.DataSourceControls.DataView dvSCP = new Infragistics.Web.UI.DataSourceControls.DataView();

            Infragistics.Web.UI.DataSourceControls.DataView dvWP = new Infragistics.Web.UI.DataSourceControls.DataView();




            SqlDataAdapter dA = new SqlDataAdapter(QueryCommand("county:zavala"));

            DataSet ds = new DataSet();

            dA.Fill(ds, "PARENT");




            dvSCP.ID = "PARENT";

            dvSCP.DataSource = ds;

            dvSCP.DataMember = "PARENT";




            src.DataViews.Add(dvSCP);




            SqlDataAdapter childdA = new SqlDataAdapter(QueryCommand("county:zavala view:wp"));

            DataSet childds = new DataSet();

            dA.Fill(childds, "CHILD");




            dvWP.ID = "CHILD";

            dvWP.DataSource = childds;

            dvWP.DataMember = "CHILD";




            src.DataViews.Add(dvWP);

        }




        private void InitRootBand(ref WebHierarchicalDataGrid grid)

        {

            grid.DataMember = "PARENT";

            grid.DataKeyFields = "id";




            grid.Columns.Add(newColumn("id", "ID", "id", null, false));

            grid.Columns.Add(newColumn("State", "State", "p_State", null, false));

        }




        private void InitChildBand(ref WebHierarchicalDataGrid grid)

        {

            Band childBand = new Band();

            grid.Bands.Add(childBand);

            childBand.AutoGenerateColumns = false;




            childBand.Key = "CHILD";

            childBand.DataMember = "CHILD";

            childBand.DataKeyFields = "cscpID";




            childBand.Columns.Add(newColumn("ID", "ID", "ID", null, false));

            childBand.Columns.Add(newColumn("cscpID", "cscpID", "cscpID", null, false));

            childBand.Columns.Add(newColumn("State", "State", "c_State", null, false));

        }

 

 

ASPX Code:

    <form id="form1" runat="server">

    <asp:ScriptManager ID="_scriptMan" runat="server">

    </asp:ScriptManager>

    <div>

        <ig:WebHierarchicalDataGrid ID="_Grid" runat="server" >

        </ig:WebHierarchicalDataGrid>

        <ig:WebHierarchicalDataSource ID="_dataSource" runat="server">

        </ig:WebHierarchicalDataSource>




    </div>

    </form>

 

 

 

The parent grid binds and loads successfully, and i see the data, but when I click on the arrow to expand a row, it gives me a popup error with a [NotSupportedException] and the message

“Key:id must be unique.  The field objects in the grid must have non-empty unique keys assigned to the Key property”

 

please help!!!  I don’t know what i can do if i don’t get this working…

in case you are wondering, the data is being pulled via stored procedure.  the “id” field in both parent and child tables is definitely unique, and the cscpID field is a foreign key to “id”  between the parent and child tables.

 

 

 

Sign In to post a reply

Replies

  • 0
    [Infragistics] Nikola Genov
    [Infragistics] Nikola Genov answered on Dec 10, 2010 9:25 AM

    Hi,

    Try to call ClearDataSource in the LoadGrid method:

     _Grid.GridView.ClearDataSource();

    • 0
      Leland
      Leland answered on Dec 10, 2010 10:34 AM

      in the beginning or at the end?

    • 0
      Leland
      Leland answered on Dec 10, 2010 10:42 AM

      hmm…  I tried adding it wherever made sense…  same issue occurred.

      thanks for the help though!  any other ideas?

      • 0
        Mark
        Mark answered on Oct 3, 2011 2:57 PM

        Any update on this?  I may be having the same issue.  I'm loading a WebHierarchicalDataSource dynamically and everything works fine until I execute a second postback.  Then I get the same error you were getting.

      • 0
        Steve Robinson
        Steve Robinson answered on Dec 16, 2011 5:24 PM

        Hi all,

        I was having the same problem as mflach.  I resolved the problem by calling the grid.columns.clear() method prior to databinding.

        Hope this helps.

        Steve

         

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Leland
Favorites
0
Replies
5
Created On
Dec 16, 2011
Last Post
14 years, 2 months ago

Suggested Discussions

Created by

Created on

Dec 16, 2011 5:24 PM

Last activity on

Feb 24, 2026 7:46 AM