- Download source files-2.45 kb
Introduction
I neededComboBox
In myDataGrid
. After looking around on the web, I found has examples, but none of them worked for me.
With inspiration from Alastair stells article here on the Code project and what else I found on the Internet, I have made the followingDatagridcomboboxcolumn
Class.
Why did the other examples not work
All the other examples populateComboBox
WithDataview
, But I need to (Want To Be Able To) populate myComboBox
WithIlist
(Arraylist
) Instead ofDataview
.
Columncombobox = <SPAN class = "CS-keyword"> New </span> datagridcomboboxcolumn (); columncombobox. comboBox. datasource = <SPAN class = "CS-keyword"> New </span> <B> arraylist </B> (mydataclass. getarray (); columncombobox. comboBox. displaymember = <SPAN class = "CPP-string"> "name" </span>; columncombobox. comboBox. valuemember = <SPAN class = "CPP-string"> "guid" </span>; |
AndMydataclass. getarray ()
ReturnsMydataclass []
, And has two properties namedName
AndGuid
.
The other examples Except CTColumncombobox. ComboBox. datasource
To beDataview
, And it beingArraylist
Generates exceptions.
I use the ComboBox to fetch display text
Since you don't know the typeColumncombobox. ComboBox. datasource
, You can't use that to translate between the underlying data and what to display inDataGrid
.
Instead, I useComboBox
Itself, by overridingComboBox
And implementing this method.
<SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> string </span> getdisplaytext (<SPAN class = "CS-keyword ""> Object </span> value) {<SPAN class = "CS-comment"> // get the text. </span> <SPAN class = "CS-keyword"> string </span> text = <SPAN class = "CS-keyword"> string </span>. empty; <SPAN class = "CS-keyword"> int </span> memindex =-<SPAN class = "CS-literal"> 1 </span>; <SPAN class = "CS-keyword"> try </span> {<SPAN class = "CS-keyword"> base </span>. beginupdate (); memindex = <SPAN class = "CS-keyword"> base </span>. selectedindex; <B> <SPAN class = "CS-keyword"> base </span>. selectedvalue = value. tostring (); text = <SPAN class = "CS-keyword"> base </span>. selecteditem. tostring (); <SPAN class = "CS-keyword"> base </span>. selectedindex = memindex; </B >}< SPAN class = "CS-keyword"> catch </SPAN >{}< SPAN class = "CS-keyword"> finally </SPAN >{< Span class = "CS-keyword"> base </span>. endupdate ();} <SPAN class = "CS-keyword"> return </span> text;} <SPAN class = "CS-comment"> // getdisplaytext </span> |
What I do is simple. I select the item which displays the text I want, get the text and then reselects the original item. by doing it this way, it doesn' t matter what data source is used.
Because I useComboBox
Itself to fetch the display text,ComboBox
Must be populated beforeDataGrid
Is drawn.
Alastair stells noted about this in his article:
Another issue which arose was an eye-opener! I discoveredComboBox
Does not get populated untilComboBox. Visible
Property is set for the first time.
This means thatComboBox
Can't be used to fetch the initial display text, because it is not visible whenDataGrid
Is first shown (painted ).
I use a normalComboBox
To restrict strate the problem and the solution.
ComboBox = <SPAN class = "CS-keyword"> New </span> ComboBox (); ComboBox. datasource = <SPAN class = "CS-keyword"> New </span> arraylist (mydataclass. getarray (); ComboBox. displaymember = <SPAN class = "CPP-string"> "name" </span> ComboBox. valuemember = <SPAN class = "CPP-string"> "guid" </span> MessageBox. show (ComboBox. items. count. tostring (); <B> <SPAN class = "CS-comment"> // This is always 0! </Span> </B> |
I learned that it didn't help to showComboBox
, But instead I have to set its parent-which internally commits the data fromDatasource
ToItems
Collection.
ComboBox = <SPAN class = "CS-keyword"> New </span> ComboBox (); <B> ComboBox. parent = <SPAN class = "CS-keyword"> This </span>; </B> <SPAN class = "CS-comment"> // This Is A form instance in my case. </span> ComboBox. datasource = <SPAN class = "CS-keyword"> New </span> arraylist (mydataclass. getarray (); ComboBox. displaymember = <SPAN class = "CPP-string"> "name" </span> ComboBox. valuemember = <SPAN class = "CPP-string"> "guid" </span> <B> <SPAN class = "CS-comment"> // This is mydataclass. getarray (). count </span> </B> MessageBox. show (ComboBox. items. count. tostring ()); |
What else about my datagridcomboboxcolumn
The source code is straight forward. First, I inheritedDatagridtextboxcolumn
, But my class then evolved into inheritingDatagridcolumnstyle
. This meant that I had to implementPaint
Methods, but at this point, I had some examples of that as well. I like the idea not having an invisibleTextbox
Behind it all.
How to Use
Sadly, I don't know how to "register" MyDatagridcomboboxcolumn
WithGridcolumnstyle
S, enabling me to designDataGrid
Columns in the designer. This Code does it manually.
<SPAN class = "CS-comment"> // create a datagridtablestyle object. </span> datagridtablestyle tablestyle = <SPAN class = "CS-keyword"> New </span> datagridtablestyle (); datagridtextboxcolumn columntextbox; datagridcomboboxcolumn columncombobox; tablestyle. rowheadersvisible = <SPAN class = "CS-keyword"> true </span>; tablestyle. rowheaderwidth = <SPAN class = "CS-literal"> 20 </span>; <SPAN class = "CS-comment"> // Add customized columns. </span> <B> columncombobox = <SPAN class = "CS-keyword"> New </span> datagridcomboboxcolumn (); columncombobox. comboBox. parent = <SPAN class = "CS-keyword"> This </span>; <SPAN class = "CS-comment"> // commit dataset. </span> columncombobox. comboBox. datasource = <SPAN class = "CS-keyword"> New </span> arraylist (mydataclass. getarray (); columncombobox. comboBox. displaymember = <SPAN class = "CPP-string"> "name" </span> columncombobox. comboBox. valuemember = <SPAN class = "CPP-string"> "guid" </span> columncombobox. mappingname = <SPAN class = "CPP-string"> "nameguid" </span>; columncombobox. headertext = <SPAN class = "CPP-string"> "name" </span>; columncombobox. width = <SPAN class = "CS-literal"> 200 </span>; tablestyle. gridcolumnstyles. add (columncombobox); </B> columntextbox = <SPAN class = "CS-keyword"> New </span> datagridtextboxcolumn (); columntextbox. mappingname = <SPAN class = "CPP-string"> "textstring" </span>; columntextbox. headertext = <SPAN class = "CPP-string"> "text" </span>; columntextbox. width = <SPAN class = "CS-literal"> 200 </span>; tablestyle. gridcolumnstyles. add (columntextbox); <SPAN class = "CS-comment"> // Add the custom tablestyle to the DataGrid. </span> DataGrid. tablestyles. clear (); DataGrid. tablestyles. add (tablestyle); DataGrid. datasource = ..... from my database .....; tablestyle. mappingname = DataGrid. datasource. getType (). name; |
I think I have focused a problem here: if you wantComboBox
In yourDataGrid
, And you want to populateComboBox
From your own custom class andArraylist
.
I hope someone finds it useful-Enjoy.