Problem with dynamically added subheader into custom GridView - c#

I'd like to get a grid with ability to add subheader (one or several rows) according to value of certain column (data source is grouped sorted by this column). Within my inherited GridView I override OnRowDataBind method: If value of considering column on current call of OnRowDataBind is not equal to previous values, then special header row is inserted before current row.
Table tbl = this.Controls[0] as Table; //this - pointer to CustomGridView
tbl.Controls.AddAt(rowIndex + add_counter + 1, NewSubHeaderRow); /*rowIndex - RowIndex of current row, add_counter - amount of already added SubHrader rows */
All works right. But problem occures on postback, when GridView restores it's state. First row and all its controls into every group (other words, first row after each dynamically added subheader row) comes without any attributes. But second row in group keeps both its own attributes and attributes of first row.
Some help will be very usefull. Maybe there is another (and right) way to add row into GridView. Then please, provide me with links to tutorials or articles.

Dynamic controls disappear on postback because on postback framework doesn't have information about such controls. It's programmer's responsibility to keep track of these dynamic controls and recreate them after postback...
Please refer to following post for re-creating dynamic controls on postback:
FAQ: Why do dynamic controls disappear on postback

Thanks for Waqas.
Here is my decision.
I override CreateChild(datasource, isBinding) and create list where put index of row, before which you should add subheader row. On SaveViewState i add this list to sealized object. On Load ViewState i load this list and if it's not null create subheader rows by saved indexes.
Maybe, there is a simpler and more natural way. But it's hidden from me securely )))

Related

ASPX C# Form that automatically adds another row of fields If Fields have Values

So basically what i want to do is, automatically create a 2nd row of fields if the first initial row is filled out.
It does not need to be triggered by a button, necessarily. How would i do this?
You will probably need some JavaScript tot resolve this cliƫnt side. For instance a postback on the last cell modification.
Represent this as a Grid control that can be represented by a DataTable. Each row will be a DataRow. Bind the DataTable to this grid.
Do a postback for each of the individual fields in the row. Run a method to check if all fields are non-empty for each postback. If yes, add a new row. Rebind DataTable to the form.
Will this work?
Take a look at this Knockout.js video on MSDN.
The sample code is there too to add fields on the fly.
http://channel9.msdn.com/Events/MIX/MIX11/FRM08

Editable GridView Distinct Drop Down LIsts

I have a simple app I am messing around with its a basic Master/Details layout.
The details panel is actually a tab panel and on one of the tab panels it has a GridView. Within that grid view it shows the "current" in database information with all cells as read only.
alt text http://lh4.ggpht.com/_JU1W2P96pD4/TAeonNNYXgI/AAAAAAAAAq0/Y_-Kse7VObE/ExampleA.jpg
I then have an add button that inserts a row into the GriView and allows the user to enter some information. The first item in my GridView is a DropDownList, which is populated from an ObjectDataSource. This drop down is basically my unique index and there can only be one selected value per GridView.
alt text http://lh3.ggpht.com/_JU1W2P96pD4/TAeonIF3DdI/AAAAAAAAAq4/JhfOTsHgsf8/ExampleWithDropDown.png
What is the best way to remove the values from the list that are already in the GridView? Do I just need to remove the data source and add a OnDataBinding method that iterates through the grid view and generates a valid list of values?
I can't use a service method because if the user adds two rows they would have the option to insert duplicates description types.
Just want to make sure there is not a better way to do this.
Keep the object datasource, which I am assuming is an IEnumerable object, in a Session variable. When someone selects a certain value remove that value from the list in the session variable. Whenever they activate the form where the drop down list is displayed simply rebind the control to the list in the session variable. It will be easier than looping through the grid every time the user adds an entry.

Display ASP.NET GridView inside a selected row in another GridView

I have been given a mockup that I do not know is possible to code in ASP.NET without being a real html and javascript wizard.
I want a GridView that when a row is selected, the selected row expands and below the selected row a panel of additional information is shown, which would also include another small GridView. The idea is this would all be in-line. So if the user selected row 4, then the additional information would appear below row 4 and then after the additional information the parent GridView would continue with row 5.
Ultimately I would want to do a multi-select type of set up, but first I need to figure out if this is even possible. Also, the solution must be 508 Compliant
The one solution I considered was using only one "column". Then I would put all my fields in the ItemTemplate, and my detail panel content in the EditItemTemplate and instead of selecting the row, set it to edit mode. The problem with this solution is I lose the functionality of multiple columns if I throw everything in one huge ItemTemplate.
Any and all suggestions or ideas are appreciated.
What you're describing would be best fulfilled with a ListView control. It takes a little more templating work to set up than a grid view, but you have much more control over it and you can emulate the look of a GridView. You would set your Selected Item template to contain another ListView (or GridView) thats bound to your detailed data.
I've done this using a gridview inside a ListView and using ajaxcontroltoolkit collapsible panel
Your parent list would be a listview that would have 2 table rows for each item, on the first row your parent column, on the second row use colspan and add a gridview wrapped on a collapsible panel

maintaining the state of Newly added row in custom gridview. on postback

I have a custom gridview on rowdatabound i am adding new gridviewrows . but on postback the data in newly added row is not persists. Please help me to maintain the state of grid on postback as well...Actually i m showing group header rows and footer rows and showing some calculated values in group footer row...but on post back the values of new group rows is not persists but the grid row is ther without values.
You're adding the rows dynamically so you have to populate the grid every time the page is posted back, not just if(!this.IsPostBack).
If you're not persisting to a db every post-back and generating the grid looping through some result-set coming from your db - you might have to store info about the temporary rows in ViewState. People will be able to help on this if you expand your question.
If you're populating dynamically and the data is coming straight from some db (or even State) every post-back you might wanna disable the grid viewstate (as in that case you're sending back and forth useless stuff).

Moving to specific record within a DataTable

Hopefully simple, but can't find any such option.
I have a data table -- has say... 10 rows in it. Some fields on the form are bound to the table.columns respectively by name.
On another form that HAS a grid, as I scroll the grid, the detail fields are refreshed as expected since the grid does some magic to trigger the DataTable record changing event.
WITHOUT using a Data Grid, How can I direct the table to go to a specific row for load/display refresh on the form... ex:
DataTable MyTable = new DataTable();
MyTable = GetResultsFromSQL(); // returns the 10 rows
MyTable.LoadTheDataForRow(3);
MyTable.LoadTheDataForRow(7);
MyTable.LoadTheDataForRow(2);
I know I can use a foreach row in the table, but need explicit use as I don't want to go through all rows, but need specificity to specific ones.
I've looked at the LoadDataRow(), but that appears to be for pushing data back to a server. NOT what I want... I just want to have the "Current" row of the table to be of a specific one...
Thanks
After further research, I've found that a FORM based control "BindingSource" (or derivative) allows this, such as a grid. But, obviously, there's something the .Net engine is doing under the hood to ultimately "Load" a given row into something that ultimately triggers back to the "BindingSource"... The DataTable has RowChanging and RowChanged events which appear to be triggered by OnRowChanging / OnRowChanged delegates, but how can we tell the data table which "row" we want it as the active one.
The form controls can do this for their binding sources, but what is really happening under the hood to trigger these OnRowChanging events... I don't want to re-load a data table, rows, etc, just change what is considered the "Active" row, as in a grid, listbox, combobox, etc.
Have you tried:
MyDataTable.Rows[3];
MyDataTable.Rows[7];
MyDataTable.Rows[2];

Categories

Resources