In the web part for Sharepoint, I am trying to add a variable number / ordering of ButtonColumns to the DataGrid dynamically based on the parameter that the user selected. The problem is that dynamic columns do not disable events that I configured on the DataGrid (e.g. SelectedIndexChanged). When the table originally contained a static set of columns, they were created in CreateChildControls (), and everything worked peach. However, since they are now dynamic, I have to postpone their addition until an event occurs when the search button is clicked. I was wondering if there was some place that I needed to move the column creation so that it still allowed it to be dynamic, but also allowed it to register / fire events.
outputDG creation in CreateChildControls ():
outputDG = new System.Web.UI.WebControls.DataGrid(); outputDG.CellPadding = 4; outputDG.HeaderStyle.Font.Bold = false; outputDG.HeaderStyle.Font.Name = "Verdana"; outputDG.HeaderStyle.BackColor = Color.FromArgb(242,242,242); outputDG.HeaderStyle.ForeColor = Color.FromArgb(128,128,128); outputDG.HeaderStyle.Wrap = false; //outputDG.ItemStyle.BorderColor = Color.Navy; outputDG.HorizontalAlign = HorizontalAlign.Left; //outputDG.BorderWidth = 1; outputDG.GridLines = GridLines.Horizontal; outputDG.Width = propsMgr.SearchGridWidth; outputDG.PageSize = 10; outputDG.AllowPaging = true; outputDG.PagerStyle.Mode = PagerMode.NumericPages; outputDG.PagerStyle.PageButtonCount = 5; outputDG.PagerStyle.NextPageText = "Next Page"; outputDG.PagerStyle.PrevPageText = "Previous Page"; outputDG.PagerStyle.Visible = true; outputDG.PageIndexChanged += new DataGridPageChangedEventHandler(this.outputDG_PageIndexChanged); outputDG.AllowSorting = false; outputDG.SortCommand += new DataGridSortCommandEventHandler(this.outputDG_SortCommand); outputDG.SelectedItemStyle.BackColor = Color.FromArgb(255,244,206); outputDG.SelectedIndexChanged += new EventHandler(this.outputDG_SelectedIndexChanged); outputDG.ItemCreated += new DataGridItemEventHandler(this.outputDG_ItemCreated); outputDG.AutoGenerateColumns = false; outputDG.ItemCommand += new DataGridCommandEventHandler(outputDG_ItemCommand); Controls.Add(outputDG);
During the search button, click the event:
ButtonColumn buttonColumnSelect = new ButtonColumn(); buttonColumnSelect.ButtonType = ButtonColumnType.LinkButton; buttonColumnSelect.CommandName = "Select"; buttonColumnSelect.HeaderText = "Column"; buttonColumnSelect.DataTextField = "columnField"; outputDG.Columns.Add(buttonColumnSelect);
And then later this same event goes through a set of results and adds data to my rows. As I mentioned, all this worked when the ButtomColumn code appeared in CreateChildControls (), but it stopped working as soon as it was transferred to the event. My best guess is that the event for the column does not have the ability to register in order to fire, as this comes from another event. If I need to solve this problem by creating a DataGrid in different ways, I definitely wish; I just need to be able to dynamically specify different columns to use.
Adam source share