Link to home
Start Free TrialLog in
Avatar of ablesa
ablesa

asked on

Displaying a user control in DataGridView

I have a user control that I got from this link:  http://www.codeproject.com/KB/combobox/checkedcombobox.aspx.

I want to be able to use this control in a DataGridView cell (the whole column will not be of this type, but just certain cells).  I have copied code off of the internet to try to accomplish this, but its not working...  I can see the control in the grid cell, but when I click on the drop-down, it shows me no members in the drop-down.  I am outta my league here, so thought I would see if there's anyone there that could tell me what I might need to change in my datagrid classes.

Thanks!
DataGridViewCheckedComboBox.doc
Avatar of jake072
jake072
Flag of Canada image

ablesa,

You need to expose the CheckedComboBox properties to your column control, otherwise, you'll have no way of setting the items/datasource, etc.

Attached is a custom control for a NumericUpDown to add to the datagridview.  The relevant code you want to look at are the additional fields added to the column type, and their associated parts in the cell and editing control.

For example, take a look at the "DecimalPlaces" Property in the Column specification, and follow that through down to the editing control to see an example of how to expose your control's properties to the column.  So, you'll want to expose the DataSource and/or Items (, etc...) properties of your CheckedComboBox.

Let me know if you need more help, but please look at the provided samples and try on your own; I can help you clean up any problems.

Jake
DataGridViewNumericUpDownCell.vb
DataGridViewNumericUpDownColumn.vb
DataGridViewNumericUpDownEditing.vb
Avatar of ablesa
ablesa

ASKER

Well, I tried what you said.  My problem seems to be that I want to use this only as a cell and not a column and thus I have no editing control because it blows up when I click the drop-down on the CheckComboBox and executes the "EditingCheckedComboBox" property of my cell class.  And I'm not sure how I should define the editing Control....

Any Ideas?  I have attached my current code...

Thank you.
DataGridViewCheckedComboBox2.doc
Ablesa,

I think I see what you mean.  So you DO NOT want to have a Column of CheckComboBox's?  How do you accomplish that?  How are you testing, if you have no column?  I do not know how you would accomplish that.

The only thing I can think of to try is to trap the EditorControlShowing event on the DataGridView, and try messing with it there.  If it's a cell you want to replace, you could get the control's bounds, and simply add one of your own CheckComboBox's to the DataGridView to essentially "Cover" whatever is really there, then send the result back to the DataGridView, and hide your control.

Jake
Avatar of ablesa

ASKER

no...i do not want a column of checkComboBoxes....  Its just like I can do now with the built-in types...  I can have a column of type DataGridViewTextColumn, but I can create a DataGridViewComboBoxCell and put it in one of the cells in that column.  I want to have a column of type DataGridViewTextColumn and I want to create a DataGridViewCheckedComboBoxCell and put it in one of the cells in that column...

I will try what you suggest.  Let me know if you come up with anything else...  I thought there was a way to do this...

As usual, thanks!
Avatar of ablesa

ASKER

Jake,

I read back over your comments again.  I would like to try the second option you mentioned:  "get the control's bounds, and simply add one of your own CheckComboBox's to the DataGridView to essentially "Cover" whatever is really there, then send the result back to the DataGridView, and hide your control."

how would I go about doing that?

Thanks.
Ablesa,

You would trap the EditingControlShowing event of the DataGridView.

Once you have that event, you can test for what cell has focus; DataGridView.CurrentCellAddress will give you a point with the address of the current cell (X, Y) = (Row, Column)...  That way you can be sure to only show your control when you want to.

Next, use the e.Control.Bounds to size and place your control.

There WILL be glitches to work out (trust me), but it is doable.

Good luck, let me know if you need more help.

Jake
Avatar of Bob Learned
There are ways to define custom classes for the DataGridView column and cell to use custom controls...
Avatar of ablesa

ASKER

TheLearnedOne posted his comment AFTER I requested to close this question.  I did not see his comment when I closed it.

In response to TheLearnedOne, Jake left me with the impression that in order to do the custom control in my cell that my column would have to be custom as well.  I don't want the column to be custom...just certain cells.  He implied that what I wanted to do could be done but that it would be messy and I got the impression I would spend much time on it and maybe still not like the outcome.  So, my decision was to find another solution which I have done.  Its not as elegant as what I wanted to do but it works.
I thank you all for your help.  I don't have problems awarding points to Jake for his help, but don't see how to do that here...
There is an inherent problem with the DataGridView, and that is the columns are strong-typed.  For example, there is a property for the DataGridViewTextBoxColumn that defines the type of cells that are expected (DataGridViewTextBoxCell).  In order to use a custom cell, you would need a custom column that defines that custom cell type.

DataGridViewTextBoxColumn.CellTemplate:


<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _
Public Overrides Property CellTemplate As DataGridViewCell
    Get
        Return MyBase.CellTemplate
    End Get
    Set(ByVal value As DataGridViewCell)
        If ((Not value Is Nothing) AndAlso Not TypeOf value Is DataGridViewTextBoxCell) Then
            Throw New InvalidCastException(SR.GetString("DataGridViewTypeColumn_WrongCellTemplateType", New Object() { "System.Windows.Forms.DataGridViewTextBoxCell" }))
        End If
        MyBase.CellTemplate = value
    End Set
End Property

Open in new window

Avatar of ablesa

ASKER

I guess what doesn't make sense to me is the fact that I can have a DataGridViewTextBoxColumn in my grid, but then I can put, for instance, a DataGridViewComboBoxCell, a DataGridViewCheckBoxCell or DataGridViewTextBoxCell in the cells of that column... Those all work fine, SO, I would think that I could have a DataGridViewTextBoxColumn and put a custom cell type of my choosing in there...  
But, no go, I guess...
Hmmm...now the ganglea are a twitchin' *GRIN*.

I never assume that I have all the answers to everything.  The untested assumption is you can't mix cell types.  Did you try to create a custom cell class, and use the same procedure to assign that cell type to column?
Could you use generics to make a generic column type?  I wonder...  Sorry, don't have the time to try; but if I get around to it, I'll definitely post here (PLEASE DON'T DELETE THIS QUESTION).

That would be sweet =)

Jake
Avatar of ablesa

ASKER

In answer to TheLearnedOne, I did try to create the custom cell class and assign it to a column, however, it did not work.  Jake072 asked to leave the question open, but I am getting bugged by Experts Exchange to close the issue.  If I hear no more before the next time EE bugs me to resolve the question, I guess I will delete...

Again, thank you both for your time and effort.
I'm asking for you to get a refund, and close the question, not to delete it...

Thanks,

Jake
I am sorry that you are getting bugged so much, but this question has some merit, and I was hoping that we could find a way to help you.  Since I am trying desparately to be the "DataGridView" guy, I am intrigued by any question that can point out that I still have some learning to do.

When you said, "however, it did not work", what did you try, and what indications did you have (errors, strange happenings, ...)?
Avatar of ablesa

ASKER

hmmm...  kinda hard to remember.  I beleive that when I tried to assign the custom cell to the datagridviewtextcolumn, it gave me a cast error.  I did find a work around, so I have been able to move beyond this, altho it would be nice to know for future applications...
thanks.
I imagine that cast exception came from the code that I showed from the .NET framework for the DataGridViewTextBoxColumn:

 Throw New InvalidCastException(SR.GetString("DataGridViewTypeColumn_WrongCellTemplateType", New Object() { "System.Windows.Forms.DataGridViewTextBoxCell" }))

What work around did you find?
Avatar of ablesa

ASKER

Well, my goal was to take a custom control which was a drop-down checkbox control and put that in the grid in one cell.  However, when I couldn't get that to work, I just put checkboxcells in the grid, one for each checkbox that would have been in the drop-down checkbox.  So, instead of using one cell (one row) for the checkboxes, I used x number of checkboxes (x rows), depending on how many checkboxes would have been in the drop-down.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ablesa

ASKER

While I don't have time to implement my user control using theLearnedOne's example here, I think that when I do find the time, this will work for me.