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
Related
Like this
how can I customize datagridview to be like this photo in like
which means its start with full empty rows, like in accounting programs
Firstly, I would not recommend that you do this. Most good accounting programs don't do it either!
That said, the first way to do it would be to create a suitable set of data with the required number of rows already in it and then assign that as the data source. If you go beyone the available list then add a new row to the datasource.
The second way to do it would be to subclass DataGridView and handle the drawing yourself to draw the grid to mimic the rows.
I cannot recommend either option, and would recommend simply allowing new rows to be automatically added as the user enters something in the 'new' row. This is the way that most good programs do it.
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
I have a very complex databinding I want to accomplish here using the below:
2 SQL CE tables named mainTable and secondaryTable
1 Fluidkit ElementFlow control named myElmntFlow
2 UserControls named myUsrCtrl and otherUsrCtrl
All of the above are already created and implemented but the UserControls are populated into the myElmntFlow control's items list programmatically through lenghty backgroundworker code that does take a good bit of time to run when the number of items to enter is > 20.
This is how they get created as of now:
The backgroundworker loops through each row of mainTable and adds the myUsrCtrl control to the list of items in myElmntFlow if the value of the row in column "Selected" = "'Yes'".
Then, it modifies the content of the newly added myUsrCtrl as such: it adds a otherUsrCtrl into the myUsrCtrl's stackpanel (named stckPanel) for each row in secondaryTable where the value of the column "FullName " = the value of the same column of the mainTable row we used to created the myUsrCtrl control.
And then populates the otherUsrCtrl's sevaral labels with the value of the secondaryTable row looked at at the moment.
Very confusing but it is a complex scenario. Let's use an example:
In mainTable, the row #4 has the FullName value of "Chad Jones" and
also has the Selected value of "Yes".
A new instance of the myUsrCtrl control is added to the
myElmntFlow's list of items as such:
myElmntFlow.Items.Add(myUsrCtrl);
The newly added myUsrCtrl control has a stackpanel (stckPanel)
We filter the secondaryTable where the FullName = "Chad Jones"
For each row in the now filtered secondaryTable, we add a new
instance of otherUsrCtrl to the previously created myUsrCtrl's
stckPanel control
The different labels in the otherUsrCtrl are populated with the values of the
row in secondaryTable
Can this possibly be converted into a DataBinding within the XAML of the controls as I want to implement several features later on (such as a nice SearchBox with autocomplete) that would be quite poor if they were to be coded behind by writing hundreds of line to tell which data to filter, sort , take , compare etc...
I wrote this as clearly as I could, just hope it's understandable.
PS: I would like to keep my SQL structure as the data is going to become quite consequent over time and I believe that the SQL is the way to go when manipulating thousands of lines.
It's not a very confusing scenario, it's just made confusing by the complex handling that goes on there. It can indeed be made much easier using bindings and MVVM (Model-View-ViewModel) so please take some time to read about the basics of that. There are a ton of tutorials and introductory materials on the web, a simple search will give you more than enough to go on.
When you're comfortable with the concepts, all you need is to transform the data into a sequence of objects (no matter how you go about it), then use an ItemsControl to represent the UI for a list of items. Use DataTemplates to specify how each item should be displayed, binding elements in the DataTemplate to the properties of each item. These things can be nested so you can have ItemsControls in your DataTemplates which use other DataTemplates etc.
In order to represent a collection of items bound to an ItemsControl look at using an ICollectionView which will help tremendously with filtering/sorting/etc.
Sorry about the very broad strokes but it is a pretty broad question. If you need more specific help I'll gladly add more.
I have been trying unsuccessfully to do something that I would have thought in WPF should be quite easy.
We wanted to display a table in a cell of another table recursively (that can be expanded / collapsed). (Not Master / Detail, but true tree like recursion into the sub-tables)
We thought maybe a DataGrid or a GridView might be able to achive this but we've been unsucessful in our attempts, so I wanted to check with the world at large that we're trying to do something possible.
If it helps, it's readonly data, but it's all data-driven so there's a dynamic number of rows and columns.
Look into the DataGrid's RowDetailsTemplate. You should be able to add whatever you want in there including another Grid.
You need a hierarchical data grid, evaluate Infragistics
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.