As devexpress control packages are increasingly used by Chinese users, it is very difficult to read the English version instructions. huidu control network has been researching the use of devexpress control packages for many years, the huidu control network will constantly share its devexpress experience with you.» Below are more devexpress development resources and help documents. We usually collect the four most commonly used devexpress winform code snippets, which are commonly used and hope to help the majority of Dev users.
I,Delete gridcontrol
Private void rilinkeditinfodel_click (Object sender, eventargs e) {If (xtramessagebox. Show ("are you sure you want to delete the current record? "," Warning ", messageboxbuttons. yesno, messageboxicon. warning) = dialogresult. yes) {datarow ROW = gvinfos. getdatarow (gvinfos. focusedrowhandle); delbycode (row ["code"]. tostring (); xtramessagebox. show ("operation successful! ");}}
2. Bind a column in a non-data table
Hashtable ht = new hashtable (); Private void gridview6_customunboundcolumndata (Object sender, devexpress. xtragrid. views. base. customcolumndataeventargs e) {gridview view = sender as gridview; If (E. rowhandle> = 0) {object needalert = view. getrowcellvalue (E. rowhandle, view. columns ["needalert"]); If (needalert! = NULL & needalert! = Dbnull. Value & needalert. tostring (). Trim ()! = "0" & view. getrowcellvalue (E. rowhandle, view. Columns ["value"])! = Dbnull. value) {decimal avervalue = convert. todecimal (view. getrowcellvalue (E. rowhandle, view. columns ["value"]); object minvalue = view. getrowcellvalue (E. rowhandle, view. columns ["minvalue"]); object maxvlaue = view. getrowcellvalue (E. rowhandle, view. columns ["maxvalue"]); If (minvalue! = Dbnull. Value & minvalue! = NULL & maxvlaue. tostring ()! = "" & Maxvlaue! = Dbnull. Value & maxvlaue! = NULL & maxvlaue. tostring ()! = "") {Decimal gridcolumn2 = convert. todecimal (view. getrowcellvalue (E. rowhandle, view. columns ["minvalue"]); decimal gridcolumn1 = convert. todecimal (view. getrowcellvalue (E. rowhandle, view. columns ["maxvalue"]); If (gridcolumn2> avervalue | avervalue> gridcolumn1) {If (! Ht. containskey ("pic") ht. add ("pic", getimage (1); E. value = HT ["pic"] ;}}} /// <summary> /// obtain the image from the resource file /// </Summary> /// <Param name = "key"> </param> // <returns> </returns> byte [] getimage (INT key) {image IMG = devexpress. utils. controls. imagehelper. createimagefromresources (string. format ("riversys.resources.20.02.16.gif", key. tostring (), typeof (riverinfos ). assembly); Return devexpress. xtraeditors. contro Ls. byteimageconverter. tobytearray (IMG, imageformat. GIF );} /// <summary> /// dynamically set the row style based on the condition /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void gridview6_rowstyle (Object sender, devexpress. xtragrid. views. grid. rowstyleeventargs e) {gridview view = sender as gridview; If (E. rowhandle> = 0) {object needalert = view. getrowcellvalue (E. rowhandle, view. columns ["needalert"]); If (needalert! = NULL & needalert! = Dbnull. Value & needalert. tostring (). Trim ()! = "0" & view. getrowcellvalue (E. rowhandle, view. Columns ["value"])! = Dbnull. value) {decimal avervalue = convert. todecimal (view. getrowcellvalue (E. rowhandle, view. columns ["value"]); object minvalue = view. getrowcellvalue (E. rowhandle, view. columns ["minvalue"]); object maxvlaue = view. getrowcellvalue (E. rowhandle, view. columns ["maxvalue"]); If (minvalue! = Dbnull. Value & minvalue! = NULL & maxvlaue. tostring ()! = "" & Maxvlaue! = Dbnull. Value & maxvlaue! = NULL & maxvlaue. tostring ()! = "") {Decimal gridcolumn2 = convert. todecimal (minvalue); decimal gridcolumn1 = convert. todecimal (maxvlaue); If (gridcolumn2> avervalue | avervalue> gridcolumn1) {e. appearance. forecolor = color. red; E. appearance. backcolor = color. lightgray ;}}}}}
III,Color Selection Control in gridcontrol
private void gvMapColor_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e){ GridView view = sender as GridView; DataView dv = view.DataSource as DataView; if (e.IsGetData) { string strVal = dv[e.ListSourceRowIndex]["Color"].ToString(); if (strVal != "") { //e.Value = DevExpress.Utils.StyleLayout.ColorFromString(strVal); e.Value = Common.HexToColor(strVal); } } else { //Color colorVal = DevExpress.Utils.StyleLayout.ColorFromString(e.Value.ToString()); Color colorVal = (Color)e.Value; dv[e.ListSourceRowIndex]["Color"] = Common.RGB_HEX(colorVal.ToArgb()); }}
Iv. gridcontrol verification example
/** // <Summary> // initialize the gridview, bind data /// </Summary> /// <Param name = "parentid"> </param> private void gridviewbinddata (string parentid) {This. gridview1.columns. clear (); this. FDS = areasetupactionhelper. getdsregionbyparentid (parentid); this. gridcarea. datasource = This. FDS. tables [0]. defaultview; this. gridview1.columns ["ID"]. visibleindex =-1; this. gridview1.columns ["childcounts"]. visibleindex =-1; this. grid View1.columns ["reg_id"]. caption = "Region ID"; this. gridview1.columns ["reg_name"]. caption = "region name"; this. gridview1.columns ["parent_id"]. caption = "parent region number"; this. gridview1.columns ["reg_desc"]. caption = "zoning description"; this. gridview1.columns ["parent_id"]. imageindex = 1; this. gridview1.columns ["reg_desc"]. imageindex = 0; repositoryitemtextedit texteditreg_id = new repositoryitemtextedit (); texteditreg_id.mask.editm Ask = parentid + "\ D {2, 3}"; texteditreg_id.mask.masktype = devexpress. xtraeditors. mask. masktype. regular; this. gridview1.columns ["reg_id"]. columnedit = texteditreg_id; this. gridview1.columns ["reg_desc"]. columnedit = new repositoryitemmemoexedit (); treelistnode node = This. treelarea. focusednode. parentnode; string FID = node = NULL? "0": node. getvalue ("regid "). tostring (). trim (); dataset DS = areasetupactionhelper. getdsregionbyparentid (FID); region lookueparent_id = new region (); region (New lookupcolumninfo ("reg_id", 40, "Region ID"); region (New lookupcolumninfo ("reg_name ", 40, "region name"); lookueparent_id.datasource = Ds. tables [0]. defaultview; lookueparent_ I D. valuemember = "reg_id"; lookueparent_id.displaymember = "reg_id"; this. gridview1.columns ["parent_id"]. columnedit = lookueparent_id ;} /** //// <summary> // related handler for gridview cell verification /// </Summary> /// <Param name = "sender"> </ param> // <Param name = "E"> </param> private void gridview1_validatingeditor (Object sender, devexpress. xtraeditors. controls. basecontainervalidateeditoreventargs e) {If (E. valid = false & th Is. gridview1.focusedcolumn. fieldname = "reg_id") {e. errortext = "the Partition Number is invalid! \ N should add 2 ~ to the parent region number ~ 3-Bit Data! ";} If (this. gridview1.focusedcolumn. fieldname = "reg_name") {RegEx Reg = new RegEx (@ "[\ u4e00-\ u9fa5] {1, 20}"); match M = reg. match (E. value. tostring (). trim (); If (M. length! = E. value. tostring (). trim (). length) {e. valid = false; E. errortext = "the partition name should be Chinese characters \ n 1 to 20"; }}} private void gridview1_invalidvalueexception (Object sender, invalidvalueexceptioneventargs e) {If (mydialog. alert ("the content you entered does not comply with the rules \ n do you want to discard the changes you just made to this item? "," The content you edited does not comply with the rules ", messageboxbuttons. yesno, messageboxicon. warning) = dialogresult. yes) {e. predictionmode = predictionmode. ignore; }}/ ** /// <summary> // the handler for the gridview row verification /// </Summary> private void gridview1_validaterow (Object sender, devexpress. xtragrid. views. base. validateroweventargs e) {string regid = This. gridview1.getrowcellvalue (E. rowhandle, "reg_id "). tostring (). trim (); string regname = This. g Ridview1.getrowcellvalue (E. rowhandle, "reg_name "). tostring (). trim (); If (regid. length <1) {e. valid = false; this. gridview1.setcolumnerror (this. gridview1.columns ["reg_id"], "enter the Region ID! ", Devexpress. xtraeditors. dxerrorprovider. errortype. Default); // E. errortext =" the partition name cannot be blank! ";} If (regname. Length <1) {e. Valid = false; this. gridview1.setcolumnerror (this. gridview1.columns [" reg_name "]," the partition name cannot be blank! ", Devexpress. xtraeditors. dxerrorprovider. errortype. default) ;}} private void gridviewincluinvalidrowexception (Object sender, devexpress. xtragrid. views. base. invalidrowexceptioneventargs e) {If (E. rowhandle> = 0) {If (this. gridview1.getrowcellvalue (E. rowhandle, this. gridview1.columns ["reg_id"]). tostring (). trim () = "" | this. gridview1.getrowcellvalue (E. rowhandle, this. gridview1.columns ["reg_name"]). to String (). trim () = "") {If (mydialog. alert ("the content you entered does not comply with the rules \ n do you want to discard the changes you just made to this item? "," The content you edited does not comply with the rules ", messageboxbuttons. yesno, messageboxicon. warning) = dialogresult. yes) {e. predictionmode = predictionmode. ignore;} else {e. predictionmode = predictionmode. noaction; }}else {e. predictionmode = predictionmode. ignore ;}}