I have created a web server control datagrid in which columns are dynamically
added and dynamically bound to a SQL Server stored procedure. The datagrid
act as an editable cross tab display with products listed vertically down
the left column and distributors displayed horizontally in the header. The
detail displays price that each product is sold by each distributor. Since
the price for a product may be different from one distributor to another,
the cross tab approach was necessary. As new distributors are added, the
system automatically adds the necessary columns. Since the data source
can change from day to day with the addition or removal or distributors,
I have written code that dynamically builds the SQL on every page load and
checks to see if the SQL matches the current stored procedure. If it does
not, then the existing stored procedure is dropped and a new stored procedure
is created. This approach maximizes the performance because the query execution
plan is only created when distributors are either added or removed.
The dynamically added columns use the InstantiateIn method with implements
the ITemplate interface. The following code is used for columns that need
to be dynamically bound to the data source:

AddHandler (controlname).DataBinding, AddressOf (databind method)
container.Controls.Add (controlname)

Columns that donít need to be bound to a data source skip the first line.

All this works great except for the following two issues:
1) In the header of certain dynamically added columns, I have added
a user-editable checkbox which is used to give users control over certain
update processes, turning the process on for specific distributors while
turning it off for others. I canít figure out how to evaluate the checkboxes
state in code because when control returns to the server, the columns no
longer exist until they are re-created. If the checkbox was set ON, how
will the system know to recreate the checkbox with checked property equal
to true? Am I missing something here?
2) Also in the header I use Link Buttons for the header text. This
gives the grid header a button control to be used for sorting. However,
I canít figure out how to dynamically associate an event handler to the link
buttons Click event. Since it is contained in the datagrid, when I click
a linkbutton, the form gives control back to the server, but I canít figure
out how to identify which control fired the event.

Any help would be extremely appreciated.