Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Dynamic Datagrid Columns Causing Problems

Status
Not open for further replies.

chadau

Programmer
Joined
Nov 14, 2002
Messages
155
Location
US
The code below creates and fills columns in a datagrid dynamically.

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.
 
since the columns are created dynamically, they are not recreated automatically again upon postback. also keep in mind that the event that adds the postback data to the controls in the page is fired before PageLoad and after PageInit. meaning that you should create the columns in the PageInit so that upon postback, when ASP.NET tries to add the post data back to the columns, they are created. Otherwise, it will not find them and will abandon the data.

hope this is clear enough, if not, post back for details.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top