Use ComboBox in the DataGrid)

Source: Internet
Author: User
    • Download source files-2.45 kb
Introduction

I neededComboBoxIn 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 followingDatagridcomboboxcolumnClass.

Why did the other examples not work

All the other examples populateComboBoxWithDataview, But I need to (Want To Be Able To) populate myComboBoxWithIlist(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 namedNameAndGuid.

The other examples Except CTColumncombobox. ComboBox. datasourceTo beDataview, And it beingArraylistGenerates 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 useComboBoxItself, by overridingComboBoxAnd 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 useComboBoxItself to fetch the display text,ComboBoxMust be populated beforeDataGridIs drawn.

Alastair stells noted about this in his article:

Another issue which arose was an eye-opener! I discoveredComboBoxDoes not get populated untilComboBox. VisibleProperty is set for the first time.

This means thatComboBoxCan't be used to fetch the initial display text, because it is not visible whenDataGridIs first shown (painted ).

I use a normalComboBoxTo 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 fromDatasourceToItemsCollection.

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 implementPaintMethods, but at this point, I had some examples of that as well. I like the idea not having an invisibleTextboxBehind it all.

How to Use

Sadly, I don't know how to "register" MyDatagridcomboboxcolumnWithGridcolumnstyleS, enabling me to designDataGridColumns 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 wantComboBoxIn yourDataGrid, And you want to populateComboBoxFrom your own custom class andArraylist.

I hope someone finds it useful-Enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.