Xgrid binding)

Source: Internet
Author: User
    1. Using system;
    2. Using system. Collections. Generic;
    3. Using system. componentmodel;
    4. Using system. Windows. forms;
    5. Using devexpress. xtragrid. columns;
    6. Using devexpress. xtragrid. Views. base;
    7. Using devexpress. xtragrid. Views. bandedgrid;
    8. Using devexpress. xtraeditors. repository;
    9. Namespace xtr1_ddemo1
    10. {
    11. Public partial class form1: devexpress. xtraeditors. xtraform
    12. {
    13. Public form1 ()
    14. {
    15. Initializecomponent ();
    16. // Drag the gridcontrol to the form, click "Click here to change View", and select "convert to" --> "advbandedgridview" in the pop-up menu ".
    17. Initgrid ();
    18. }
    19. /// Initialize the table
    20. Private void initgrid ()
    21. {
    22. // Advbandedgridview1 is the default view on the table. Note that: bandedgridview is declared here.
    23. Bandedgridview view = advbandedgridview1 as bandedgridview;
    24. View. beginupdate (); // edit the start view to prevent other events from being triggered.
    25. View. begindataupdate (); // start data editing
    26. View. bands. Clear ();
    27. // Modify additional options
    28. View. optionsview. showcolumnheaders = false; // The columnheader is hidden because the band Column exists.
    29. View. optionsview. showgrouppanel = false; // remove the group if it is not necessary.
    30. View. optionsview. enableappearanceevenrow = false; // whether to enable the appearance of even rows
    31. View. optionsview. enableappearanceoddrow = true; // whether to enable the appearance of odd rows
    32. View. optionsview. showfilterpanelmode = showfilterpanelmode. Never; // whether to display the filter panel
    33. View. optionscustomization. allowcolumnmoving = false; // whether to allow column Movement
    34. View. optionscustomization. allowcolumnresizing = false; // whether the column width can be adjusted
    35. View. optionscustomization. allowgroup = false; // whether to allow grouping
    36. View. optionscustomization. allowfilter = false; // whether to allow filtering
    37. View. optionscustomization. allowsort = true; // whether sorting is allowed
    38. View. optionsselection. enableappearancefocusedcell = true ;//???
    39. View. optionsbehavior. editable = true; // whether to allow users to edit Cells
    40. // Add a column title
    41. Gridband bandid = view. bands. addband ("ID ");
    42. Bandid. Visible = false; // hide the ID column
    43. Gridband bandname = view. bands. addband ("name ");
    44. Gridband bandsex = view. bands. addband ("gender ");
    45. Gridband bandbirth = view. bands. addband ("Date of Birth ");
    46. Gridband bandscore = view. bands. addband ("score ");
    47. Gridband bandmath = bandscore. Children. addband ("Mathematics ");
    48. Gridband bandchinese = bandscore. Children. addband (" ");
    49. Gridband bandenglish = bandscore. Children. addband ("English ");
    50. Gridband bandsubtotal = bandscore. Children. addband ("subtotal ");
    51. Gridband bandremark = view. bands. addband ("Remarks ");
    52. // Column Title alignment
    53. Bandname. appearanceheader. textoptions. halignment = devexpress. utils. horzarignment. Center;
    54. Bandsex. appearanceheader. textoptions. halignment = devexpress. utils. horzarignment. Center;
    55. Bandbirth. appearanceheader. textoptions. halignment = devexpress. utils. horzarignment. Center;
    56. Bandscore. appearanceheader. textoptions. halignment = devexpress. utils. horzarignment. Center;
    57. Bandmath. appearanceheader. textoptions. halignment = devexpress. utils. horzarignment. Center;
    58. Bandchinese. appearanceheader. textoptions. halignment = devexpress. utils. horzarignment. Center;
    59. Bandenglish. appearanceheader. textoptions. halignment = devexpress. utils. horzarignment. Center;
    60. Bandsubtotal. appearanceheader. textoptions. halignment = devexpress. utils. horzarignment. Center;
    61. Bandremark. appearanceheader. textoptions. halignment = devexpress. utils. horzarignment. Center;
    62. // Simulate several data items
    63. List <record> listdatasource = new list <record> ();
    64. Listdatasource. Add (New Record (1, "Zhang San", "male", convert. todatetime ("1989-5-6"), 115.5f, 101,96 ,""));
    65. Listdatasource. Add (New Record (2, "", "", convert. todatetime ("1987-12-23"), 92, 85, 87 ,""));
    66. Listdatasource. Add (New Record (3, "", "", convert. todatetime ("1990-2-11"), 88, 69, 41.5f ,""));
    67. Listdatasource. Add (New Record (4, "Zhao ", "male", convert. todatetime ("1988-9-1"), 119,108,110, "Remarks "));
    68. // Bind and display the data source
    69. Gridcontrol1.datasource = listdatasource;
    70. Gridcontrol1.mainview. populatecolumns ();
    71. // [Subtotal] because this column is not bound to the data source, you need to manually add
    72. // (It's a bit complicated. Take a look)
    73. String [] fieldnames = new string [] {"subtotal "};
    74. Gridcolumn column; // declare a single column
    75. Column = view. Columns. addfield (fieldnames [0]); // Add a data field
    76. Column. visibleindex = view. Columns. Count-1; // sets the position of the column when editing the view (the second to last column)
    77. Column. unboundtype = devexpress. Data. unboundcolumntype. Decimal;
    78. Column. optionscolumn. allowedit = false; // This column cannot be edited.
    79. Column. Visible = true;
    80. View. Columns. Add (column); // Add a column to the view.
    81. // Bind the event. When the [score] changes, the [subtotal] also changes.
    82. // (It seems that the cell value can be changed only in the binding event, and the direct modification is invalid)
    83. View. customunboundcolumndata + = new
    84. Devexpress. xtragrid. Views. Base. customcolumndataeventhandler (advbandedgridview1_munmunboundcolumndata );
    85. // Bind the [gender] column to ComboBox
    86. Repositoryitemcombobox ricombo = new repositoryitemcombobox ();
    87. Ricombo. Items. addrange (New String [] {"male", "female "});
    88. Gridcontrol1.repositoryitems. Add (ricombo );
    89. View. Columns ["sex"]. columnedit = ricombo;
    90. // [Date of Birth] column bound to date
    91. Repositoryitemdateedit ridate = new repositoryitemdateedit ();
    92. Gridcontrol1.repositoryitems. Add (ridate );
    93. View. Columns ["birth"]. columnedit = ridate;
    94. // Bind the [score] column to spinedit
    95. Repositoryitemspinedit rispin = new repositoryitemspinedit ();
    96. Gridcontrol1.repositoryitems. Add (rispin );
    97. View. Columns ["math"]. columnedit = rispin;
    98. View. Columns ["Chinese"]. columnedit = rispin;
    99. View. Columns ["English"]. columnedit = rispin;
    100. // Bind the [Remarks] column to memoexedit
    101. Repositoryitemmemoexedit rimemoex = new repositoryitemmemoexedit ();
    102. Gridcontrol1.repositoryitems. Add (rimemoex );
    103. View. Columns ["remark"]. columnedit = rimemoex;
    104. // Add a summary in the subtotal Column
    105. View. optionsview. showfooter = true; // display the table footer
    106. View. Columns ["subtotal"]. summaryitem. fieldname = "subtotal ";
    107. View. Columns ["subtotal"]. summaryitem. displayformat = "{0: F2 }";
    108. View. Columns ["subtotal"]. summaryitem. summarytype = devexpress. Data. summaryitemtype. Average;
    109. // Match the title column with the data column
    110. View. Columns ["ID"]. ownerband = bandid;
    111. View. Columns ["name"]. ownerband = bandname;
    112. View. Columns ["sex"]. ownerband = bandsex;
    113. View. Columns ["birth"]. ownerband = bandbirth;
    114. View. Columns ["math"]. ownerband = bandmath;
    115. View. Columns ["Chinese"]. ownerband = bandchinese;
    116. View. Columns ["English"]. ownerband = bandenglish;
    117. View. Columns ["subtotal"]. ownerband = bandsubtotal;
    118. View. Columns ["remark"]. ownerband = bandremark;
    119. View. enddataupdate (); // end data editing
    120. View. endupdate (); // edit the end view
    121. }
    122. // Calculate the subtotal
    123. Private float calcsubtotal (float math, float Chinese, float English)
    124. {
    125. Return math + Chinese + English;
    126. }
    127. Private void advbandedgridview1_munmunboundcolumndata (Object sender, customcolumndataeventargs E)
    128. {
    129. Columnview colview = sender as columnview;
    130. If (E. Column. fieldname = "subtotal" & E. isgetdata) E. value = calcsubtotal (
    131. Convert. tosingle (colview. getrowcellvalue (E. rowhandle, colview. Columns ["math"]),
    132. Convert. tosingle (colview. getrowcellvalue (E. rowhandle, colview. Columns ["Chinese"]),
    133. Convert. tosingle (colview. getrowcellvalue (E. rowhandle, colview. Columns ["English"]);
    134. }
    135. # Bind region to the data source that implements the ilist interface during runtime
    136. Public class record
    137. {
    138. Int ID;
    139. Datetime birth;
    140. String name, sex, remark;
    141. Float math, Chinese, English;
    142. Public Record (int id, string name, string sex, datetime birth, float math, float Chinese, float English, string remark)
    143. {
    144. This. ID = ID;
    145. This. Name = Name;
    146. This. Sex = sex;
    147. This. Birth = birth;
    148. This. Math = math;
    149. This. Chinese = Chinese;
    150. This. English = English;
    151. This. remark = remark;
    152. }
    153. Public int ID {get {return ID ;}}
    154. Public string name
    155. {
    156. Get {return name ;}
    157. Set {name = value ;}
    158. }
    159. Public String sex
    160. {
    161. Get {return sex ;}
    162. Set {sex = value ;}
    163. }
    164. Public datetime birth
    165. {
    166. Get {return birth ;}
    167. Set {birth = value ;}
    168. }
    169. Public float math
    170. {
    171. Get {return math ;}
    172. Set {math = value ;}
    173. }
    174. Public float Chinese
    175. {
    176. Get {return Chinese ;}
    177. Set {Chinese = value ;}
    178. }
    179. Public float English
    180. {
    181. Get {return English ;}
    182. Set {English = value ;}
    183. }
    184. Public String remark
    185. {
    186. Get {return remark ;}
    187. Set {remark = value ;}
    188. }
    189. }
    190. # Endregion
    191. }
    192. }

Xgrid binding)

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.