By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

Adding a fresh column to a datagrid

P: n/a
I want to add a fresh column to a datagrid that is bound to a datasource

i want it to be the first column on the datagrid so that i can number each
row 1,2,3,4.... etc. (but i have no need to store this within my database)

how do i add an empty column?
Thanks
Mike Fellows

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mike,

You need to add a datacolumn to the datatable you are showing in the
datagrid. Add a tablestyle to the datagrid to adjust the order the columns
are displayed.

To add a column to data table
Dim dcNew As New DataColumn("Numbered", GetType(Integer))

daCustomer.Fill(ds, "Clients")

ds.Tables("Clients").Columns.Add(dcNew)

Dim x As Integer

For x = 0 To ds.Tables("Clients").Rows.Count - 1

Dim dr As DataRow = ds.Tables("Clients").Rows(x)

dr.BeginEdit()

dr.Item("Numbered") = x

dr.EndEdit()

Next

SetupData()

To add a table style

Private Sub SetupData()

Dim ts As New DataGridTableStyle

ts.MappingName = "Clients"

Dim col As New DataGridTextBoxColumn

With col

..MappingName = "Numbered"

..HeaderText = "Numbered"

..Width = 50

End With

Dim colPhone As New DataGridTextBoxColumn

With colPhone

..MappingName = "PhoneNumber"

..HeaderText = "Phone Number"

..Width = 200

End With

Dim colName As New DataGridTextBoxColumn

With colName

..MappingName = "LastName"

..HeaderText = "Name"

..Width = 250

End With

ts.GridColumnStyles.Add(col)

ts.GridColumnStyles.Add(colPhone)

ts.GridColumnStyles.Add(colName)

DataGrid1.TableStyles.Add(ts)

ts = Nothing

colPhone = Nothing

colName = Nothing

End Sub

Ken
-----------------
"Mike Fellows" <mi*************@equityhouse.co.uk.SPAM> wrote in message
news:RX***************@newsfep3-gui.server.ntli.net...
I want to add a fresh column to a datagrid that is bound to a datasource

i want it to be the first column on the datagrid so that i can number each
row 1,2,3,4.... etc. (but i have no need to store this within my database)

how do i add an empty column?
Thanks
Mike Fellows

Nov 20 '05 #2

P: n/a
You need to do this thing programmatically.. I mean as runtime.

You need to write simple DATAGRID code to add a column, and compute
serial number by incrementing a loop counter and inserting that value in
the CELL for that newly added column.

Hope this makes sense. I am in rush, so cannot write code... rightnow...
but later if u need let me know.

Keyur Shah
Verizon Communications
732-423-0745

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.