简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM