The code below creates and fills columns in a datagrid dynamically.
My problem occurs when trying to retrieve the text value in the second column. When using the code below it generates an error stating that the column does not exist. I know this code works because its been used before with the only difference being that the columns were created in design mode.
Any help would be greatly appreciated.
Code:
With Me.dgrdResults
btnCol = New ButtonColumn
btnCol.ButtonType = ButtonColumnType.LinkButton
btnCol.Text = "Select"
btnCol.CommandName = "Select"
Me.dgrdResults.Columns.Add(btnCol)
bCol = New BoundColumn
bCol.DataField = Me._ds.Tables(0).Columns(0).ColumnName
bCol.HeaderText = "ID"
Me.dgrdResults.Columns.Add(bCol)
bCol = New BoundColumn
bCol.DataField = Me._ds.Tables(0).Columns(1).ColumnName
bCol.HeaderText = "Name"
Me.dgrdResults.Columns.Add(bCol)
.DataSource = Me._ds
.DataBind()
End With
My problem occurs when trying to retrieve the text value in the second column. When using the code below it generates an error stating that the column does not exist. I know this code works because its been used before with the only difference being that the columns were created in design mode.
Code:
Me.dgrdResults.Items(e.Item.ItemIndex).Cells(1).Text
Any help would be greatly appreciated.