Hi, you could do this by having handlers for DataGrid's ItemCreated and ItemDataBound events. In ItemDataBound you place code to inspect if button should be created (based on data) and initially place the button on the Cell's Controls. Then you'd need to put
into ViewState (or somewhere) the index of the row, index of the cell to indicate that button should be recreated there (for this row and for this cell). On ItemDataCreated when e.Item.DataItem is nothing or null (ItemCreated will be called before ItemdataBound
on the binding request and also on postback when grid is restored from ViewState) you would recreate the button control (based on current row's and cell's index) if the flag exists in ViewState. You probably wonder that if you can check in ItemCreated for
e.Item.DataItem being null or nothing, that you'd place all the logic into ItemCreated. However, there's the catch that in ItemCreated, DataGridItem is not yet added to the Items collection (DataGridItem is also a control) e.g Controls and it haven't yet started
the control lifecycle (to track viewstate). It means that child controls you add to it, won't persist properties to ViewState (properties which you'd set in ItemCreated). This just means that if you have settings for the Button which should persist over postbacks,
such would be set in ItemdataBound because then viewstate tracking is already on. So the logic is two-sided. a) On databinding, in ItemdataBound manage the initial creation of the control (property setting) e.g setting the flag to indicate that control was
created on this row and on that cell b) In ItemCreated, based on flag decide to recreate the control instance (properties are restored from ViewState when lifecycle advances to the step that DataGridItem gets added to Items collection)
joteke
All-Star
46224 Points
6896 Posts
ASPInsiders
MVP
Re: DatagridL Adding Buttons to Cells Dynamically: Attempt 2!
Aug 24, 2004 12:33 PM|LINK
Teemu Keiski
Finland, EU