Solved

DataTable  Increment

Posted on 2011-03-08
1
434 Views
Last Modified: 2012-05-11
how do i adding identity column to datatable?

i want it to Increment by 1 each time i enter a row in.


here is the sample code that i have coded in c#:

        DataTable dt = new DataTable();

        dt.Columns.Add("DomainNames");
        dt.Columns.Add("Status");
        dt.Columns.Add("Delete");
        dt.Columns.Add("Enabled");
        dt.Columns.Add("Index");


This creates columns

i want it to auto Increment by 1 each time i enter a row in.

0
Comment
Question by:JCWEBHOST
1 Comment
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 35068164
To add auto increment column in the DataTable, we can set the AutoIncrement property of the DataColumn object as true and specify the seed value after adding that field into the DataTable object.


// create columns for the DataTable
DataTable dTable =  new DataTable();
DataColumn auto = new DataColumn("AutoID", typeof(System.Int32));
dTable.Columns.Add(auto);
// specify it as auto increment field
auto.AutoIncrement = true;
auto.AutoIncrementSeed = 1;
auto.ReadOnly = true;


Source: http://www.dotnetfunda.com/interview/exam545-how-to-add-auto-increment-column-in-the-datatable-.aspx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now