I am binding a dynamically built dataset to an ASPxGridView and need help with setting the primary key

I have to query across several datasources so I query those various places and retrieve bits of information and build a "hand-made" dataset on the fly in my ASP.Net page's code behind.  I am using DevExpress' ASPxGridView.  After I create my dataset, I bind to it with my ASPxGridView and it shows my data.  

I created the GridView columns manually at design time and not bound to a datasource (since I build my own on the fly).  I am trying to use the selection features but I keep getting this error:

Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 4/5/2010 4:33:53 PM
Event time (UTC): 4/5/2010 8:33:53 PM
Event ID: 5139394830804561854bbf78bc97270a
Event sequence: 21
Event occurrence: 2
Event detail code: 0

Exception information:
    Exception type: MissingPrimaryKeyException
    Exception message: Missing primary key.

I get that I must have to set the primary key somewhere and I have tried setting it where I do the bind...

        DataSet MyDS = new DataSet();
        DataTable MyTable = MyDS.Tables.Add();

       MyTable.Columns.Add("Name", typeof(string));
       MyTable.Columns.Add("Region", typeof(string));

        DataColumn[] keys = new DataColumn[1];
        DataColumn column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "ID";
        keys[0] = column;
        MyTable.PrimaryKey = keys;

I add several rows rows...no problems
Then I bind:
MyASPxGridView.DataSource = MyDS.Tables[0];
MyASPxGridView.KeyFieldName = "ID";

All this works fine and I can see my data as I expect.  However, when I select a bunch of rows and do a postback, I get the error above.  I am not sure where I am going wrong or if I can do what I am trying here...any thoughts would be appreciated!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HainKurtSr. System AnalystCommented:
as error message says, you should a primary key to the table
do you have any such key on underlying tales?
HainKurtSr. System AnalystCommented:
what is your db? open the table and add a primary key on field ID of the table...
aspwvAuthor Commented:
I am not drawing data from one table or database.  I pull from several sources and build my own dataset in code.  I was hoping that this line:

MyTable.PrimaryKey = keys;

would set a primary key on my "created" table.  Otherwise I have no single table in a database on which to add a primary key.  Does that make sense?
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Todd GerbertIT ConsultantCommented:
This uses an auto-incrementing ID column, you do not need to insert it's value.

DataTable table = new DataTable();
DataColumn idColumn = table.Columns.Add("ID");
idColumn.AllowDBNull = false;
idColumn.AutoIncrement = true;
idColumn.AutoIncrementSeed = 1;
idColumn.AutoIncrementStep = 1;
idColumn.Unique = true;
table.PrimaryKey = new DataColumn[] { idColumn };

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aspwvAuthor Commented:
Very good...right on the money.  Thanks for your help
aspwvAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.