Hi all!
I wrote a usercontrol that contains a Tabcontrol. I need to dynamically add tabs and nested grids in the according tabpagecontrols. I managed to do that, but i have the problem to retrieve or alter the displaylayout settings after the datasource was assigned to the grid. If I do not try to manke changes to the displaylayout in the usercontrol and the control is shown on the form it works fine, but i need to change displaylayout settings. Where is the error in code, means, when can i set the displaylayout setting?
--code:
//Getting Data for tabs
DST = Connect.GetDataSet(strSQLQuery);
//clearing all tabs
this.ultraTabControl1.Tabs.Clear();
GridsToFormat = new Infragistics.Win.UltraWinGrid.UltraGrid[DST.Tables[0].Rows.Count];
int i = 0;
//Create Tab for each row
foreach (System.Data.DataRow Rwo in DST.Tables[0].Rows)
{
Infragistics.Win.UltraWinTabControl.UltraTab Tab = new Infragistics.Win.UltraWinTabControl.UltraTab();
Infragistics.Win.UltraWinGrid.UltraGrid StockGrid = new Infragistics.Win.UltraWinGrid.UltraGrid();
Infragistics.Win.UltraWinTabControl.UltraTabPageControl TabPageControl = new Infragistics.Win.UltraWinTabControl.UltraTabPageControl();
((System.ComponentModel.ISupportInitialize)(StockGrid)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.ultraTabControl1)).BeginInit();
this.ultraTabControl1.SuspendLayout();
TabPageControl.SuspendLayout();
this.SuspendLayout();
this.ultraTabControl1.Controls.Add(TabPageControl);
Tab.TabPage = TabPageControl;
Tab.Key = System.Convert.ToString(Rwo["MST_Bezeichnung"]);
Tab.Text = System.Convert.ToString(Rwo["MST_Bezeichnung"]);
this.ultraTabControl1.Tabs.Add(Tab);
TabPageControl.Controls.Add(StockGrid);
this.intMedikament_ID = System.Convert.ToInt32(Rwo["LAG_Medicament_ID"]);
TabPageControl.Controls.Add(this.grdDosageHistory);
TabPageControl.Location = new System.Drawing.Point(1, 23);
TabPageControl.Name = "ultraTabPageControl1";
TabPageControl.Size = new System.Drawing.Size(389, 327);
TabPageControl.Controls[0].Dock = DockStyle.Fill;
((System.ComponentModel.ISupportInitialize)(StockGrid)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.ultraTabControl1)).EndInit();
this.ultraTabControl1.ResumeLayout(false);
TabPageControl.ResumeLayout(false);
this.ResumeLayout(false);
//Fetching Dataset for Grid and assign datasource
FillValues(StockGrid);
//Formatting the grid - here the error occurs (Displaylayout is null)
FormatGrid(StockGrid);
if (Convert.ToInt32(Rwo["LAG_Medicament_ID"]) == this.intActiveMedikamentID)
this._ActiveStockGrid = StockGrid;
}
GridsToFormat[i] = StockGrid;
i++;
Please suggest, what i can do... Leaving out the funtion (FormatGrid(...) works, but displaylayout looks ugly...
Thank, Marc!
It's pretty hard to read this code, and I'm not sure exactly what problem you are having. What exactly is not working here?
The best way to modify the layout of the grid is to use the InitializeLayout event.
If you need to apply appearances or calculations to individual rows, then you should use the InitializeRow event instead of looping through all of the rows.
usually, when I assigne the datasource to a grid , i can imediately change the displaylayout like header.caption ord hide columns and so on.
I the code i like to add tabs to a tabcontrol based on database values. For each row a new tab should be generated, that contains a grid with special data according to the Tab. Therefor I supsend the layout to add a tab and the gridcontrol to the tabpage and after that i resume the layout. But after resuming layout i do not have a displaylayout yet, although i already assigned the datasource to the grid, that means i raise an errormessage, when trying to set displaylayouts header.caption for example.
If iI do not try to change the display layout and the usercontrol is shown, it shows the grids with the correct content, but the displaylayout is not as i wish it to be. what is missing, so that i can change the grids displaylayout when adding tabscontrols dynamically?
compwaremedical said:usually, when I assigne the datasource to a grid , i can imediately change the displaylayout like header.caption ord hide columns and so on.
It's generally better to use the InitializeLayout event, but there's nothing neccessarily wrong with modifying the layout in code immediately after setting the DataSource.
compwaremedical said:Therefor I supsend the layout to add a tab and the gridcontrol to the tabpage and after that i resume the layout. But after resuming layout i do not have a displaylayout yet, although i already assigned the datasource to the grid, that means i raise an errormessage, when trying to set displaylayouts header.caption for example.
What do you mean by "I do not have a DislayLayout yet?" The grid's DisplayLayout property is always there and will never return null. So it's not possible not to have one.
Perhaps something about calling SuspendLayout is preventing the grid from painting or is preventing the BindingManager from sending notifications. Ty taking out the calls to SuspendLayout and see if that helps.
Or, as I suggested, use the InitializeLayout event - which is generally a better place to do things, anyway, since the event will fire whenever the layout needs to be initialized.
Mike I am getting the similar problems witht he displaylayout being null. You have mentioned in other posts that it can only be null if the grid is disposing or is disposed but I have stepped through the code and it is not in any of these modes so there is obvuiously something else going on as to why DisplayLayout is null.
Sample code below. Just trying to merely save and lad the DisplayLayout:
Stream layoutStream = null;
private void grdGrid_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e){ if (layoutStream != null) {
//stepped through this bit and the DisplayLayout is not disposed or disposing if(!grdGrid.IsDisposed && !grdGrid.Disposing) { grdGrid.DisplayLayout.Load(layoutStream, PropertyCategories.All); } } else { grdGrid.DisplayLayout.Save(layoutStream, PropertyCategories.All); }}
Any idea what is going on? If its not supposed to be null other than when disposed or disposing, then I think this maybe a possible bug.
Putting the grid on a form probably just gave the grid a valid BindingContext. So you could probably have just set the grid.BindingContext to a new binding context, or the binding context of any existing form in your application.
I had a similar problem where I was programmatically creating a WinGrid and exporting to a spreadsheet. The ultrawinGrid.DisplayLayout property was null and the InitializeLayout event was not being fired. My logic existed in a C# class.
To fix this problem, I had to create a windows form in code (System.Windows.Forms.Form) and then add the UltraWinGrid to the form (from.controls.add) to get the displaylayout to not be null and for the InitializeLayout event to fire.
What error are you getting and on what line of code is it occurring?
How do you know it's the DisplayLayout that is null?
By the way... if you are writing code in InitializeLayout, you should be using e.Layout rather than grid.DisplayLayout. Perhaps your event handler is being called from a different grid than grdGrid.The DisplayLayout should still never be null, but it's generally good practice to use the event args, anyway.