Q: Adding a column to a table

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hi

Can anybody help me with the following, hopefully simple, question?

I have a table which I've connected to a dataset. I wish to add a new column
to the beginning of the table
and to fill it with incremental values e.g. if the tables looks
like this:

23 56
45 87
21 67
34 09

I'd like it to be changed into:

1 23 56
2 45 87
3 21 67
4 34 09

In fact, what I'm trying to do is to give the table an index key in the
first column. However, I only want the table of the dataset to have this new
column i.e. not the original table which the connection was made to.

Can anybody help?

Thanks in advance

Geoff
 
Hi Geoff,

Please do not multipost, you got an answer in the VBDotnet newsgroup.

Nobody sees it, when you crosspost (one messages in one time to more
newsgroups) everybody can act on the answer from OHM.

Cor
 
What you need is:

- Make a new DataColumn whith option AutoIncrement = True,
- Add this column to the DataSet
- Fill the DataSet

In VB.NET:

Dim newColumn As New DataColumn
newColumn.AutoIncrement = True
newColumn.DataType = GetType(System.Int32)
myDataSet.Tables("tablename")Columns.Add(newColumn)
mySqlDataAdapter.Fill(myDataSet)


In C# it's similar...


Manuel Llavador
 
Geoff Jones said:
Hi

Can anybody help me with the following, hopefully simple, question?

I have a table which I've connected to a dataset. I wish to add a new column
to the beginning of the table
and to fill it with incremental values e.g. if the tables looks
like this:

23 56
45 87
21 67
34 09

I'd like it to be changed into:

1 23 56
2 45 87
3 21 67
4 34 09

In fact, what I'm trying to do is to give the table an index key in the
first column. However, I only want the table of the dataset to have this new
column i.e. not the original table which the connection was made to.

Dim dc As New DataColumn("ColumnNameHere",
System.Type.GetType("System.String"))

Dim Counter As Integer = 0

dc.AutoIncrement = True

dc.AutoIncrementSeed = Counter 'Do this so you can seed it at some point
other than 0 if need be

dt.Columns.Add(dc)

I'm not sure what you mean 'this table' vs the orignal one, but it wouldn't
matter much on either table b/c if you are binding it to a grid and don't
want it to show, you can just not set the columnmapping. IT won't affect
the update either way unless you specify a column mapping and set your
update logic to reflect it.

I may have misunderstood your questoin though and If I did, please let me
know and I'll see what I can do.
Can anybody help?

Thanks in advance

Geoff

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 
Back
Top