Datagrid Dynamic Columns Postback and SelectedIndexChanged problems

G

George

All,

I have a datagrid that has a few design time columns (two buttons -
one for select and one for delete) and the programatically I add
several other databound columns based on which information the user
would like to view. I did encounter the intial problem where when the
select button was selected, all the dynamically added columns in the
grid were lost. To get around this I found some articles which
indicated I needed to re-add the dynamic columns every time (a real
pain since I read them from the database and if anyone know how to
resolve this is a better I would appreciate it also) in the Page_Init.
It was explained in one article that the merging back up on the
server side between the view state and controls happens between the
Page_Init and the Page_Load event, thus the columns that were added
are not maintaned in the view state AND all the dynamic columns need
to be added back to the grid for the viewstate to match up the values
again. SO... I got all the working and now in the data and columns
persist between trips to the server.

The problem now is that.. when I click on the select button which
should trigger the SelectedIndexChanged event of the grid, the page
init (where I re-add all the columns) and load fire but the
SelectedIndexChanged event never fires. Anyone have any suggestions?

Code Samples below:

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
Dim lb_Succeeded As Boolean
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()

txtID.Text = Request.QueryString("ID")
lb_Succeeded = True

If OpenDatabaseConnection() = True Then
If AddHeaderAttributes() = True Then
If SetupPage() = True Then
RefreshGrid()
End If
End If
End If

End Sub

(NOTE - I have tried this with the RefreshGrid in the Load event and
it made no difference.)


Private Function SetupPage() As Boolean
Dim lobj_DataGridColumn As BoundColumn
Dim lobj_DataReader As OleDbDataReader
Dim lobj_Command As New OleDbCommand
Dim ls_Column_Type As String
Dim li_Column_Length As Integer
Dim ls_Parameter_Name As String

Try
SetupPage = False

'Add the parameter
lobj_Command.Parameters.Add("pStandardValueTable",
OleDbType.VarChar).Value = lblHeader.Attributes("_Table_Name")
If iobj_DatabaseTools.ReturnDataReader(lobj_DataReader,
"Get_Standard_Value_Fields_Single", CommandType.StoredProcedure,
is_ReturnMessage, lobj_Command, False) = True Then
'We should only get one row but put this in a loop
just to be safe

While lobj_DataReader.Read = True
'Add the columns to the grid
lobj_DataGridColumn = New BoundColumn
If IsDBNull(lobj_DataReader.Item("Header_Text")) =
False Then
lobj_DataGridColumn.HeaderText =
CType(lobj_DataReader.Item("Header_Text"), String)
End If

lobj_DataGridColumn.Visible =
CType(lobj_DataReader.Item("Visible"), Boolean)
lobj_DataGridColumn.DataField =
CType(lobj_DataReader.Item("Database_Field_Name"), String)

If IsDBNull(lobj_DataReader.Item("Column_Type")) =
False Then
ls_Column_Type =
UCase(CType(lobj_DataReader.Item("Column_Type"), String))
End If

If IsDBNull(lobj_DataReader.Item("Column_Length"))
= False Then
li_Column_Length =
UCase(CType(lobj_DataReader.Item("Column_Length"), Integer))
Else
li_Column_Length = 0
End If

If
IsDBNull(lobj_DataReader.Item("Parameter_Name")) = False Then
ls_Parameter_Name =
UCase(CType(lobj_DataReader.Item("Parameter_Name"), String))
End If

dgInfo.Columns.Add(lobj_DataGridColumn)

If SetUpRow(lobj_DataGridColumn.HeaderText,
ls_Column_Type, li_Column_Length, ls_Parameter_Name) = False Then
Exit Try
End If

End While
Else
lblError.Text = is_ReturnMessage
Exit Try
End If

SetupPage = True


Catch ex As Exception
lblError.Text = "The following error occurred in Function
AddHeaderAttributes: " & ex.Message
Finally

If Not lobj_DataReader Is Nothing Then
If lobj_DataReader.IsClosed = False Then
lobj_DataReader.Close()
End If
lobj_DataReader = Nothing
End If

If Not lobj_Command Is Nothing Then
lobj_Command.Dispose()
lobj_Command = Nothing
End If
End Try
End Function



Thanks
George
 
G

George

All,

OK.. I was just working too late. The reason the
SelectedIndexChanged event was not firing was because I had copied the
method SelectedIndexChanged from another page which had the grid
control named differently. Once I fixed the name on the event
procudure, it all worked fine.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top