Adding a fresh column to a datagrid

M

Mike Fellows

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
 
K

Ken Tucker [MVP]

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
 
K

keyur shah

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top