RradioButton GroupBox in DataGridView? - c#

At run time I populate the values in DataGrid. dropdownlists,numeric,bool,text.
Is it possible to have a group of radio buttons ?
How can to do it ? how to add a groupbox so that one of them is selected at a time?
if I don't use a DataGridView, what is another suitable View ?
PS. not using xaml,wpf. classic windows forms.

Datagridview will not be suitable for this kind of combination, therefore, it's best if you use the flow layout control and that should help with organising the lay out.
I think you can't put a groupbox in a datagridview component, it's not made for that I think.

You have to create your own DataGridViewCell with painting logic and handling click events. Long time ago I did something like that, maybe it will be useful for you:
http://pastebin.com/nh4PfPPS
I paint check box and 2 radio buttons and save their positions to handle clicks.
I did not create column for it, because I needed to mix cell types, so just in place where I was adding rows and needed that cell I did:
row.Cells[0] = new DataGridViewControlCell();
I would not say it is the best way, but hey worked for me. Good luck :)

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

How to display a DataGridViewComboBoxColumn without a combobox?

This might by a silly question, but I was wondering if there was a way to have a DataGridViewComboBoxColumn in my DGV, but instead of showing the ugly combobox, it is just a white cell - when the cell is clicked, the dropdown shows up.
My problem is basically that this is really ugly:
How do you get around this? I am used to working in WPF, not Winforms. Is it possible to just hide the combobox, but have the cell function the exact same way??
ComboBoxes have three different appearances you can choose from. In this case you're looking for flat appearance. But if you want a beautiful datagridview you might want to check out PF Grid Toolkit. It goes for quite an affordable price.
NB: This page could also prove useful.
you would want to handle the cell formatting event.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx

Issue with controls on tabControl - no data refreshing before clicking on all tabPages

I have a problem with controls nested in a TabControl. I have a TabControl with n TabPages, with a DataGridView on each TabPage. Each DataGridView has a CheckBoxC column. I populate all datagridviews with different datasources (so each has different types of data). This is working ok!
I have added a ComboBox column so I can select all the rows on all DataGridViews. I do this programmatically (on a button click), and the counting of the selection is ok, except that the ticks are not added to checkBox cell of DataGridViews except on TabPage #1 (the one that I can see on startup).
If I click all the tabPages before I go and select all the rows in DataGridViews, the code works fine, and the ticks are added to all the rows (like I wanted).
But why this does not work without clicking all the tabPages? Is there any bug or something of TabControl?
My workaround was to add this in the load event of the form.
this.tabcontrol1.BindingContext = this.BindingContext;
I know this answer is correct for WPF, not positive about WinForms though. With WPF at least, it's a visually based interface, so the program does not load any of the objects/controls/etc on other tabs until they are clicked on. So it wouldn't be a bug, it's a part of the design.
I had a similar problem with trying to clear all textboxes on multiple tabs with a single button. I never did get it working, but I know there should be a way by using a combination of VisualTreeHelper and a foreach statement.
Again, this is based off of WPF and not WinForms, but hopefully it can point you in the right direction as to how to solve it.

Combobox with checkbox in winforms

I am trying to look for a simple way to design a winform with a combobox that has checkbox values in it to select multiple values.
But there are no free samples I could find.
If anybody has a good link for a sample which does not require a license.
Please let me know.
I am not looking for controls like telerik and infragistics.
Maybe this example can help you.
CheckBox ComboBox Extending the ComboBox Class and Its Items
It sounds like what you really want is a checked listbox control or maybe even just a listbox. These controls do multi-select in a way that is more standard for Windows.
If you really need a combo box with checkboxes in it, here's an article on code project I used once.
My suggestion, if space is an issue as #rmc00 has eluded to, place a button at the end of a readonly Textbox with perhaps an elipse (...) or down arrow (same as combobox) as the text of the button and when clicked or MouseDown make visible and position a CheckBoxList or open a popup dialog with a CheckBoxList this way you can either prepopulate at design time or pass a DataTable as a parameter/property to your control/form so it is databound at runtime. You can always place your control or write code to position the control/form exactly below your TextBox in the MouseDown/Click event. On check change update your textbox with a comma separated list (or go fancy and say if more than 3 items the text box can have the list stored in the Tag and the TextBox Text can have the count of items checked). Finally on LostFocus hide the Control (or Form), and further if you want to get fancy make the exception to not hide when the ActiveControl is the Button that way you can toggle the visibility of consecutive button presses.

Change the color of particular cell in datagrid?

I'm using VS 2008 to create a Windows Mobile program in C#. I need to display data in a grid. The only grid control I could find for Windows Mobile is DataGrid, so I placed one on my form. I now need to change the color of particular cell according to some condition. How do I do this?
Thanks in advance...
Usually grids have some kind of "fetching" event; your best solution is a row fetching or, at worst, a cell fetching event.
Listening to this, you can trap the moment when the row/cell is being created and apply your style logic. This is true for grids spanning from VB6 to Asp.NET, so I suppose can apply on your case too.
Hope this helps!
mt

Categories