Gridview and Column sets - c#

I have a Gridview that is data bound to an array of objects, with a ton of properties attached to them. The grid would need to be too wide to show all of them at once (and also overwhelming for the user), so I'd like to have some link buttons that post back to the server and show different sets of columns (all from this same data set array of objects), based on what "tab" the user clicked.
In the GridView I use TemplateFields to bind the columns to the object properties. What would be the best way to implement the different columns and views?
Should I just bind all the data, and then on the post back event for a tab press, show and hide only the columns I need for that tab? This seems like since it would be binding a lot more data than I am showing, that it might be unnecessarily slow.
Should I dynamically create the columns before the binding, and only create the columns and bind the data for the columns I want to show? What is the performance hit the page would take for dynamically creating the columns each time based on which tab was pressed?
Thanks!

If you have that many columns, you may want to think about using a different control instead of a gridview. A DetailsView with paging would probably render better and be more manageable for the users.
To answer your question, though, I don't believe option 1 would be that much of a performance hit. And I believe that option would be less strenuous than option 2. That mostly my opinion, though.

Related

Using a custom UserControl as a Column in a DataGridView

I've created a custom User Control that inherits from UserControl. It is pretty basic, it has some text fields, drop downs, and some radio buttons for right now. Ultimately I would like to string together 100's of these side by side to allow the user to fill out as many as they would like. The ideal solution to this would be to have a DataGridView where each column is an instance of my custom User Control. Is there a way to accomplish this? Or perhaps a better/alternative solution?
My initial thought is I'll need to create a custom DataGridViewCell that uses this custom control, but I don't know if this is a) possible and b) the most efficient way to do this.
I'm working with winforms.
You want to use a UserControl as a Column in a DataGridView. To display scores/rows/columns of your UserControl ('yuc') there are several options. Here are three that come to my mind:
Drop the DGV and go for a FlowLayoutPanel. This is simple to implement and will work pretty much out of the box. The only con is that the performance will get sluggish if you have too many controls in total in it. Let's assume your UC (yuc) has 10 controls; a few thousand controls are the limit in WinForms, so a few (100-300) yucs will work ok, but beyond that you need to rethink the design.
Go all the way and create a specialized DataGridView Cell that will host your yuc. Here is a complete and nice walk-through. As you can see this will amount to quite a lot of extra work; classes to add, interfaces to implement etc.. And, what is worse: All this is really meant to make the new cell type act like a regular DGV cell, read it will hold and allow you to edit only one value. That's a lot less than what your yuc probably can do..
Option 3: Cheat! You can combine the avantages of yuc data and DGV display performance if you display only one yuc in the current cell by overlaying it and make all other cells display what their yucs would look like.
This third option will allow you to add pretty much as many rows as your memory allows, although it is worth mentioning that the total column widths can't exceed 64k.
I can think of two ways to create the right display: The cells could display a Bitmap they hold along with their other data in a Tag structure or they could paint them in the CellPaint event. The former takes more memory but should work faster.
You need to create a data class yucData for your yuc that holds all data needed to initalze a yuc. It could also hold a Bitmap a yuc can create using the DrawToBitmap method.
Now each time the current cell is moved you show/move the editing yuc and initialize it to the data in the cell's Tag. When the values have changed you update the Bitmap.
And in the CellPainting event you draw the Bitmap into each cell.
See here for an example of overlaying a UserControl onto a DataGridView. There it overlays a whole row, which grows accordion-like to hold all its size.. The other rows and cells are quite regular..
What I wrote is about rows but you can just as well put them into columns you create dynamically.
But all this is really only worth it if you hit the limit of controls in Winforms with a FLP.
Well, if WPF is an option, there all this will not be an issue..
Excuse me.I can not write English.
But the sample code I wrote may help solve this problem and I am happy if that happens.
Display user controls in C # 4.0 Winforms DataGridView

WPF window that looks like excel

I am new to WPF and c#, and I am trying trying to create an excel like table with a fixed number of columns and a varying number of rows according to the user's needs.People advise to use a datagrid, but I am very confused, some say that it is used mostly to display data source content (which is data contained in database if I understood). I read also about listviews, binding things to itemsource...etc. This is really a lot of information to work with! What I simply need is a way to create a table with fixed columns, and adding rows automatically when the user clicks on a button, that's it! No binding, or anything of this sort.But how to achieve that? Also, if you have good websites tutorials for working with datagrids, I would be very grateful (most of those that I found are too much complex, or don't explain well).
Thanks a lot!
You can create datatemplates that will style your data to look however you like - in this case an Excel row. Then you can display this data as the ItemsSource in an items control. Since you want the number of rows to vary based on some criteria, your data should be in an ObservableCollection. I'm not sure how you'd set-up the header, but I think you could style-up some containers and bind their width properties to the datatemplate controls.
If you wish to do more cell level customizations, then you can try Grid
http://www.syncfusion.com/products/user-interface-edition/wpf/grid/grid-control

Add extra columns after initial load (Telerik MVC Grid)

I want to be able to add extra columns after the grid has been loaded. The user should be able to submit a form from the top of the page where the user may checks a checkbox, and then the page updates with the newly added column to the grid.
As an example, if I have two columns from the initial load and the user wants to add a third column. How can I achieve this technically? I saw an example of creating a new GridColumnSettings, though I don't think that's what I want.
The Telerik documentation about column settings is pretty good:
http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-columns.html#ColumnConfig
I feel like you have two options.
One is to use the Hidden method when generating the columns, and then use javascript to "un-hide" the extra columns when they click a button.
The other option is to selectively render the columns or not on the sever side. Take the value of the checkbox, and pass that to the Visible method of the column in your column definintion code.

Dynamically add textbox in asp.net

In my page, i got two column and multiple rows. The first column contain the label such as question for the 1st row, and the options for the questions. And the second column is the textboxes. When i click on add button, i wish to add those controls to page which subsequently allowed me to add the value in the texbox to database. I did some research but most of them uses javascript or datatable. Is there any other method?
You don't specifically say what type of .net development you are doing, and your question is tagged with asp-classic, which I doubt you're using. [If you are please please stop] So I will assume you are using Web Forms.
While I don't agree with Inerdial's position that you should avoid dynamic controls at all costs, I will say it does make things much more complex and requires a very good knowledge of the ASP.net Lifecycle. If you truly want to go down that path, here is a great resource.
With that said what you are describing to me does not appear to need that and his suggestion of setting the control visibility to false is a good one.
You could create a row, a panel or a div and output the controls that you need when adding a new row and set it's server-side visibility to false whenever you don't want it displayed. Then you could have a link that when clicked it toggles the visibility to true and will allow the user to add items. Once users add items they'll be displayed in your data table and you can reuse the form to add additional items.
I would also like to encourage you to consider JavaScript if it isn't an overly complex form. It eliminates an extra round-trip to your server and in general is a better user experience.
Edit: This link may also be of use to you.

Storing a collection into a list and session - Front End

I have this program in which I am trying to store a collection of values into a list and session
Example:
valueCollection = (List<Values>)Session["Value"];
I want to do the front end in a way which will show a table with each row showing a label and textbox. This would be too simple to do obviously but I want it to show 4 rows of the table by default and then the user can select "add another" this will then add another row onto the table with a label and textbox exactly similar to the 4 default. Everytime the user selects "add another" the table increments by 1.
How do you do something like that, or is there an easy way?
Here is a screenshot to explain it better:
http://img828.imageshack.us/img828/9986/idead.png
The basic idea is that you bind a control to your data and provide GUI controls to allow users to add/edit/delete records. Once the records are modified you re-bind the control to see the changes.
Take a look at the Databound controls in the .Net Framework. For a quick first pass you could use a ListView - a control which provides built-in functionality to automatically generate and mange the add/edit/delete GUI elements.
In 'real world' usage, storing big datasets isn't a great idea as more users = more sessions and each session uses server resources. You should really be getting/storing your data in a database or perhaps an XML file.
HTH

Categories