Customize Data Grid View in Windows Form - c#

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.

Related

C# Windows Forms

I am new here and startig to learn C#. Before I programed in C and Windows.Forms in VBA.
Now I have a Project where I receive serial port data from a device, that sends the contents of various C-structs (some nested). I want to display these Datasets in Windows.Forms each labeled, edit the values and send it back to the device. So it can easily copy the received data into its structures.
Now I don't know an easy way to get the structured data inside a c#-byte-list into various text boxes(?) that have describing labels to it. I would like to design the layout in Form Designer, so not want to create the controls dynamically. But maybe this is the best way?
Does someone know a good strategy / a best suited control to achieve this?
Thanks in advance.
You could use a DataGridView with control buttons inside each row or external buttons that handle selected row(s). In order to see and change the details a corresponding dialog form comes in handy in which I would implement a hex editor with the byte/hex values on the left and the readable value on the right.
To display the byte data just convert it to a hexadecimal string so you can display it in one column inside the DataGridView.
For the nesting part, you could either implement child elements inside the DataGridView itself (you need to either code this yourself or use some 3rd party extensions like this or Telerik, e.g., which also has the possibility of including button columns) or open a conditional dialog form which again shows a DataGridView for the child elements and so on (so you would open the same form as child form ad infinitum).
Thereby you are as flexible as possible and do not rely on fixed text box elements which you have to add or extend on further information.

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

Text user control with sorting possibility

I need control in winforms that can add tens lines per second. I use now richtextbox, but i'm looking for something with sorting possibility (according some datatime or int)
Lines which are added are just custom objects complex of several int and datetime.
Delay aspect is quite important here.
EDIT:
It can be also some table/grid, but I suppose it take too much time (searching existing lines and inserting new line into appropriate place)
RichTextBox will typically offer more overhead in managing, searching, and inserting.
Much more appropriate would be a control like ListView. It gives simpler control over sorting, scrolling, and more.
Additionally, ListView has the ability to handle Virtual data backing. If performance or content size is an issue, this will let you only worry about the sub-set currently displayed in the ListView "view" window.
Two options
Use RichTextBox as you are doing now hold the data in a structure like SortedList from which the control will be updated.
Or, you can use a Grid, format the look and feel of the grid to remove the row & column grid lines and each new line will get appended this way you can let user sort by clicking on the header...
As for adding say ten lines per second I am pretty certain both these controls can handle more traffic than that....

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