Hi,
I have a WHDG with a 3 level (tables) dataset, Autogenerate columns true. My grid displays fine, 3 band, columns and data are visible. Now I would like to format columns (hide, color, etc...) after binding them.Tried these in DataBound or simply just after calling DataBind()
grid3.GridView.Columns["Check"].Header.Text = "";grid3.Columns["Check"].Header.Text = "";
but my Columns collection is always empty. Why is that?
Thank you
Update: I realized I have to set AutoGenerateColumns to false but cannot figure out in which event. InitializeBand..?
Hi onlien,
Thank you for posting in our forum.
You can set the AutoGenerateColumns from the markup or in code-behind in InitializeBand event. If it is set to false, you should have the columns added in the aspx. To access the columns of child band, you should use the following syntax:
WebHierarchicalDataGrid1.GridView.Band.Bands[0].Columns["ID"].Hidden = true;
The GridView.Band is the top-level band of the grid.
Let me know if you have any questions regarding this matter.
Hi Nikolay,
Thank you for your answer, sorry, I wasn't clear enough. I have set AutoGenerateColums to false earlier from aspx but it only applied to the root band. My question was about the child and grandchild bands which still stayed AutoGen true and I wanted to add their columns manually, too, so that I can format and hide them after binding.
Finally I managed it somehow: Set child and grandchild bands' AutoGenerateColums false in InitializeBand and immediately after added my columns
protected void WHDG_InitializeBand(object sender, Infragistics.Web.UI.GridControls.BandEventArgs e){ switch (e.Band.BandIndexAddress) { case "0": // root band // AGC already false, just add columns, set datakeyfield break; case "0_0": // child band: e.Band.AutoGenerateColumns = false; // checked with debug: it's originally true, even if I set it false on the grid, why? // add columns, set datakeyfield case "0_0_0": // grandchild band: e.Band.AutoGenerateColumns = false; // add columns, set datakeyfield }
Seems working so far but is it the recommended way to do it? I tried RowIslandsPopulating too but that way I couldn't reach the grandchild band, only child band.
Hello onlien,
I would suggest you to use the InitializeBand event for this kind of operations.
Feel free to contact me if you have any further questions.
Now I have problems with a command button. I implemented a template column with an imagebutton (after this article : http://help.infragistics.com/Help/NetAdvantage/ASPNET/2011.2/CLR4.0/html/WebDataGrid_Using_Item_Template.html)
In InitializeBand I have:
tdf = new TemplateDataField();tdf.Key = "Restart";
(e.Band.Columns.FromKey("Restart") as TemplateDataField).ItemTemplate = new RestartItemTemplate();
I bind the data on every page load so the above code should run and re-create my button every time.Still, the button is only displayed when I initially call the page. After postback it is gone and OnItemCommand is never firing (I added OnItemCommand="Chain_OnItemCommand" in aspx).
Q1: Why is it not firing, when should I re-create the template?
Q2: How do I give a commandArgument? Template class:
public class RestartItemTemplate : ITemplate { public void InstantiateIn(Control container) { ImageButton btnRestart = new ImageButton(); btnRestart.ImageUrl = "~/images/smallicons/delete16.png"; btnRestart.CommandName = "Restart"; btnRestart.CommandArgument = " # I need my ID here - cannot access in initializeBand! #"; container.Controls.Add(btnRestart); } }
It works from aspx but I need it from code.
<ig:TemplateDataField Key="Restart" > <ItemTemplate > <asp:ImageButton ID="btnRestart" runat="server" ImageUrl="~/restart16.gif" CommandArgument='<%# Eval("Id") %>' CommandName="Restart" /> </ItemTemplate> </ig:TemplateDataField>
Thanks for your reply. I'm glad you solved your issue.
Let me know if you have any other questions.
Finally I added itemtemplate in InitializeBand:(e.Band.Columns.FromKey("Restart") as TemplateDataField).
ItemTemplate = new RestartItemTemplate();I was struggling some more with adding commandArgument in InitializeRow but this one worked for me after I realized I forgot to set the ID property to "btnRestart" of my imagebutton in InstantiateIn: ImageButton btnRestart = e.Row.Items[0].FindControl("btnRestart") as ImageButton; // (if button is in 1st column)btnRestart.CommandArgument = e.Row.Items.FindItemByKey("Id").Value.ToString(); Thanks again for your help--
Hi online,
I would suggest you to apply the ItemTemplate to your column on page load:
TemplateDataField templateColumn1 = (TemplateDataField)WebDataGrid1.Columns["TemplateColumn1"];
templateColumn1.ItemTemplate = new RestartItemTemplate();
You can set the CommandArgument using the following syntax:
btnRestart.CommandArgument = ((DataRowView)((TemplateContainer)container).DataItem)["ID"].ToString();
Please refer to the attached sample and let me know if you have any questions.