• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6499
  • Last Modified:

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!

  • 3
  • 2
1 Solution
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?
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

aspwvAuthor Commented:
Very good...right on the money.  Thanks for your help
aspwvAuthor Commented:

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now