dataset manipulation

i have one datatable in dataset .datatable has columns with data.i dont have any primary key column.i want to add one column in this datatable name as ID.which should get increament based on rows insertions in datatable.it shoud increment by one.and it should have unique value.like 1 ,2  3, so on.i want to do all this on dataset level only.kindly tell me how to do?
ajay1febAsked:
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.

p_davisCommented:
DataColumn[] pkCol = {dataSet.Tables["dataTableName"].Columns["columnName"]};

dataSet.Tables["dataTableName"].PrimaryKey = pkCol;
dataSet.Tables["dataTableName"].Columns["columnName"].AutoIncrement = true;
0
patrickrivaCommented:
This could do.....
DataSet ds = new DataSet();
			ds.Tables.Add(new DataTable());
 
			ds.Tables[0].Columns.Add(new DataColumn("ID", typeof (int)));
			for ( int i = 0; i < ds.Tables[0].Rows.Count; i++ )
				ds.Tables[0].Rows[i]["ID"] = i;

Open in new window

0
ajay1febAuthor Commented:
dera patrickriva,

   solution is nice but one problem is here.if i have 10 rows in rowset then it is giving id like 0 to 9.its not starting from 1.its should start from 1 and it should end to 10 if rowset has 10 rows.waiting for ur reply.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

patrickrivaCommented:
just change the following line with a + 1

                             
ds.Tables[0].Rows[i]["ID"] = i + 1;

Open in new window

0
ajay1febAuthor Commented:
dear patrickriva,

now   i want to make this ID column as primary key.
0
patrickrivaCommented:
here ...
ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns["ID"] };

Open in new window

0
ajay1febAuthor Commented:
it is not working.following message is displaying.
These columns don't currently have unique values.
now is like following code.now it is not showing any exception.
 strItems = "SELECT GRM.group,GRM.productline,IM.productType,IM.CatalogNumber,IM.Description,PR.RecommededUnits,CLP.CurrentListPrice from GroupLineMapping GRM,ItemMaster IM,  ProductLineConfiguration PR, ConsignmentListPrice CLP WHERE GRM.productline=PR.productline AND PR.CatalogNumber=IM.CatalogNumber AND  IM.CatalogNumber=CLP.CatalogNumber AND GRM.group='" + comboGroup.SelectedItem.Text + "'" + "AND PR.productline='" + comboPoductLine.SelectedItem.Text + "'";

        DataSet dsItems = new DataSet();
        dsItems = AccessDB.DBDataSet(strItems);
         
        dsItems.Tables.Add(new DataTable());
       
        dsItems.Tables[0].Columns.Add(new DataColumn("ItemID", typeof(int)));
        //dsItems.Tables[0].PrimaryKey = new DataColumn[] { dsItems.Tables[0].Columns["ItemID"] };
     

        for (int i = 0; i < dsItems.Tables[0].Rows.Count; i++)
            dsItems.Tables[0].Rows[i]["ItemID"] = i+1;
            GVQuote.DataSource = dsItems;
            dsItems.Tables[0].PrimaryKey = new DataColumn[] { dsItems.Tables[0].Columns["ItemID"] };





0
patrickrivaCommented:
Looking at your indentation I think you need to put the last two lines outside the for loop....
0

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
p_davisCommented:
ajay1feb -- did you even try my solution suggestion. it uses the datasets native properties.-- also there is a autoincrement seed to start out with the id number that you want.

also i already told you how to add ad the column as the primary key
0
ajay1febAuthor Commented:
dear davis,

   i tried yours suggestion but it didn't work in my scenario.but patrickriva's suggestion worked.
Regards,
Ajay
0
p_davisCommented:
if you have time i am interested to know what, exactly, didn't work.
0
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
.NET Programming

From novice to tech pro — start learning today.