dkDataGridView.cs 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786
  1. /*******************************************************************************
  2. * Copyright(c) 2012 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:dkDataGridView.cs
  5. * 2.功能描述:扩展的DataGridView控件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 欧阳涛 2012/06/07 1.00 新建
  9. * 周兴 2012/07/24 1.00 扩展复合型表头和树形结构
  10. *******************************************************************************/
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using System.ComponentModel;
  15. using System.Data;
  16. using System.Diagnostics;
  17. using System.Drawing;
  18. using System.Windows.Forms;
  19. using System.Windows.Forms.VisualStyles;
  20. using Dongke.IBOSS.Framework.DKResources;
  21. using Dongke.IBOSS.Framework.Library;
  22. namespace Dongke.IBOSS.Framework.Controls
  23. {
  24. /// <summary>
  25. /// 剪贴板的种类枚举
  26. /// </summary>
  27. public enum ClipboardType
  28. {
  29. InnerAndDisp,
  30. InnerOnly
  31. }
  32. /// <summary>
  33. /// 过滤类型
  34. /// </summary>
  35. public enum FilterType
  36. {
  37. /// <summary>
  38. /// 全部匹配
  39. /// </summary>
  40. Equal,
  41. /// <summary>
  42. /// 全部不匹配
  43. /// </summary>
  44. NotEqual,
  45. /// <summary>
  46. /// 模糊匹配
  47. /// </summary>
  48. Like,
  49. /// <summary>
  50. /// 不模糊匹配
  51. /// </summary>
  52. NotLike
  53. }
  54. public partial class dkDataGridView : DataGridView
  55. {
  56. #region 枚举类型
  57. /// <summary>
  58. /// CELL的背景色区分
  59. /// </summary>
  60. [Flags]
  61. public enum CellBackColorFlags
  62. {
  63. /// <summary>
  64. /// 无背景色
  65. /// </summary>
  66. None = 0,
  67. /// <summary>
  68. /// 只读背景色
  69. /// </summary>
  70. ReadOnlyBackColor = 1,
  71. /// <summary>
  72. /// 内容有变化的背景色
  73. /// </summary>
  74. ChangesBackColor = 2,
  75. /// <summary>
  76. /// 所有全部背景色
  77. /// </summary>
  78. All = ReadOnlyBackColor | ChangesBackColor
  79. }
  80. /// <summary>
  81. /// 右键菜单选项
  82. /// </summary>
  83. [Flags]
  84. public enum ContextMenuVisibleFlags
  85. {
  86. /// <summary>
  87. /// 无
  88. /// </summary>
  89. None = 0,
  90. /// <summary>
  91. /// 过滤
  92. /// </summary>
  93. Refine = 1,
  94. /// <summary>
  95. /// 输出Excel文件
  96. /// </summary>
  97. FileOut = 2,
  98. /// <summary>
  99. /// 打印
  100. /// </summary>
  101. Print = 3,
  102. /// <summary>
  103. /// 所有菜单
  104. /// </summary>
  105. All = Refine | FileOut
  106. }
  107. #endregion
  108. #region 委托
  109. public delegate void CsvConveringEventHandler(object sender, CsvConvertingEventArgs e);
  110. #endregion
  111. #region 事件
  112. public event CsvConveringEventHandler CsvConverting = null;
  113. public event EventHandler MouseClickOnSpace;
  114. public event EventHandler RowsHeaderClick;
  115. #endregion
  116. #region 成员变量
  117. private ContextMenuStrip _ctxColumnHeader;
  118. private ContextMenuStrip _ctxCell;
  119. private ToolStripMenuItem _mnuFrozen = new ToolStripMenuItem();
  120. private ToolStripRefine _mnuRefine = new ToolStripRefine(); // 条件过滤
  121. private ToolStripMenuItem _mnuFilter = new ToolStripMenuItem(); // 过滤
  122. private ToolStripMenuItem _mnuFilterExcept = new ToolStripMenuItem(); // 除外过滤
  123. private ToolStripMenuItem _mnuUnfilter = new ToolStripMenuItem(); // 清除过滤
  124. private ToolStripMenuItem _mnuOutputFile = new ToolStripMenuItem(); // 输出csv
  125. private ToolStripMenuItem _mnuOutputFileXls = new ToolStripMenuItem(); // 输出excel
  126. private ToolStripMenuItem _mnuPrint = new ToolStripMenuItem(); // 打印
  127. private ToolStripMenuItem _mnuDelete = new ToolStripMenuItem(); // 删除
  128. private ContextMenuVisibleFlags _contextMenuVisibleFlag = ContextMenuVisibleFlags.All;
  129. private Label labelHideFocus; // 取得焦点的Lable
  130. private System.ComponentModel.IContainer components;
  131. private bool _isAllowUserToModifyRows = true;
  132. private bool _isAllowUserToSortRows = true;
  133. private CellBackColorFlags _cellBackColorFlag = CellBackColorFlags.None; // 背景颜色的设定标识
  134. private Color _cellBackColorNochanged = Color.FromArgb(255, 255, 255); // 无变化时Cell的背景颜色
  135. private Color _cellBackColorReadOnly = SystemColors.Control; // 只读时Cell的背景颜色
  136. private Color _cellBackColorAdded = Color.FromArgb(255, 230, 230); // 新建时Cell的背景颜色
  137. private Color _cellBackColorModified = Color.FromArgb(210, 220, 255); // 编辑时Cell的背景颜色
  138. private Color _cellBackColorDeleted = Color.FromArgb(240, 255, 220); // 删除时Cell的背景颜色
  139. private bool _isDispRowCount = true; // 是否显示总行数信息
  140. private int[] _selectCellRowCol = { 0, 0 }; // 选择行的行号和列号
  141. private bool _isFrozen = false; // 冻结·解冻标识
  142. private bool _isChildDGV = false;
  143. private string _viewRowFilterString = string.Empty;
  144. private string _sortOrderColumnName;
  145. private string _sortColumnName = string.Empty;
  146. private SortOrder _sortOrderType = SortOrder.None;
  147. private bool _isClearSelected = false;
  148. private bool _isShowDelete = false;
  149. private bool _isCanDelete = false;
  150. private int _gridRowIndex = 0;
  151. private int _gridColumnindex = 0;
  152. private TreeView[] _columnTreeView;
  153. private ArrayList _columnList = new ArrayList();
  154. private int _cellHeight = 25;
  155. private bool _hscrollRefresh = false;
  156. private int _columnDeep = 1;
  157. private TreeGridNode _root;
  158. private TreeGridColumn _expandableColumn;
  159. internal ImageList _imageList;
  160. private bool _virtualNodes = false;
  161. private bool _isHasNode = false;
  162. private string _nodeColumnName = string.Empty;
  163. private bool _inExpandCollapse = false;
  164. internal bool _inExpandCollapseMouseCapture = false;
  165. private Control hideScrollBarControl;
  166. private bool _showLines = true;
  167. private string _dynamicColumnName = string.Empty;
  168. private string[] _childNodeColumnName = null;
  169. private string[] _childNodeColumnText = null;
  170. private DataTable dt;//绑定的表
  171. private string filter = "";//filter字符串
  172. public bool _isOpenTotalRow = false;
  173. private DataGridViewColumnInfo[] _columnInfos = null;
  174. #endregion
  175. #region 公开属性
  176. [System.ComponentModel.Browsable(true)]
  177. [System.ComponentModel.DefaultValue(false)]
  178. [System.ComponentModel.Description("常选择。")]
  179. public bool IsClearSelected
  180. {
  181. get
  182. {
  183. return _isClearSelected;
  184. }
  185. set
  186. {
  187. _isClearSelected = value;
  188. }
  189. }
  190. [System.ComponentModel.DefaultValue(false)]
  191. [System.ComponentModel.Description("删除标识。")]
  192. public bool IsShowDelete
  193. {
  194. get
  195. {
  196. return _isShowDelete;
  197. }
  198. set
  199. {
  200. _isShowDelete = value;
  201. }
  202. }
  203. [System.ComponentModel.DefaultValue(false)]
  204. [System.ComponentModel.Description("允许删除标识。")]
  205. public bool IsCanDelete
  206. {
  207. get
  208. {
  209. return _isCanDelete;
  210. }
  211. set
  212. {
  213. _isCanDelete = value;
  214. }
  215. }
  216. [System.ComponentModel.Browsable(true)]
  217. [System.ComponentModel.DefaultValue(false)]
  218. [System.ComponentModel.Description("设定或者获取可否允许用户编辑行。")]
  219. public bool AllowUserToModifyRows
  220. {
  221. get
  222. {
  223. return _isAllowUserToModifyRows;
  224. }
  225. set
  226. {
  227. _isAllowUserToModifyRows = value;
  228. }
  229. }
  230. [System.ComponentModel.Browsable(true)]
  231. [System.ComponentModel.DefaultValue(true)]
  232. [System.ComponentModel.Description("设定或者获取可否允许用户排序。")]
  233. public bool AllowUserToSortRows
  234. {
  235. get
  236. {
  237. return _isAllowUserToSortRows;
  238. }
  239. set
  240. {
  241. _isAllowUserToSortRows = value;
  242. foreach (DataGridViewColumn column in this.Columns)
  243. {
  244. column.SortMode =
  245. (_isAllowUserToSortRows) ? DataGridViewColumnSortMode.Automatic :
  246. DataGridViewColumnSortMode.NotSortable;
  247. }
  248. }
  249. }
  250. [System.ComponentModel.Browsable(true)]
  251. [System.ComponentModel.DefaultValue("")]
  252. [System.ComponentModel.Description("设定或者获取排序的列名。")]
  253. public string SortOrderColumnName
  254. {
  255. get
  256. {
  257. return _sortOrderColumnName;
  258. }
  259. set
  260. {
  261. _sortOrderColumnName = value;
  262. }
  263. }
  264. [System.ComponentModel.Browsable(true)]
  265. [System.ComponentModel.DefaultValue(CellBackColorFlags.None)]
  266. [System.ComponentModel.Description("设定或者获取Cell背景色设定标识。")]
  267. public CellBackColorFlags CellBackColorFlag
  268. {
  269. get
  270. {
  271. return _cellBackColorFlag;
  272. }
  273. set
  274. {
  275. _cellBackColorFlag = value;
  276. }
  277. }
  278. [System.ComponentModel.Browsable(true)]
  279. [System.ComponentModel.DefaultValue(typeof(Color), "255, 255, 255")]
  280. [System.ComponentModel.Description("设定或者获取Cell没有改变时的背景颜色。")]
  281. public Color CellBackColorNochanged
  282. {
  283. get
  284. {
  285. return _cellBackColorNochanged;
  286. }
  287. set
  288. {
  289. _cellBackColorNochanged = value;
  290. }
  291. }
  292. [System.ComponentModel.Browsable(true)]
  293. [System.ComponentModel.DefaultValue(typeof(Color), "SystemColors.Control")]
  294. [System.ComponentModel.Description("设定或者获取只读Cell的背景颜色。")]
  295. public Color CellBackColorReadOnly
  296. {
  297. get
  298. {
  299. return _cellBackColorReadOnly;
  300. }
  301. set
  302. {
  303. _cellBackColorReadOnly = value;
  304. }
  305. }
  306. [System.ComponentModel.Browsable(true)]
  307. [System.ComponentModel.DefaultValue(typeof(Color), "255, 230, 230")]
  308. [System.ComponentModel.Description("设定或者获取新建Cell的背景颜色。")]
  309. public Color CellBackColorAdded
  310. {
  311. get
  312. {
  313. return _cellBackColorAdded;
  314. }
  315. set
  316. {
  317. _cellBackColorAdded = value;
  318. }
  319. }
  320. [System.ComponentModel.Browsable(true)]
  321. [System.ComponentModel.DefaultValue(typeof(Color), "210, 220, 255")]
  322. [System.ComponentModel.Description("设定或者获取编辑过Cell的背景颜色。")]
  323. public Color CellBackColorModified
  324. {
  325. get
  326. {
  327. return _cellBackColorModified;
  328. }
  329. set
  330. {
  331. _cellBackColorModified = value;
  332. }
  333. }
  334. [System.ComponentModel.Browsable(true)]
  335. [System.ComponentModel.DefaultValue(typeof(Color), "240, 255, 220")]
  336. [System.ComponentModel.Description("设定或者获取删除的Cell背景颜色。")]
  337. public Color CellBackColorDeleted
  338. {
  339. get
  340. {
  341. return _cellBackColorDeleted;
  342. }
  343. set
  344. {
  345. _cellBackColorDeleted = value;
  346. }
  347. }
  348. [System.ComponentModel.Browsable(true)]
  349. [System.ComponentModel.Description("设定或者获取右键菜单的内容。")]
  350. public ContextMenuVisibleFlags ContextMenuVisible
  351. {
  352. get
  353. {
  354. return _contextMenuVisibleFlag;
  355. }
  356. set
  357. {
  358. _contextMenuVisibleFlag = value;
  359. }
  360. }
  361. /// <summary>
  362. /// 设置输入单元格的颜色
  363. /// </summary>
  364. public bool IsSetInputColumnsColor
  365. {
  366. set
  367. {
  368. if (DataSource != null && !this.ReadOnly && this.Enabled)
  369. {
  370. for (int i = 0; i < this.Rows.Count; i++)
  371. {
  372. if (!this.Rows[i].ReadOnly)
  373. {
  374. for (int j = 0; j < this.Columns.Count; j++)
  375. {
  376. if (!this.Rows[i].Cells[this.Columns[j].Name].ReadOnly)
  377. {
  378. this.Rows[i].Cells[this.Columns[j].Name].Style.BackColor
  379. = ColorTranslator.FromHtml(Constant.ALLOW_MODIFY_AREA_BACKGROUND_COLOR);
  380. }
  381. else
  382. {
  383. this.Rows[i].Cells[this.Columns[j].Name].Style.BackColor = Color.White;
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. }
  391. /// <summary>
  392. /// 设定或者获取当前DataTable
  393. /// </summary>
  394. public DataTable CurrentDataTable
  395. {
  396. get
  397. {
  398. return DataConvert.ToDataTable(this.DataSource, this.DataMember);
  399. }
  400. }
  401. [System.ComponentModel.Browsable(true)]
  402. [System.ComponentModel.DefaultValue(typeof(bool), "true")]
  403. [System.ComponentModel.Description("设定或者获取是否显示行数选项。")]
  404. public bool IsDispRowCount
  405. {
  406. get
  407. {
  408. return _isDispRowCount;
  409. }
  410. set
  411. {
  412. _isDispRowCount = value;
  413. }
  414. }
  415. [Description("设置多维表头时,设置动态列")]
  416. public string DynamicColumnName
  417. {
  418. get
  419. {
  420. return this._dynamicColumnName;
  421. }
  422. set
  423. {
  424. this._dynamicColumnName = value;
  425. }
  426. }
  427. [Description("设置多维表头时,设置的子列名称")]
  428. public string[] ChildNodeColumnName
  429. {
  430. get
  431. {
  432. return this._childNodeColumnName;
  433. }
  434. set
  435. {
  436. this._childNodeColumnName = value;
  437. }
  438. }
  439. [Description("设置多维表头时,设置的子列显示名称")]
  440. public string[] ChildNodeColumnText
  441. {
  442. get
  443. {
  444. return this._childNodeColumnText;
  445. }
  446. set
  447. {
  448. this._childNodeColumnText = value;
  449. }
  450. }
  451. [Description("是否设置节点")]
  452. public bool HasNode
  453. {
  454. get
  455. {
  456. return this._isHasNode;
  457. }
  458. set
  459. {
  460. this._isHasNode = value;
  461. }
  462. }
  463. public new object DataSource
  464. {
  465. get
  466. {
  467. return base.DataSource;
  468. }
  469. set
  470. {
  471. try
  472. {
  473. if (value != null)
  474. {
  475. dt = (DataTable)value;
  476. // 设置列头显示的形式
  477. if (this.RowHeadersVisible)
  478. {
  479. this.RowHeadersWidth = 50;
  480. if (dt.Rows.Count > 999)
  481. {
  482. this.RowHeadersWidth += (dt.Rows.Count.ToString().Length - 3) * 15;
  483. }
  484. }
  485. }
  486. else
  487. {
  488. this.RowHeadersWidth = 50;
  489. // 如果数据源为空,并且是打开了合计行的,需要重新计算合计行的值
  490. if (_isOpenTotalRow && base.DataSource != null)
  491. {
  492. dt = ((DataTable)base.DataSource).Clone();
  493. if (this.ReadOnly && this.AllowUserToAddRows)
  494. {
  495. this.AllowUserToAddRows = false;
  496. }
  497. //CountTotalRow();
  498. }
  499. //if (_isOpenTotalRow && base.DataSource == null)
  500. //{
  501. // CountTotalRow();
  502. //}
  503. if (_isOpenTotalRow)
  504. {
  505. ClearTotalRowData();
  506. }
  507. }
  508. // 根据列名去设置树形结构
  509. if (HasNode && value != null && !string.IsNullOrEmpty(NodeColumnName))
  510. {
  511. this.Nodes.Clear();
  512. DataRow[] dataRows = null;
  513. TreeGridNode node = null;
  514. DataRow[] dataRowsTmp = null;
  515. DataTable dataTable = (DataTable)value;
  516. SortedList<int, string> sortedlist = new SortedList<int, string>();
  517. dataRows = dataTable.Select(" ParentID = 0");
  518. for (int i = 0; i < dataRows.Length; i++)
  519. {
  520. node = this.Nodes.Add(dataRows[i], dataTable.Columns, this.Columns, sortedlist, out sortedlist);
  521. dataRowsTmp = dataTable.Select(" ParentID = " + dataRows[i][NodeColumnName]);
  522. InitNode(dataTable, node, sortedlist, dataRowsTmp);
  523. }
  524. // Control when edit occurs because edit mode shouldn't start when expanding/collapsing
  525. //this.EditMode = DataGridViewEditMode.EditProgrammatically;
  526. this.RowTemplate = new TreeGridNode() as DataGridViewRow;
  527. // This sample does not support adding or deleting rows by the user.
  528. this.AllowUserToAddRows = false;
  529. this.AllowUserToDeleteRows = false;
  530. }
  531. // 动态生成多维表头(目前只支持2维)
  532. else if (!string.IsNullOrEmpty(DynamicColumnName) && value != null
  533. && ChildNodeColumnName != null && ChildNodeColumnName.Length > 0)
  534. {
  535. DataTable dataTable = (DataTable)value;
  536. ArrayList dynamicnames = new ArrayList();
  537. for (int i = 0; i < dataTable.Rows.Count; i++)
  538. {
  539. if (!dynamicnames.Contains(dataTable.Rows[i][DynamicColumnName]))
  540. {
  541. dynamicnames.Add(dataTable.Rows[i][DynamicColumnName]);
  542. }
  543. }
  544. DataTable tmpTable = LoadColumnHeaderTree(dynamicnames);
  545. this.ColumnDeep = 2;
  546. this.ColumnHeadersHeightSizeMode =
  547. DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
  548. this.ScrollBars = ScrollBars.Both;
  549. //base.DataSource = value;
  550. tmpTable = BindData(dataTable, tmpTable, dynamicnames);
  551. this.RowCount = tmpTable.Rows.Count;
  552. base.DataSource = tmpTable;
  553. }
  554. else
  555. {
  556. base.DataSource = value;
  557. this.ClearSelection();
  558. }
  559. // 设置交替行颜色
  560. if (IsOpenMergeCellFlag )
  561. {
  562. this.SetDataGridViewRowColor();
  563. }
  564. }
  565. catch (Exception ex)
  566. {
  567. throw ex;
  568. }
  569. }
  570. }
  571. [Browsable(false)]
  572. [DefaultValue(false)]
  573. public bool IsChildDGV
  574. {
  575. get
  576. {
  577. return _isChildDGV;
  578. }
  579. set
  580. {
  581. _isChildDGV = value;
  582. }
  583. }
  584. [Browsable(false)]
  585. public string ViewRowFilter
  586. {
  587. get
  588. {
  589. return _viewRowFilterString;
  590. }
  591. set
  592. {
  593. _viewRowFilterString = value;
  594. }
  595. }
  596. public int CellHeight
  597. {
  598. get
  599. {
  600. return _cellHeight;
  601. }
  602. set
  603. {
  604. _cellHeight = value;
  605. }
  606. }
  607. public bool RefreshAtHscroll
  608. {
  609. get
  610. {
  611. return _hscrollRefresh;
  612. }
  613. set
  614. {
  615. _hscrollRefresh = value;
  616. }
  617. }
  618. [Description("设置或获得合并表头数的深度")]
  619. public int ColumnDeep
  620. {
  621. get
  622. {
  623. if (this.Columns.Count == 0)
  624. {
  625. _columnDeep = 1;
  626. }
  627. this.ColumnHeadersHeight = _cellHeight * _columnDeep;
  628. return _columnDeep;
  629. }
  630. set
  631. {
  632. if (value < 1)
  633. {
  634. _columnDeep = 1;
  635. }
  636. else
  637. {
  638. _columnDeep = value;
  639. }
  640. this.ColumnHeadersHeight = _cellHeight * _columnDeep;
  641. }
  642. }
  643. [Description("添加合并式单元格绘制的所需要的节点对象")]
  644. public TreeView[] ColumnTreeView
  645. {
  646. get
  647. {
  648. return _columnTreeView;
  649. }
  650. set
  651. {
  652. //if (_columnTreeView != null)
  653. //{
  654. // for (int i = 0; i <=_columnTreeView.Length -1; i++)
  655. // {
  656. // _columnTreeView[i].Dispose();
  657. // }
  658. //}
  659. _columnTreeView = value;
  660. }
  661. }
  662. [Description("设置添加的字段数的相关属性")]
  663. public TreeView ColumnTreeViewNode
  664. {
  665. get
  666. {
  667. if (_columnTreeView != null && _columnTreeView.Length > 0 && _columnTreeView[0] != null)
  668. {
  669. return _columnTreeView[0];
  670. }
  671. return null;
  672. }
  673. }
  674. public ArrayList ColumnList
  675. {
  676. get
  677. {
  678. if (_columnTreeView == null)
  679. {
  680. return null;
  681. }
  682. if (_columnTreeView[0] == null)
  683. {
  684. return null;
  685. }
  686. if (_columnTreeView[0].Nodes == null)
  687. {
  688. return null;
  689. }
  690. if (_columnTreeView[0].Nodes.Count == 0)
  691. {
  692. return null;
  693. }
  694. _columnList.Clear();
  695. GetColumnNodes(_columnList, _columnTreeView[0].Nodes[0], false);
  696. return _columnList;
  697. }
  698. }
  699. public new DataGridViewRowCollection Rows
  700. {
  701. get { return base.Rows; }
  702. }
  703. public new DataGridViewRow RowTemplate
  704. {
  705. get { return base.RowTemplate; }
  706. set { base.RowTemplate = value; }
  707. }
  708. [DefaultValue(true)]
  709. public bool ShowLines
  710. {
  711. get { return this._showLines; }
  712. set
  713. {
  714. if (value != this._showLines)
  715. {
  716. this._showLines = value;
  717. this.Invalidate();
  718. }
  719. }
  720. }
  721. public new int RowCount
  722. {
  723. get
  724. {
  725. return base.RowCount;
  726. }
  727. set
  728. {
  729. base.RowCount = value;
  730. }
  731. }
  732. [Description("是否能导出excel")]
  733. [DefaultValue(true)]
  734. public bool IsExportFlag
  735. {
  736. set;
  737. get;
  738. }
  739. [DefaultValue(true)]
  740. public bool IsProcessRightFlag
  741. {
  742. get;
  743. set;
  744. }
  745. [DefaultValue(false)]
  746. public bool IsSelectGoodsFlag
  747. {
  748. get;
  749. set;
  750. }
  751. /// <summary>
  752. /// 需要进行合计的列,如果没有,就是所有的可以的合计的列都合计
  753. /// </summary>
  754. public List<string> TotalSumColumns
  755. {
  756. get;
  757. set;
  758. }
  759. /// <summary>
  760. /// 是否自动计算合计行,默认是自动
  761. /// </summary>
  762. public bool IsAutoCountSum
  763. {
  764. get;
  765. set;
  766. }
  767. /// <summary>
  768. /// 是否有小计,在有合计行时使用
  769. /// </summary>
  770. public bool IsSubTotalFlag
  771. {
  772. get;
  773. set;
  774. }
  775. /// <summary>
  776. /// 是否点了自适应列宽
  777. /// </summary>
  778. public bool IsAutoResizeColumns
  779. {
  780. get;
  781. set;
  782. }
  783. /// <summary>
  784. /// 是否点击F12隐藏
  785. /// </summary>
  786. public bool IsClickF12
  787. {
  788. get;
  789. set;
  790. }
  791. [Description("设置树形结构时,需要设置关系的主键ID名称")]
  792. public string NodeColumnName
  793. {
  794. get
  795. {
  796. return this._nodeColumnName;
  797. }
  798. set
  799. {
  800. this._nodeColumnName = value;
  801. }
  802. }
  803. public TreeGridNodeCollection Nodes
  804. {
  805. get
  806. {
  807. return this._root.Nodes;
  808. }
  809. }
  810. public TreeGridNode CurrentRowT
  811. {
  812. get
  813. {
  814. return base.CurrentRow as TreeGridNode;
  815. }
  816. }
  817. [Description("Causes nodes to always show as expandable. Use the NodeExpanding event to add nodes.")]
  818. public bool VirtualNodes
  819. {
  820. get { return _virtualNodes; }
  821. set { _virtualNodes = value; }
  822. }
  823. public TreeGridNode CurrentNode
  824. {
  825. get
  826. {
  827. return this.CurrentRowT;
  828. }
  829. }
  830. public ImageList ImageList
  831. {
  832. get { return this._imageList; }
  833. set
  834. {
  835. this._imageList = value;
  836. //TODO: should we invalidate cell styles when setting the image list?
  837. }
  838. }
  839. /// <summary>
  840. /// 用合计的值进行计算值 的列
  841. /// </summary>
  842. public string TotalCountName
  843. {
  844. get;
  845. set;
  846. }
  847. /// <summary>
  848. /// 用合计的值进行计算 的公式
  849. /// </summary>
  850. public string TotalCountFormula
  851. {
  852. get;
  853. set;
  854. }
  855. /// <summary>
  856. /// 需要合并的列(周兴 2015-12-6 修改)
  857. /// </summary>
  858. public List<string> MergeColumnNames
  859. {
  860. get;
  861. set;
  862. }
  863. /// <summary>
  864. /// 需要合并的列(明细,必须在上面属性中也存在,这个属性类似于子属性)(周兴 2015-12-6 修改)
  865. /// </summary>
  866. public List<string> MergeDetailColumnNames
  867. {
  868. get;
  869. set;
  870. }
  871. /// <summary>
  872. /// 合并单元格时需要唯一列(比如SalesID,不同的SalesID的是不允许合并在一起的
  873. /// </summary>
  874. public string MergeOnlyColumn
  875. {
  876. get;
  877. set;
  878. }
  879. /// <summary>
  880. /// 合并单元格时需要唯一列(子列)(比如DetailID,相同SalesID的不同DetailID也是不允许合并的
  881. /// </summary>
  882. public string MergeDetailOnlyColumn
  883. {
  884. get;
  885. set;
  886. }
  887. /// <summary>
  888. /// 合并单元格时需要唯一列(子列)(比如DetailID,相同SalesID的不同DetailID也是不允许合并的
  889. /// </summary>
  890. public string MergeDDetailOnlyColumn
  891. {
  892. get;
  893. set;
  894. }
  895. /// <summary>
  896. /// 需要合并的列(明细,必须在上面属性中也存在,这个属性类似于子属性)(周兴 2015-12-6 修改)
  897. /// </summary>
  898. public List<string> MergeDDetailColumnNames
  899. {
  900. get;
  901. set;
  902. }
  903. /// <summary>
  904. /// 是否开启合并单元格
  905. /// </summary>
  906. public bool IsOpenMergeCellFlag
  907. {
  908. get;
  909. set;
  910. }
  911. /// <summary>
  912. /// 是否打印
  913. /// </summary>
  914. public bool IsPrintFlag
  915. {
  916. get;
  917. set;
  918. }
  919. #endregion
  920. #region 构造函数
  921. public dkDataGridView()
  922. {
  923. InitializeComponent();
  924. this.Tag = true;
  925. IsProcessRightFlag = true;
  926. IsSelectGoodsFlag = false;
  927. IsAutoCountSum = true;
  928. IsExportFlag = true;
  929. IsAutoResizeColumns = false;
  930. IsSubTotalFlag = false;
  931. this.Rows.CollectionChanged +=
  932. new System.ComponentModel.CollectionChangeEventHandler(Rows_CollectionChanged);
  933. this._mnuFrozen.Name = "mnuFrozen";
  934. this._mnuFrozen.Size = new System.Drawing.Size(143, 22);
  935. this._mnuFrozen.Text = DKResources.ControlsTips.DK_DataGridView_Frozen;
  936. this._mnuFrozen.ToolTipText = DKResources.ControlsTips.TOOLTIP_DataGridView_M001;
  937. this._mnuRefine.ToolTipText = DKResources.ControlsTips.TOOLTIP_DataGridView_M002;
  938. this._mnuFilter.Name = "mnuFilter";
  939. this._mnuFilter.Size = new System.Drawing.Size(118, 22);
  940. this._mnuFilter.Text = DKResources.ControlsTips.DK_DataGridView_Filter;
  941. this._mnuFilter.ToolTipText = DKResources.ControlsTips.TOOLTIP_DataGridView_M003;
  942. this._mnuFilterExcept.Name = "mnuFilterExcept";
  943. this._mnuFilterExcept.Size = new System.Drawing.Size(143, 22);
  944. this._mnuFilterExcept.Text = DKResources.ControlsTips.DK_DataGridView_FilterExcept;
  945. this._mnuFilterExcept.ToolTipText = DKResources.ControlsTips.TOOLTIP_DataGridView_M004;
  946. this._mnuUnfilter.Name = "mnuUnfilter";
  947. this._mnuUnfilter.Size = new System.Drawing.Size(143, 22);
  948. this._mnuUnfilter.Text = DKResources.ControlsTips.DK_DataGridView_Unfilter;
  949. this._mnuUnfilter.ToolTipText = DKResources.ControlsTips.TOOLTIP_DataGridView_M005;
  950. this._mnuOutputFile.Name = "mnuOutputFile";
  951. this._mnuOutputFile.Size = new System.Drawing.Size(143, 22);
  952. this._mnuOutputFile.Text = DKResources.ControlsTips.DK_DataGridView_OutputFile;
  953. // 周兴 2016-7-22 增加 输出excel
  954. this._mnuOutputFileXls.Name = "mnuOutputFileXls";
  955. this._mnuOutputFileXls.Size = new System.Drawing.Size(143, 22);
  956. this._mnuOutputFileXls.ToolTipText = DKResources.ControlsTips.TOOLTIP_DataGridView_M008;
  957. this._mnuOutputFileXls.Text = DKResources.ControlsTips.DK_DataGridView_OutputFileXLs;
  958. this._mnuDelete.Name = "mnuDelete";
  959. this._mnuDelete.Size = new System.Drawing.Size(143, 22);
  960. this._mnuDelete.Text = DKResources.ControlsTips.DK_DataGridView_DeleteRow;
  961. this._mnuDelete.ToolTipText = DKResources.ControlsTips.TOOLTIP_DataGridView_M006;
  962. this._mnuPrint.Name = "mnuPrint";
  963. this._mnuPrint.Size = new System.Drawing.Size(143, 22);
  964. this._mnuPrint.Text = DKResources.ControlsTips.DK_DataGridView_Print;
  965. this._mnuPrint.ToolTipText = DKResources.ControlsTips.TOOLTIP_DataGridView_M007;
  966. // 事件
  967. this._mnuRefine.RefineTextFixed += new EventHandler(mnuRefine_RefineTextFixed);
  968. this._mnuFilter.Click += new EventHandler(mnuFilter_Click);
  969. this._mnuFilterExcept.Click += new EventHandler(mnuFilterExcept_Click);
  970. this._mnuUnfilter.Click += new EventHandler(mnuUnfilter_Click);
  971. this._mnuOutputFile.Click += new EventHandler(mnuOutputFile_Click);
  972. this._mnuOutputFileXls.Click += new EventHandler(mnuOutputFileXls_Click);
  973. this._mnuFrozen.Click += new EventHandler(mnuFrozen_Click);
  974. this._mnuDelete.Click += new EventHandler(mnuDelete_Click);
  975. this._mnuPrint.Click += new EventHandler(mnuPrint_Click);
  976. //// Control when edit occurs because edit mode shouldn't start when expanding/collapsing
  977. //this.EditMode = DataGridViewEditMode.EditProgrammatically;
  978. //this.RowTemplate = new TreeGridNode() as DataGridViewRow;
  979. //// This sample does not support adding or deleting rows by the user.
  980. //this.AllowUserToAddRows = false;
  981. //this.AllowUserToDeleteRows = false;
  982. this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  983. this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
  984. this.RowHeadersWidth = 50;
  985. this._root = new TreeGridNode(this);
  986. this._root.IsRoot = true;
  987. //// Ensures that all rows are added unshared by listening to the CollectionChanged event.
  988. //base.Rows.CollectionChanged += delegate(object sender, System.ComponentModel.CollectionChangeEventArgs e) { };
  989. }
  990. #endregion
  991. #region 公开方法/函数
  992. public CurrencyManager GetCurrencyManager()
  993. {
  994. if (this.DataSource == null)
  995. {
  996. return null;
  997. }
  998. return this.BindingContext[this.DataSource, this.DataMember] as CurrencyManager;
  999. }
  1000. public DataTable GetDataTable()
  1001. {
  1002. CurrencyManager bindingManager = GetCurrencyManager();
  1003. if (bindingManager == null)
  1004. {
  1005. return null;
  1006. }
  1007. DataView dataView = bindingManager.List as DataView;
  1008. if (dataView == null)
  1009. {
  1010. return null;
  1011. }
  1012. return dataView.Table;
  1013. }
  1014. public DataRowView GetDataRowView(int viewRowIndex)
  1015. {
  1016. if (viewRowIndex < 0)
  1017. {
  1018. return null;
  1019. }
  1020. if (this.DataSource == null)
  1021. {
  1022. return null;
  1023. }
  1024. try
  1025. {
  1026. CurrencyManager bindingManager = GetCurrencyManager();
  1027. if (bindingManager != null)
  1028. {
  1029. if (viewRowIndex < bindingManager.List.Count)
  1030. {
  1031. return (bindingManager.List[viewRowIndex] as DataRowView);
  1032. }
  1033. }
  1034. }
  1035. catch (Exception ex)
  1036. {
  1037. Debug.Assert(false, "Error", ex.Message);
  1038. }
  1039. return null;
  1040. }
  1041. public DataRow GetDataRow(int viewRowIndex)
  1042. {
  1043. DataRowView rowView = GetDataRowView(viewRowIndex);
  1044. if (rowView == null)
  1045. {
  1046. return null;
  1047. }
  1048. if (rowView.Row == null)
  1049. {
  1050. return null;
  1051. }
  1052. return rowView.Row;
  1053. }
  1054. public int GetDataRowIndex(int viewRowIndex)
  1055. {
  1056. DataRow row = GetDataRow(viewRowIndex);
  1057. if (row == null)
  1058. {
  1059. return -1;
  1060. }
  1061. return row.Table.Rows.IndexOf(row);
  1062. }
  1063. public int GetViewColumnIndex(string dataColumnName)
  1064. {
  1065. for (int i = 0; i < this.Columns.Count; i++)
  1066. {
  1067. DataGridViewColumn column = this.Columns[i];
  1068. string dataPropartyName = column.DataPropertyName;
  1069. if (dataColumnName.Equals(dataPropartyName))
  1070. {
  1071. return i;
  1072. }
  1073. }
  1074. return -1;
  1075. }
  1076. public void OutputFile()
  1077. {
  1078. OutputFile(true);
  1079. }
  1080. public void SetFrozen()
  1081. {
  1082. int selectRow = _gridRowIndex;
  1083. int selectCol = _gridColumnindex;
  1084. bool isSelectSameCell = true;
  1085. // 清除冻结
  1086. if (0 < this.Rows.Count)
  1087. {
  1088. this.Rows[0].Frozen = false;
  1089. }
  1090. if (0 < this.Columns.Count)
  1091. {
  1092. this.Columns[0].Frozen = false;
  1093. }
  1094. // 是否选择了同一个单元格
  1095. isSelectSameCell = IsSelectSameCell(selectRow, selectCol);
  1096. if (isSelectSameCell && _isFrozen)
  1097. {
  1098. _isFrozen = false;
  1099. return;
  1100. }
  1101. _isFrozen = false;
  1102. if (0 < selectRow)
  1103. {
  1104. // 行冻结处理
  1105. this.Rows[selectRow - 1].Frozen = true;
  1106. _isFrozen = true;
  1107. }
  1108. if (0 < selectCol)
  1109. {
  1110. // 列冻结处理
  1111. //this.Columns[selectCol - 1].Frozen = true;
  1112. if (this.ColumnTreeViewNode != null && this.ColumnTreeViewNode.Nodes.Count > 0 && _columnDeep > 1)
  1113. {
  1114. // 说明是多维表头
  1115. foreach (TreeNode node in this.ColumnTreeViewNode.Nodes)
  1116. {
  1117. if (this.Columns[selectCol].Name.Equals(node.Name))
  1118. {
  1119. this.Columns[node.Index].Frozen = true;
  1120. break;
  1121. }
  1122. else
  1123. {
  1124. if (node.Nodes != null)
  1125. {
  1126. foreach (TreeNode childNode in node.Nodes)
  1127. {
  1128. if (this.Columns[selectCol].Name.Equals(childNode.Name))
  1129. {
  1130. this.Columns[node.LastNode.Name].Frozen = true;
  1131. return;
  1132. }
  1133. }
  1134. }
  1135. }
  1136. }
  1137. }
  1138. else
  1139. {
  1140. foreach (DataGridViewColumn column in this.Columns)
  1141. {
  1142. if (selectCol == column.Index)
  1143. {
  1144. this.Columns[column.DisplayIndex - 1].Frozen = true;
  1145. break;
  1146. }
  1147. }
  1148. }
  1149. if (_isOpenTotalRow)
  1150. {
  1151. _columnInfos = GetDataGridViewColumnInfos();
  1152. if (_columnInfos != null)
  1153. {
  1154. this.HorizontalScrollingOffset = 0;
  1155. for (int i = 0; i < _columnInfos.Length; i++)
  1156. {
  1157. if (_columnInfos[i].Visible)
  1158. {
  1159. NewText(_columnInfos[i].DataPropertyName, i);
  1160. }
  1161. }
  1162. CountTotalRow();
  1163. }
  1164. }
  1165. _isFrozen = true;
  1166. }
  1167. // 当前选择的行号和列号
  1168. _selectCellRowCol[0] = selectRow;
  1169. _selectCellRowCol[1] = selectCol;
  1170. }
  1171. public void OutputFile(bool isAllCells)
  1172. {
  1173. try
  1174. {
  1175. SaveFileDialog dialog = new SaveFileDialog();
  1176. dialog.Filter = "csv file (*.csv)|*.csv";
  1177. if (dialog.ShowDialog() == DialogResult.OK)
  1178. {
  1179. string csv = ToCsvString(true, false, true, isAllCells);
  1180. if (!string.IsNullOrEmpty(csv))
  1181. {
  1182. System.IO.FileInfo fileInfo = new System.IO.FileInfo(dialog.FileName);
  1183. System.IO.FileStream fileStream = fileInfo.Create();
  1184. Byte[] buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(csv);
  1185. fileStream.Write(buffer, 0, buffer.Length);
  1186. fileStream.Dispose();
  1187. }
  1188. }
  1189. }
  1190. catch (Exception ex)
  1191. {
  1192. //
  1193. }
  1194. }
  1195. /// <summary>
  1196. /// 导出excel
  1197. /// </summary>
  1198. public void OutputFileXls()
  1199. {
  1200. try
  1201. {
  1202. string sheetName = string.Empty;
  1203. string fileName = string.Empty;
  1204. bool flag = true;
  1205. Control control = this.Parent;
  1206. while (flag)
  1207. {
  1208. if (control != null && control is TabPage)
  1209. {
  1210. sheetName = control.Text;
  1211. }
  1212. if (control != null && control is Form)
  1213. {
  1214. fileName = control.Text;
  1215. if (string.IsNullOrEmpty(sheetName))
  1216. {
  1217. sheetName = fileName;
  1218. }
  1219. flag = false;
  1220. }
  1221. control = control.Parent;
  1222. if (control == null)
  1223. {
  1224. flag = false;
  1225. }
  1226. }
  1227. TreeView tv = null;
  1228. if (_columnTreeView != null && _columnTreeView.Length > 0)
  1229. {
  1230. tv = _columnTreeView[0];
  1231. }
  1232. Common.Instance.ExportExcel(this, tv, null, fileName, fileName, this.Font, "", this.Font, "", sheetName, true, true);
  1233. }
  1234. catch (Exception ex)
  1235. {
  1236. //
  1237. }
  1238. }
  1239. public string GetColumnIndex(int columnIndex)
  1240. {
  1241. if (_columnInfos == null)
  1242. {
  1243. _columnInfos = GetDataGridViewColumnInfos();
  1244. }
  1245. if (columnIndex > _columnInfos.Length -1 )
  1246. {
  1247. return string.Empty;
  1248. }
  1249. return _columnInfos[columnIndex].DataPropertyName;
  1250. }
  1251. public string ToCsvString(bool isHeaderFirstRow, bool isTsv,
  1252. bool isFormattedValue, bool isAllCells)
  1253. {
  1254. if (this.CurrentCell != null)
  1255. {
  1256. if (this.CurrentCell.IsInEditMode && !(
  1257. this.CurrentCell is DataGridViewCheckBoxCell))
  1258. {
  1259. return null;
  1260. }
  1261. }
  1262. if (CurrentDataTable == null)
  1263. {
  1264. return null;
  1265. }
  1266. DataTable table = new DataTable();
  1267. List<int> columnIndexList;
  1268. int rowIndexMin, rowIndexMax;
  1269. DataGridViewColumnInfo[] columnInfo = null;
  1270. if (isAllCells)
  1271. {
  1272. columnIndexList = new List<int>();
  1273. columnInfo = this.GetDataGridViewColumnInfos();
  1274. //for (int i = 0; i < columnInfo.Length; i++)
  1275. //{
  1276. // columnIndexList.Add(i);
  1277. //}
  1278. //for (int i = 0; i < this.Columns.Count; i++)
  1279. //{
  1280. // if (this.Columns[i].Visible)
  1281. // {
  1282. // columnIndexList.Add(this.Columns[i].DisplayIndex);
  1283. // }
  1284. //}
  1285. rowIndexMin = 0;
  1286. rowIndexMax = this.Rows.Count - 1;
  1287. if (this.AllowUserToAddRows)
  1288. {
  1289. rowIndexMax -= 1;
  1290. }
  1291. }
  1292. else
  1293. {
  1294. GetSelectedRange(out columnIndexList, out rowIndexMin, out rowIndexMax);
  1295. }
  1296. List<int> invalidColumnList = new List<int>();
  1297. //foreach (int columnIndex in columnIndexList)
  1298. //{
  1299. // string columnName = this.Columns[columnIndex].Name;
  1300. // DataGridViewColumn column = this.Columns[columnName];
  1301. // if (column == null)
  1302. // {
  1303. // invalidColumnList.Add(columnIndex);
  1304. // continue;
  1305. // }
  1306. // if (!isFormattedValue && column.CellType.Name ==
  1307. // "DataGridViewSearchFileBoxCellEx")
  1308. // {
  1309. // invalidColumnList.Add(columnIndex);
  1310. // continue;
  1311. // }
  1312. // DataColumn dcolColumn = new DataColumn(column.Name);
  1313. // dcolColumn.Caption = column.HeaderText;
  1314. // table.Columns.Add(dcolColumn);
  1315. //}
  1316. //foreach (int columnIndex in invalidColumnList)
  1317. //{
  1318. // columnIndexList.Remove(columnIndex);
  1319. //}
  1320. if (columnInfo != null)
  1321. {
  1322. for (int i = 0; i < columnInfo.Length; i++)
  1323. {
  1324. DataColumn dcolColumn = new DataColumn(columnInfo[i].ColumnName);
  1325. dcolColumn.Caption = columnInfo[i].TreeColumnText;
  1326. table.Columns.Add(dcolColumn);
  1327. }
  1328. }
  1329. DataRow drowRow;
  1330. for (int i = rowIndexMin; i <= rowIndexMax; i++)
  1331. {
  1332. drowRow = table.NewRow();
  1333. DataGridViewRow rowView = this.Rows[i];
  1334. if (rowView != null)
  1335. {
  1336. int j = 0;
  1337. for (int k = 0; k < columnInfo.Length; k++)
  1338. {
  1339. DataGridViewCell cell = rowView.Cells[columnInfo[k].ColumnName];
  1340. object value;
  1341. if (isFormattedValue)
  1342. {
  1343. value = cell.FormattedValue;
  1344. }
  1345. else
  1346. {
  1347. value = cell.Value;
  1348. }
  1349. // 周兴 2015-11-19 如果前几个都是0,导出csv就会消失
  1350. if ("String".Equals(cell.ValueType.Name) && (cell.Value + "").StartsWith("0"))
  1351. {
  1352. value = ((char)(9)).ToString() + value;
  1353. }
  1354. //周兴 2016-3-22 修改
  1355. if ("String".Equals(cell.ValueType.Name) && (cell.Value + "").IndexOf("-") > 0)
  1356. {
  1357. value = ((char)(9)).ToString() + value;
  1358. }
  1359. //if (!string.IsNullOrEmpty(value + "") && value.ToString().StartsWith("-"))
  1360. //{
  1361. // value = "'" + value;
  1362. //}
  1363. CsvConvertingEventArgs e = new CsvConvertingEventArgs(k, value);
  1364. OnCsvCoverting(e);
  1365. drowRow[j] = e.Value;
  1366. j++;
  1367. }
  1368. table.Rows.Add(drowRow);
  1369. }
  1370. }
  1371. // 周兴 2015-11-24 如果有合计把合计行也显示进来
  1372. if (_isOpenTotalRow)
  1373. {
  1374. try
  1375. {
  1376. drowRow = table.NewRow();
  1377. TextBox tempTB;
  1378. bool flag = false;
  1379. for (int k = 0; k < columnInfo.Length; k++)
  1380. {
  1381. tempTB = findTextBox("txt" + columnInfo[k].DataPropertyName);
  1382. if (tempTB != null && table.Columns.Contains(columnInfo[k].DataPropertyName))
  1383. {
  1384. if (!flag && "".Equals(tempTB.Text))
  1385. {
  1386. drowRow[columnInfo[k].DataPropertyName] = "合计";
  1387. flag = true;
  1388. }
  1389. else
  1390. {
  1391. drowRow[columnInfo[k].DataPropertyName] = tempTB.Text;
  1392. }
  1393. }
  1394. }
  1395. table.Rows.Add(drowRow);
  1396. }
  1397. catch (Exception ex)
  1398. {
  1399. }
  1400. }
  1401. // DataTable情報をcsv形式に変換
  1402. Char delim = ',';
  1403. if (isTsv)
  1404. {
  1405. delim = '\t';
  1406. }
  1407. return CsvManager.ToString(table, isHeaderFirstRow, delim, '\"');
  1408. }
  1409. /// <summary>
  1410. /// 绘制合并表头
  1411. /// </summary>
  1412. /// <param name="node">合并表头节点</param>
  1413. /// <param name="e">绘图参数集</param>
  1414. /// <param name="level">节点深度</param>
  1415. public void PaintUnitHeader(TreeNode node, DataGridViewCellPaintingEventArgs e, int level)
  1416. {
  1417. // 根节点退出递归调用
  1418. if (level == 0)
  1419. {
  1420. return;
  1421. }
  1422. RectangleF uhRectangle;
  1423. int uhWidth;
  1424. using (
  1425. SolidBrush gridBrush = new SolidBrush(this.GridColor),
  1426. backColorBrush = new SolidBrush(e.CellStyle.BackColor))
  1427. {
  1428. using (Pen gridLinePen = new Pen(gridBrush))
  1429. {
  1430. StringFormat textFormat = new StringFormat();
  1431. textFormat.Alignment = StringAlignment.Center;
  1432. uhWidth = GetUnitHeaderWidth(node);
  1433. if (node.Nodes.Count == 0)
  1434. {
  1435. uhRectangle = new RectangleF(e.CellBounds.Left, e.CellBounds.Top + node.Level * _cellHeight,
  1436. uhWidth - 1, _cellHeight * (_columnDeep - node.Level) - 1);
  1437. }
  1438. else
  1439. {
  1440. uhRectangle = new RectangleF(e.CellBounds.Left, e.CellBounds.Top + node.Level * _cellHeight,
  1441. uhWidth - 1, _cellHeight - 1);
  1442. }
  1443. // 画矩形
  1444. e.Graphics.FillRectangle(backColorBrush, uhRectangle);
  1445. // 画底线
  1446. e.Graphics.DrawLine(gridLinePen, uhRectangle.Left,
  1447. uhRectangle.Bottom, uhRectangle.Right, uhRectangle.Bottom);
  1448. // 画右端线
  1449. e.Graphics.DrawLine(gridLinePen, uhRectangle.Right,
  1450. uhRectangle.Top, uhRectangle.Right, uhRectangle.Bottom);
  1451. if (!string.IsNullOrEmpty(node.Text))
  1452. {
  1453. float tmpWidth = uhRectangle.Width / 2;
  1454. if (this.Parent != null)
  1455. {
  1456. if (uhRectangle.Width - this.HorizontalScrollingOffset > this.Parent.Width)
  1457. {
  1458. tmpWidth = (this.Parent.Width - uhRectangle.Left) / 2;
  1459. }
  1460. else
  1461. {
  1462. tmpWidth = uhRectangle.Width / 2;
  1463. }
  1464. }
  1465. e.Graphics.DrawString(node.Text, this.Font, new SolidBrush(e.CellStyle.ForeColor),
  1466. uhRectangle.Left + tmpWidth
  1467. - e.Graphics.MeasureString(node.Text, this.Font).Width / 2 - 1,
  1468. uhRectangle.Top + uhRectangle.Height / 2
  1469. - e.Graphics.MeasureString(node.Text, this.Font).Height / 2);
  1470. }
  1471. if (node.PrevNode == null)
  1472. {
  1473. if (node.Parent != null)
  1474. {
  1475. PaintUnitHeader(node.Parent, e, level - 1);
  1476. }
  1477. }
  1478. // 周兴 20141116 修改
  1479. else
  1480. {
  1481. if (node != null && node.Parent != null)
  1482. {
  1483. // 最后一个节点才去判断父节点是否已经重绘
  1484. if (node.Parent.LastNode.Name.Equals(node.Name))
  1485. {
  1486. string name = node.Parent.FirstNode.Name;
  1487. if (this.Columns.Contains(name) && !this.Columns[name].Visible)
  1488. {
  1489. PaintUnitHeader(node.Parent, e, level - 1);
  1490. }
  1491. }
  1492. }
  1493. }
  1494. }
  1495. }
  1496. }
  1497. #endregion
  1498. #region 私有方法
  1499. /// <summary>
  1500. /// 设置列表的背景颜色
  1501. /// </summary>
  1502. public void SetDataGridViewRowColor()
  1503. {
  1504. try
  1505. {
  1506. // 只有开启了合并行才行
  1507. if (IsOpenMergeCellFlag && !string.IsNullOrEmpty(MergeOnlyColumn))
  1508. {
  1509. List<string> rowColor = new List<string>();
  1510. rowColor.Add(Constant.OA_COLOR_WHITE);
  1511. rowColor.Add(Constant.OA_COLOR_GREY);
  1512. int id = 0;
  1513. int j = 0;
  1514. for (int i = 0; i < this.Rows.Count; i++)
  1515. {
  1516. if (!string.IsNullOrEmpty(this.Rows[i].Cells[MergeOnlyColumn].Value + ""))
  1517. {
  1518. if (i == 0)
  1519. {
  1520. id = Convert.ToInt32(this.Rows[i].Cells[MergeOnlyColumn].Value);
  1521. }
  1522. else
  1523. {
  1524. if (id != Convert.ToInt32(this.Rows[i].Cells[MergeOnlyColumn].Value))
  1525. {
  1526. if (j == 0)
  1527. {
  1528. j = 1;
  1529. }
  1530. else
  1531. {
  1532. j = 0;
  1533. }
  1534. }
  1535. id = Convert.ToInt32(this.Rows[i].Cells[MergeOnlyColumn].Value);
  1536. }
  1537. this.Rows[i].DefaultCellStyle.BackColor = ColorTranslator.FromHtml(rowColor[j]);
  1538. this.Rows[i].Cells[MergeOnlyColumn].Tag = rowColor[j];
  1539. }
  1540. }
  1541. }
  1542. }
  1543. catch (Exception ex)
  1544. {
  1545. throw ex;
  1546. }
  1547. }
  1548. private DataTable BindData(DataTable dataTable, DataTable tmpTable, ArrayList dynamicnames)
  1549. {
  1550. for (int i = 0; i < dataTable.Rows.Count; i++)
  1551. {
  1552. DataRow row = null;
  1553. if (tmpTable.Rows.Count > 0)
  1554. {
  1555. for (int j = 0; j < tmpTable.Rows.Count; j++)
  1556. {
  1557. bool isNewRow = true;
  1558. for (int t = 0; t < dataTable.Columns.Count; t++)
  1559. {
  1560. if (tmpTable.Columns.Contains(dataTable.Columns[t].ColumnName))
  1561. {
  1562. if (tmpTable.Rows[j][dataTable.Columns[t].ColumnName]
  1563. == dataTable.Rows[i][dataTable.Columns[t].ColumnName])
  1564. {
  1565. isNewRow = false;
  1566. }
  1567. else
  1568. {
  1569. isNewRow = true;
  1570. break;
  1571. }
  1572. }
  1573. }
  1574. if (!isNewRow)
  1575. {
  1576. row = tmpTable.Rows[j];
  1577. }
  1578. }
  1579. if (row == null)
  1580. {
  1581. row = tmpTable.NewRow();
  1582. SetDataToTable(row, i, dataTable, dynamicnames);
  1583. tmpTable.Rows.Add(row);
  1584. }
  1585. else
  1586. {
  1587. SetDataToTable(row, i, dataTable, dynamicnames);
  1588. }
  1589. }
  1590. else
  1591. {
  1592. row = tmpTable.NewRow();
  1593. SetDataToTable(row, i, dataTable, dynamicnames);
  1594. tmpTable.Rows.Add(row);
  1595. }
  1596. }
  1597. return tmpTable;
  1598. }
  1599. private void SetDataToTable(DataRow row, int i, DataTable dataTable, ArrayList dynamicnames)
  1600. {
  1601. for (int t = 0; t < dataTable.Columns.Count; t++)
  1602. {
  1603. if (this.Columns.Contains(dataTable.Columns[t].ColumnName))
  1604. {
  1605. row[dataTable.Columns[t].ColumnName] =
  1606. dataTable.Rows[i][dataTable.Columns[t].ColumnName];
  1607. }
  1608. else
  1609. {
  1610. if (!DynamicColumnName.Equals(dataTable.Columns[t].ColumnName))
  1611. {
  1612. for (int j = 0; j < dynamicnames.Count; j++)
  1613. {
  1614. if (dynamicnames[j].Equals(dataTable.Rows[i][DynamicColumnName]))
  1615. {
  1616. row[dataTable.Columns[t].ColumnName + (j + 1)]
  1617. = dataTable.Rows[i][dataTable.Columns[t].ColumnName];
  1618. }
  1619. }
  1620. }
  1621. }
  1622. }
  1623. }
  1624. private DataTable LoadColumnHeaderTree(ArrayList dynamicnames)
  1625. {
  1626. TreeView treeview = new TreeView();
  1627. DataGridViewColumnCollection tempColumns = this.Columns;
  1628. DataTable tmpTable = new DataTable();
  1629. for (int i = 0; i < tempColumns.Count; i++)
  1630. {
  1631. if (DynamicColumnName.Equals(tempColumns[i].Name))
  1632. {
  1633. int x = 0;
  1634. for (int j = 0; j < dynamicnames.Count; j++)
  1635. {
  1636. TreeNode rootNode = new TreeNode(dynamicnames[j] + "");
  1637. rootNode.Text = dynamicnames[j] + "";
  1638. rootNode.Name = tempColumns[j].Name;
  1639. treeview.Nodes.Add(rootNode);
  1640. for (int t = 0; t < ChildNodeColumnName.Length; t++)
  1641. {
  1642. x++;
  1643. DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
  1644. column.Name = ChildNodeColumnName[t] + (j + 1);
  1645. column.DataPropertyName = ChildNodeColumnName[t] + (j + 1);
  1646. column.HeaderText = ChildNodeColumnText[t];
  1647. this.Columns.Insert(i + x, column);
  1648. tmpTable.Columns.Add(new DataColumn(column.Name));
  1649. TreeNode node = new TreeNode(ChildNodeColumnName[t]);
  1650. node.Text = ChildNodeColumnText[t];
  1651. node.Name = ChildNodeColumnName[t];
  1652. rootNode.Nodes.Add(node);
  1653. }
  1654. }
  1655. this.Columns.Remove(DynamicColumnName);
  1656. }
  1657. else
  1658. {
  1659. if (!tmpTable.Columns.Contains(tempColumns[i].Name))
  1660. {
  1661. tmpTable.Columns.Add(new DataColumn(tempColumns[i].Name));
  1662. TreeNode rootNode = new TreeNode(tempColumns[i].Name);
  1663. rootNode.Text = this.Columns[i].HeaderText;
  1664. rootNode.Name = tempColumns[i].Name;
  1665. treeview.Nodes.Add(rootNode);
  1666. }
  1667. }
  1668. }
  1669. this.ColumnTreeView = new TreeView[] { treeview };
  1670. return tmpTable;
  1671. }
  1672. /// <summary>
  1673. /// 获得合并标题字段的宽度
  1674. /// </summary>
  1675. /// <param name="node"></param>
  1676. /// <returns></returns>
  1677. private int GetUnitHeaderWidth(TreeNode node)
  1678. {
  1679. int uhWidth = 0;
  1680. if (node.Nodes == null || node.Nodes.Count == 0)
  1681. {
  1682. return this.Columns[GetColumnListNodeIndex(node)].Width;
  1683. }
  1684. for (int i = 0; i <= node.Nodes.Count - 1; i++)
  1685. {
  1686. //if (this.Columns[node.Nodes[i].Name].Visible)
  1687. //{
  1688. uhWidth = uhWidth + GetUnitHeaderWidth(node.Nodes[i]);
  1689. //}
  1690. }
  1691. return uhWidth;
  1692. }
  1693. /// <summary>
  1694. /// 获得底层字段索引
  1695. /// </summary>
  1696. /// <param name="node"></param>
  1697. /// <returns></returns>
  1698. private int GetColumnListNodeIndex(TreeNode node)
  1699. {
  1700. for (int i = 0; i <= _columnList.Count - 1; i++)
  1701. {
  1702. if (((TreeNode)_columnList[i]).Equals(node))
  1703. {
  1704. return i;
  1705. }
  1706. }
  1707. return -1;
  1708. }
  1709. private void GetColumnNodes(ArrayList alList, TreeNode node, bool isChecked)
  1710. {
  1711. if (!isChecked)
  1712. {
  1713. if (node.FirstNode == null)
  1714. {
  1715. alList.Add(node);
  1716. if (node.NextNode != null)
  1717. {
  1718. GetColumnNodes(alList, node.NextNode, false);
  1719. return;
  1720. }
  1721. if (node.Parent != null)
  1722. {
  1723. GetColumnNodes(alList, node.Parent, true);
  1724. return;
  1725. }
  1726. }
  1727. else
  1728. {
  1729. if (node.FirstNode != null)
  1730. {
  1731. GetColumnNodes(alList, node.FirstNode, false);
  1732. return;
  1733. }
  1734. }
  1735. }
  1736. else
  1737. {
  1738. if (node.FirstNode == null)
  1739. {
  1740. return;
  1741. }
  1742. if (node.NextNode != null)
  1743. {
  1744. GetColumnNodes(alList, node.NextNode, false);
  1745. return;
  1746. }
  1747. if (node.Parent != null)
  1748. {
  1749. GetColumnNodes(alList, node.Parent, true);
  1750. return;
  1751. }
  1752. }
  1753. }
  1754. /// <summary>
  1755. /// 画单元格(合并单元格 周兴 2015-12-6 增加)
  1756. /// </summary>
  1757. /// <param name="e"></param>
  1758. private void DrawCell(DataGridViewCellPaintingEventArgs e)
  1759. {
  1760. // 只有开启的才允许
  1761. if (!this.IsOpenMergeCellFlag || this.MergeColumnNames == null || this.MergeColumnNames.Count == 0
  1762. || string.IsNullOrEmpty(MergeOnlyColumn) || !this.Columns.Contains(MergeOnlyColumn) || this.CurrentRow == null)
  1763. {
  1764. return;
  1765. }
  1766. if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet)
  1767. {
  1768. e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  1769. }
  1770. Brush gridBrush = new SolidBrush(this.GridColor);
  1771. SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
  1772. SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
  1773. int cellwidth;
  1774. //上面相同的行数
  1775. int upRows = 0;
  1776. //下面相同的行数
  1777. int downRows = 0;
  1778. //总行数
  1779. int count = 0;
  1780. bool deilFlag = false;
  1781. bool isSeleted = false;
  1782. int upHeight = 0;
  1783. int downHeight = 0;
  1784. bool dDetailFlag = false;
  1785. if (MergeDetailColumnNames != null && MergeDetailColumnNames.Count > 0 && !string.IsNullOrEmpty(MergeDetailOnlyColumn))
  1786. {
  1787. deilFlag = true;
  1788. }
  1789. if (MergeDDetailColumnNames != null && MergeDDetailColumnNames.Count > 0 && !string.IsNullOrEmpty(MergeDDetailOnlyColumn))
  1790. {
  1791. dDetailFlag = true;
  1792. }
  1793. if (this.MergeColumnNames.Contains(this.Columns[e.ColumnIndex].Name) && e.RowIndex != -1)
  1794. {
  1795. cellwidth = e.CellBounds.Width;
  1796. Pen gridLinePen = new Pen(gridBrush);
  1797. string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
  1798. string mergeOnlyColumnName = this.Rows[e.RowIndex].Cells[MergeOnlyColumn].Value + "";
  1799. string mergeDetailOnlyColumnName = string.Empty;
  1800. if (!string.IsNullOrEmpty(MergeDetailOnlyColumn))
  1801. {
  1802. mergeDetailOnlyColumnName = this.Rows[e.RowIndex].Cells[MergeDetailOnlyColumn].Value + "";
  1803. }
  1804. string mergeDDetailOnlyColumnName = string.Empty;
  1805. if (!string.IsNullOrEmpty(MergeDDetailOnlyColumn))
  1806. {
  1807. mergeDDetailOnlyColumnName = this.Rows[e.RowIndex].Cells[MergeDDetailOnlyColumn].Value + "";
  1808. }
  1809. string curSelected = this.CurrentRow.Cells[e.ColumnIndex].Value == null ? "" : this.CurrentRow.Cells[e.ColumnIndex].Value.ToString().Trim();
  1810. //if (!string.IsNullOrEmpty(curValue))
  1811. //{
  1812. #region 获取下面的行数
  1813. for (int i = e.RowIndex; i < this.Rows.Count; i++)
  1814. {
  1815. if ((this.Rows[i].Cells[e.ColumnIndex].Value + "").Equals(curValue) && mergeOnlyColumnName.Equals(this.Rows[i].Cells[MergeOnlyColumn].Value + ""))
  1816. {
  1817. if (dDetailFlag && MergeDDetailColumnNames.Contains(this.Columns[e.ColumnIndex].Name))
  1818. {
  1819. if (mergeDDetailOnlyColumnName.Equals(this.Rows[i].Cells[MergeDDetailOnlyColumn].Value + ""))
  1820. {
  1821. downRows++;
  1822. isSeleted = isSeleted || this.Rows[i].Selected;
  1823. downHeight += this.Rows[i].Height;
  1824. if (e.RowIndex != i)
  1825. {
  1826. cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
  1827. }
  1828. }
  1829. else
  1830. {
  1831. break;
  1832. }
  1833. }
  1834. else if (deilFlag && MergeDetailColumnNames.Contains(this.Columns[e.ColumnIndex].Name))
  1835. {
  1836. if (mergeDetailOnlyColumnName.Equals(this.Rows[i].Cells[MergeDetailOnlyColumn].Value + ""))
  1837. {
  1838. downRows++;
  1839. isSeleted = isSeleted || this.Rows[i].Selected;
  1840. downHeight += this.Rows[i].Height;
  1841. if (e.RowIndex != i)
  1842. {
  1843. cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
  1844. }
  1845. }
  1846. else
  1847. {
  1848. break;
  1849. }
  1850. }
  1851. //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
  1852. else
  1853. {
  1854. downRows++;
  1855. isSeleted = isSeleted || this.Rows[i].Selected;
  1856. downHeight += this.Rows[i].Height;
  1857. if (e.RowIndex != i)
  1858. {
  1859. cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
  1860. }
  1861. }
  1862. }
  1863. else
  1864. {
  1865. break;
  1866. }
  1867. }
  1868. #endregion
  1869. #region 获取上面的行数
  1870. for (int i = e.RowIndex; i >= 0; i--)
  1871. {
  1872. if ((this.Rows[i].Cells[e.ColumnIndex].Value + "").Equals(curValue) && mergeOnlyColumnName.Equals(this.Rows[i].Cells[MergeOnlyColumn].Value + ""))
  1873. {
  1874. if (dDetailFlag && MergeDDetailColumnNames.Contains(this.Columns[e.ColumnIndex].Name))
  1875. {
  1876. if (mergeDDetailOnlyColumnName.Equals(this.Rows[i].Cells[MergeDDetailOnlyColumn].Value + ""))
  1877. {
  1878. upRows++;
  1879. isSeleted = isSeleted || this.Rows[i].Selected;
  1880. upHeight += this.Rows[i].Height;
  1881. if (e.RowIndex != i)
  1882. {
  1883. cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
  1884. }
  1885. }
  1886. else
  1887. {
  1888. break;
  1889. }
  1890. }
  1891. else if (deilFlag && MergeDetailColumnNames.Contains(this.Columns[e.ColumnIndex].Name))
  1892. {
  1893. if (mergeDetailOnlyColumnName.Equals(this.Rows[i].Cells[MergeDetailOnlyColumn].Value + ""))
  1894. {
  1895. upRows++;
  1896. isSeleted = isSeleted || this.Rows[i].Selected;
  1897. upHeight += this.Rows[i].Height;
  1898. if (e.RowIndex != i)
  1899. {
  1900. cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
  1901. }
  1902. }
  1903. else
  1904. {
  1905. break;
  1906. }
  1907. }
  1908. else
  1909. {
  1910. //this.Rows[i].Cells[e.ColumnIndex].Selected = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected;
  1911. upRows++;
  1912. isSeleted = isSeleted || this.Rows[i].Selected;
  1913. upHeight += this.Rows[i].Height;
  1914. if (e.RowIndex != i)
  1915. {
  1916. cellwidth = cellwidth < this.Rows[i].Cells[e.ColumnIndex].Size.Width ? cellwidth : this.Rows[i].Cells[e.ColumnIndex].Size.Width;
  1917. }
  1918. }
  1919. }
  1920. else
  1921. {
  1922. break;
  1923. }
  1924. }
  1925. #endregion
  1926. count = downRows + upRows - 1;
  1927. if (count < 2)
  1928. {
  1929. return;
  1930. }
  1931. //}
  1932. if (this.Rows[e.RowIndex].Selected || isSeleted)
  1933. {
  1934. backBrush.Color = e.CellStyle.SelectionBackColor;
  1935. fontBrush.Color = e.CellStyle.SelectionForeColor;
  1936. }
  1937. //以背景色填充
  1938. e.Graphics.FillRectangle(backBrush, e.CellBounds);
  1939. //画字符串
  1940. //PaintingFont(e, cellwidth, upRows, downRows, count);
  1941. string text = e.FormattedValue + "";
  1942. if (!string.IsNullOrEmpty(text))
  1943. {
  1944. DataGridViewCellStyle cellStyle = e.CellStyle;
  1945. TextFormatFlags textFormatFlags = this.ComputeTextFormatFlagsForCellStyleAlignment((this.RightToLeft == RightToLeft.Yes), cellStyle.Alignment, cellStyle.WrapMode);
  1946. if ((textFormatFlags & TextFormatFlags.SingleLine) != TextFormatFlags.Default)
  1947. {
  1948. textFormatFlags |= TextFormatFlags.EndEllipsis;
  1949. }
  1950. Rectangle rectangle2 = new Rectangle(e.CellBounds.X, e.CellBounds.Bottom - upHeight, cellwidth, upHeight + downHeight - e.CellBounds.Height);
  1951. int num = (cellStyle.WrapMode == DataGridViewTriState.True) ? 1 : 2;
  1952. rectangle2.Offset(0, num);
  1953. rectangle2.Width = rectangle2.Width;
  1954. rectangle2.Height -= num + 1;
  1955. if (rectangle2.Width > 0 && rectangle2.Height > 0 && rectangle2.Y >= 0 && rectangle2.X >=0)
  1956. {
  1957. TextRenderer.DrawText(e.Graphics, text, cellStyle.Font, rectangle2, fontBrush.Color, textFormatFlags);
  1958. }
  1959. }
  1960. if (downRows == 1)
  1961. {
  1962. e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
  1963. count = 0;
  1964. }
  1965. // 画右边线
  1966. e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
  1967. e.Handled = true;
  1968. }
  1969. }
  1970. private TextFormatFlags ComputeTextFormatFlagsForCellStyleAlignment(bool rightToLeft, DataGridViewContentAlignment alignment, DataGridViewTriState wrapMode)
  1971. {
  1972. TextFormatFlags textFormatFlags;
  1973. if (alignment <= DataGridViewContentAlignment.MiddleCenter)
  1974. {
  1975. switch (alignment)
  1976. {
  1977. case DataGridViewContentAlignment.TopLeft:
  1978. textFormatFlags = TextFormatFlags.Default;
  1979. if (rightToLeft)
  1980. {
  1981. textFormatFlags |= TextFormatFlags.Right;
  1982. goto IL_C0;
  1983. }
  1984. goto IL_C0;
  1985. case DataGridViewContentAlignment.TopCenter:
  1986. textFormatFlags = TextFormatFlags.HorizontalCenter;
  1987. goto IL_C0;
  1988. case (DataGridViewContentAlignment)3:
  1989. break;
  1990. case DataGridViewContentAlignment.TopRight:
  1991. textFormatFlags = TextFormatFlags.Default;
  1992. if (rightToLeft)
  1993. {
  1994. goto IL_C0;
  1995. }
  1996. textFormatFlags |= TextFormatFlags.Right;
  1997. goto IL_C0;
  1998. default:
  1999. if (alignment != DataGridViewContentAlignment.MiddleLeft)
  2000. {
  2001. if (alignment == DataGridViewContentAlignment.MiddleCenter)
  2002. {
  2003. textFormatFlags = (TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
  2004. goto IL_C0;
  2005. }
  2006. }
  2007. else
  2008. {
  2009. textFormatFlags = TextFormatFlags.VerticalCenter;
  2010. if (rightToLeft)
  2011. {
  2012. textFormatFlags |= TextFormatFlags.Right;
  2013. goto IL_C0;
  2014. }
  2015. goto IL_C0;
  2016. }
  2017. break;
  2018. }
  2019. }
  2020. else
  2021. {
  2022. if (alignment <= DataGridViewContentAlignment.BottomLeft)
  2023. {
  2024. if (alignment != DataGridViewContentAlignment.MiddleRight)
  2025. {
  2026. if (alignment == DataGridViewContentAlignment.BottomLeft)
  2027. {
  2028. textFormatFlags = TextFormatFlags.Bottom;
  2029. if (rightToLeft)
  2030. {
  2031. textFormatFlags |= TextFormatFlags.Right;
  2032. goto IL_C0;
  2033. }
  2034. goto IL_C0;
  2035. }
  2036. }
  2037. else
  2038. {
  2039. textFormatFlags = TextFormatFlags.VerticalCenter;
  2040. if (rightToLeft)
  2041. {
  2042. goto IL_C0;
  2043. }
  2044. textFormatFlags |= TextFormatFlags.Right;
  2045. goto IL_C0;
  2046. }
  2047. }
  2048. else
  2049. {
  2050. if (alignment == DataGridViewContentAlignment.BottomCenter)
  2051. {
  2052. textFormatFlags = (TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter);
  2053. goto IL_C0;
  2054. }
  2055. if (alignment == DataGridViewContentAlignment.BottomRight)
  2056. {
  2057. textFormatFlags = TextFormatFlags.Bottom;
  2058. if (rightToLeft)
  2059. {
  2060. goto IL_C0;
  2061. }
  2062. textFormatFlags |= TextFormatFlags.Right;
  2063. goto IL_C0;
  2064. }
  2065. }
  2066. }
  2067. textFormatFlags = (TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
  2068. IL_C0:
  2069. if (wrapMode == DataGridViewTriState.False)
  2070. {
  2071. textFormatFlags |= TextFormatFlags.SingleLine;
  2072. }
  2073. else
  2074. {
  2075. textFormatFlags |= TextFormatFlags.WordBreak;
  2076. }
  2077. textFormatFlags |= TextFormatFlags.NoPrefix;
  2078. textFormatFlags |= TextFormatFlags.PreserveGraphicsClipping;
  2079. if (rightToLeft)
  2080. {
  2081. textFormatFlags |= TextFormatFlags.RightToLeft;
  2082. }
  2083. return textFormatFlags;
  2084. }
  2085. /// <summary>
  2086. /// 画字符串(合并单元格 周兴 2015-12-6 增加)
  2087. /// </summary>
  2088. /// <param name="e"></param>
  2089. /// <param name="cellwidth"></param>
  2090. /// <param name="UpRows"></param>
  2091. /// <param name="DownRows"></param>
  2092. /// <param name="count"></param>
  2093. private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
  2094. {
  2095. SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
  2096. int fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
  2097. int fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
  2098. int cellheight = e.CellBounds.Height;
  2099. if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
  2100. {
  2101. e.Graphics.DrawString(e.FormattedValue + "", e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y + cellheight * DownRows - fontheight);
  2102. }
  2103. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
  2104. {
  2105. e.Graphics.DrawString(e.FormattedValue + "", e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y + cellheight * DownRows - fontheight);
  2106. }
  2107. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
  2108. {
  2109. e.Graphics.DrawString(e.FormattedValue + "", e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y + cellheight * DownRows - fontheight);
  2110. }
  2111. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
  2112. {
  2113. e.Graphics.DrawString(e.FormattedValue + "", e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
  2114. }
  2115. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
  2116. {
  2117. e.Graphics.DrawString(e.FormattedValue + "", e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
  2118. }
  2119. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
  2120. {
  2121. e.Graphics.DrawString(e.FormattedValue + "", e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
  2122. }
  2123. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
  2124. {
  2125. e.Graphics.DrawString(e.FormattedValue + "", e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1));
  2126. }
  2127. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
  2128. {
  2129. e.Graphics.DrawString(e.FormattedValue + "", e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1));
  2130. }
  2131. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
  2132. {
  2133. e.Graphics.DrawString(e.FormattedValue + "", e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1));
  2134. }
  2135. else
  2136. {
  2137. e.Graphics.DrawString(e.FormattedValue + "", e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
  2138. }
  2139. }
  2140. #endregion
  2141. #region 受保护的方法/函数
  2142. protected override void OnColumnDisplayIndexChanged(DataGridViewColumnEventArgs e)
  2143. {
  2144. try
  2145. {
  2146. if (_isOpenTotalRow && !IsClickF12)
  2147. {
  2148. _columnInfos = this.GetDataGridViewColumnInfos();
  2149. if (_columnInfos != null)
  2150. {
  2151. for (int i = 0; i < _columnInfos.Length; i++)
  2152. {
  2153. if (_columnInfos[i].Visible)
  2154. {
  2155. NewText(_columnInfos[i].DataPropertyName, i);
  2156. }
  2157. }
  2158. CountTotalRow();
  2159. }
  2160. base.OnColumnDisplayIndexChanged(e);
  2161. }
  2162. }
  2163. catch (Exception ex)
  2164. {
  2165. throw ex;
  2166. }
  2167. }
  2168. protected override bool ProcessDialogKey(Keys keyData)
  2169. {
  2170. if (keyData == Keys.Enter)
  2171. {
  2172. return this.ProcessRightKey(keyData);
  2173. }
  2174. return base.ProcessDialogKey(keyData);
  2175. }
  2176. public new bool ProcessRightKey(Keys keyData)
  2177. {
  2178. if (keyData == Keys.Enter)
  2179. {
  2180. if (!IsProcessRightFlag || this.CurrentCell == null)
  2181. {
  2182. return false;
  2183. }
  2184. int rowIndex = this.CurrentCell.RowIndex;
  2185. int columnIndex = this.CurrentCell.ColumnIndex;
  2186. string columnName = this.Columns[columnIndex].Name;
  2187. // 选择商品时控制鼠标的行为
  2188. if ((columnName.Contains("Code") || columnName.Contains("OnlyCode"))
  2189. && !string.IsNullOrEmpty(this.CurrentRow.Cells[columnName].EditedFormattedValue + "")
  2190. && !this.CurrentRow.Cells[columnName].EditedFormattedValue.ToString()
  2191. .Equals(this.CurrentRow.Cells[columnName].Value))
  2192. {
  2193. base.ProcessRightKey(keyData);
  2194. }
  2195. // 如果是数值类型,数值合法时才跳到下一个可输入单元格
  2196. if (this.CurrentCell != null && this.CurrentCell.ValueType != null
  2197. && this.CurrentCell.ValueType.IsValueType
  2198. && this.CurrentCell.EditType != null
  2199. && "DataGridViewTextBoxEditingControl".Equals(this.CurrentCell.EditType.Name))
  2200. {
  2201. decimal temp = -1;
  2202. bool flag = decimal.TryParse(this.CurrentRow.Cells[columnName].EditedFormattedValue + "", out temp);
  2203. // 说明有非数字
  2204. if (!flag)
  2205. {
  2206. return true;
  2207. }
  2208. }
  2209. DataGridViewColumnInfo[] columnInfo = this.GetDataGridViewColumnInfos();
  2210. if (columnInfo != null && columnInfo.Length > 0)
  2211. {
  2212. for (int i = rowIndex; i < this.Rows.Count; i++)
  2213. {
  2214. bool flag = false;
  2215. for (int j = 0; j < columnInfo.Length; j++)
  2216. {
  2217. if (i == rowIndex)
  2218. {
  2219. if (!flag && !columnName.Equals(columnInfo[j].ColumnName))
  2220. {
  2221. continue;
  2222. }
  2223. else
  2224. {
  2225. if (!flag)
  2226. {
  2227. j += 1;
  2228. }
  2229. flag = true;
  2230. }
  2231. }
  2232. if (columnInfo.Length > j && !this.Rows[i].Cells[columnInfo[j].ColumnName].ReadOnly
  2233. && this.Rows[i].Cells[columnInfo[j].ColumnName].Visible)
  2234. {
  2235. //this.CurrentCell = this.Rows[i].Cells[columnInfo[j].ColumnName];
  2236. this.Rows[i].Cells[columnInfo[j].ColumnName].Selected = true;
  2237. return true;
  2238. }
  2239. }
  2240. }
  2241. }
  2242. //base.ProcessRightKey(keyData);
  2243. // 如果datagridview只读,按回车就会跳到下一行
  2244. //if (!isEnterKey && this.ReadOnly)
  2245. //{
  2246. // if (this.CurrentCell.RowIndex < this.Rows.Count - 1)
  2247. // {
  2248. // this.CurrentCell = this.Rows[this.CurrentCell.RowIndex + 1].Cells[0];
  2249. // }
  2250. //}
  2251. }
  2252. return true;
  2253. }
  2254. protected override bool ProcessDataGridViewKey(KeyEventArgs e)
  2255. {
  2256. if (e.KeyCode == Keys.Enter)
  2257. {
  2258. //isEnterKey = true;
  2259. return this.ProcessRightKey(e.KeyData);
  2260. }
  2261. return base.ProcessDataGridViewKey(e);
  2262. }
  2263. protected override void OnCellEnter(DataGridViewCellEventArgs e)
  2264. {
  2265. base.OnCellEnter(e);
  2266. }
  2267. protected override void OnMouseUp(MouseEventArgs e)
  2268. {
  2269. // used to keep extra mouse moves from selecting more rows when collapsing
  2270. base.OnMouseUp(e);
  2271. this._inExpandCollapseMouseCapture = false;
  2272. }
  2273. protected override void OnMouseMove(MouseEventArgs e)
  2274. {
  2275. // while we are expanding and collapsing a node mouse moves are
  2276. // supressed to keep selections from being messed up.
  2277. if (!this._inExpandCollapseMouseCapture)
  2278. base.OnMouseMove(e);
  2279. }
  2280. protected virtual void SetBackColorCell(int viewRowIndex, int viewColumnIndex)
  2281. {
  2282. if (!this.Visible)
  2283. {
  2284. return;
  2285. }
  2286. if (CellBackColorFlag == CellBackColorFlags.None)
  2287. {
  2288. return;
  2289. }
  2290. if (viewRowIndex < 0)
  2291. {
  2292. for (int i = 0; i < this.Rows.Count; i++)
  2293. {
  2294. SetBackColorCell(i, viewColumnIndex);
  2295. }
  2296. }
  2297. else if (viewColumnIndex < 0)
  2298. {
  2299. for (int i = 0; i < this.Columns.Count; i++)
  2300. {
  2301. SetBackColorCell(viewRowIndex, i);
  2302. }
  2303. }
  2304. else if (0 <= viewRowIndex && 0 <= viewColumnIndex)
  2305. {
  2306. DataGridViewCell cell = this.Rows[viewRowIndex].Cells[viewColumnIndex];
  2307. if (cell == null)
  2308. {
  2309. return;
  2310. }
  2311. if (this.ReadOnly || this.Rows[viewRowIndex].ReadOnly ||
  2312. this.Columns[viewColumnIndex].ReadOnly ||
  2313. this.Rows[viewRowIndex].Cells[viewColumnIndex].ReadOnly)
  2314. {
  2315. if (this.CellBackColorReadOnly != Color.Transparent &&
  2316. (this.CellBackColorFlag &
  2317. CellBackColorFlags.ReadOnlyBackColor) != 0)
  2318. {
  2319. cell.Style.BackColor = this.CellBackColorReadOnly;
  2320. return;
  2321. }
  2322. }
  2323. if (CurrentDataTable != null)
  2324. {
  2325. if ((CellBackColorFlag & CellBackColorFlags.ChangesBackColor) != 0)
  2326. {
  2327. DataRowView dataRowView = this.GetDataRowView(viewRowIndex);
  2328. if (dataRowView != null)
  2329. {
  2330. switch (dataRowView.Row.RowState)
  2331. {
  2332. case DataRowState.Added:
  2333. cell.Style.BackColor = CellBackColorAdded;
  2334. return;
  2335. case DataRowState.Modified:
  2336. string dataPropertyName =
  2337. this.Columns[viewColumnIndex].DataPropertyName;
  2338. if (string.IsNullOrEmpty(dataPropertyName))
  2339. {
  2340. break;
  2341. }
  2342. object valueOriginal =
  2343. dataRowView.Row[dataPropertyName, DataRowVersion.Original];
  2344. object valueCurrent =
  2345. dataRowView.Row[dataPropertyName, DataRowVersion.Current];
  2346. if (!valueOriginal.Equals(valueCurrent))
  2347. {
  2348. if (valueOriginal.Equals(DBNull.Value) &&
  2349. valueCurrent.Equals(string.Empty) ||
  2350. valueCurrent.Equals(DBNull.Value) &&
  2351. valueOriginal.Equals(string.Empty))
  2352. {
  2353. break;
  2354. }
  2355. cell.Style.BackColor = CellBackColorModified;
  2356. return;
  2357. }
  2358. break;
  2359. case DataRowState.Deleted:
  2360. cell.Style.BackColor = CellBackColorDeleted;
  2361. return;
  2362. default:
  2363. break;
  2364. }
  2365. }
  2366. }
  2367. }
  2368. if (this.CellBackColorNochanged != Color.Transparent &&
  2369. (cell.Style.BackColor != this.CellBackColorNochanged))
  2370. {
  2371. cell.Style.BackColor = this.CellBackColorNochanged;
  2372. }
  2373. }
  2374. }
  2375. protected void DispRowCount()
  2376. {
  2377. if (IsDispRowCount)
  2378. {
  2379. Graphics graphics = this.CreateGraphics();
  2380. OnPaint(new PaintEventArgs(graphics, new Rectangle(new Point(0, 0),
  2381. this.TopLeftHeaderCell.Size)));
  2382. graphics.Dispose();
  2383. }
  2384. }
  2385. protected void OnCsvCoverting(CsvConvertingEventArgs e)
  2386. {
  2387. if (CsvConverting != null)
  2388. {
  2389. CsvConverting(this, e);
  2390. }
  2391. }
  2392. #region 树形结构
  2393. public event ExpandingEventHandler NodeExpanding;
  2394. public event ExpandedEventHandler NodeExpanded;
  2395. public event CollapsingEventHandler NodeCollapsing;
  2396. public event CollapsedEventHandler NodeCollapsed;
  2397. protected virtual void OnNodeExpanding(ExpandingEventArgs e)
  2398. {
  2399. if (this.NodeExpanding != null)
  2400. {
  2401. NodeExpanding(this, e);
  2402. }
  2403. }
  2404. protected virtual void OnNodeExpanded(ExpandedEventArgs e)
  2405. {
  2406. if (this.NodeExpanded != null)
  2407. {
  2408. NodeExpanded(this, e);
  2409. }
  2410. }
  2411. protected virtual void OnNodeCollapsing(CollapsingEventArgs e)
  2412. {
  2413. if (this.NodeCollapsing != null)
  2414. {
  2415. NodeCollapsing(this, e);
  2416. }
  2417. }
  2418. protected virtual void OnNodeCollapsed(CollapsedEventArgs e)
  2419. {
  2420. if (this.NodeCollapsed != null)
  2421. {
  2422. NodeCollapsed(this, e);
  2423. }
  2424. }
  2425. #endregion
  2426. #endregion
  2427. #region 重写方法
  2428. protected override bool SetCurrentCellAddressCore(int columnIndex, int rowIndex, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick)
  2429. {
  2430. try
  2431. {
  2432. return base.SetCurrentCellAddressCore(columnIndex, rowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick);
  2433. }
  2434. catch (Exception ex)
  2435. {
  2436. return false;
  2437. }
  2438. }
  2439. private bool _isFlag = false;
  2440. protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
  2441. {
  2442. if (e.ColumnIndex >= 0 && e.RowIndex >= 0 && !_isFlag)
  2443. {
  2444. string value = this.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue + "";
  2445. if (!string.IsNullOrEmpty(value))
  2446. {
  2447. decimal valueDec = 0;
  2448. string format = this.Columns[e.ColumnIndex].DefaultCellStyle.Format;
  2449. bool flag = decimal.TryParse(value + "", out valueDec);
  2450. if (flag && format.IndexOf("F") >= 0)
  2451. {
  2452. _isFlag = true;
  2453. this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = valueDec.ToString(format);
  2454. _isFlag = false;
  2455. }
  2456. }
  2457. }
  2458. base.OnCellValueChanged(e);
  2459. }
  2460. protected override void OnSelectionChanged(EventArgs e)
  2461. {
  2462. try
  2463. {
  2464. base.OnSelectionChanged(e);
  2465. // 开合并行之后,在切换行处理
  2466. if (IsOpenMergeCellFlag && !string.IsNullOrEmpty(MergeOnlyColumn) &&
  2467. MergeColumnNames != null && MergeColumnNames.Count > 0 &&
  2468. this.Columns.Contains(MergeOnlyColumn) && this.CurrentCell != null
  2469. && (string.IsNullOrEmpty(this.Tag + "") || Convert.ToBoolean(this.Tag)))
  2470. {
  2471. Color color;
  2472. // 先清空颜色
  2473. for (int i = 0; i < this.Rows.Count; i++)
  2474. {
  2475. if (string.IsNullOrEmpty(this.Rows[i].Cells[MergeOnlyColumn].Tag + ""))
  2476. {
  2477. color = Color.White;
  2478. }
  2479. else
  2480. {
  2481. color = ColorTranslator.FromHtml(this.Rows[i].Cells[MergeOnlyColumn].Tag + "");
  2482. }
  2483. for (int j = 0; j < this.Columns.Count; j++)
  2484. {
  2485. if (this.Rows[i].Cells[j].Style.BackColor == ColorTranslator.FromHtml(Constant.COLOR_RED))
  2486. {
  2487. this.Rows[i].Cells[j].Style.BackColor = color;
  2488. this.Rows[i].Cells[j].Style.ForeColor = Color.Black;
  2489. }
  2490. }
  2491. }
  2492. // 向上找 ,如果有,就把背景颜色修改为选中
  2493. string onlyColumnC = this.Rows[this.CurrentCell.RowIndex].Cells[MergeOnlyColumn].Value + "";
  2494. string onlyDetailColumnC = string.Empty;
  2495. if (!string.IsNullOrEmpty(MergeDetailOnlyColumn))
  2496. {
  2497. onlyDetailColumnC = this.Rows[this.CurrentCell.RowIndex].Cells[MergeDetailOnlyColumn].Value + "";
  2498. }
  2499. string onlyDDetailColumnC = string.Empty;
  2500. if (!string.IsNullOrEmpty(MergeDDetailOnlyColumn))
  2501. {
  2502. onlyDDetailColumnC = this.Rows[this.CurrentCell.RowIndex].Cells[MergeDDetailOnlyColumn].Value + "";
  2503. }
  2504. List<string> tempColumns = new List<string>();
  2505. if (MergeDDetailColumnNames != null && MergeDDetailColumnNames.Count > 0)
  2506. {
  2507. tempColumns.AddRange(MergeDDetailColumnNames.ToArray());
  2508. }
  2509. if (MergeDetailColumnNames != null && MergeDetailColumnNames.Count > 0)
  2510. {
  2511. tempColumns.AddRange(MergeDetailColumnNames.ToArray());
  2512. }
  2513. if (this.CurrentCell.RowIndex >= 1)
  2514. {
  2515. for (int i = this.CurrentCell.RowIndex - 1; i >= 0; i--)
  2516. {
  2517. if (!string.IsNullOrEmpty(onlyDDetailColumnC) && onlyDDetailColumnC.Equals(this.Rows[i].Cells[MergeDDetailOnlyColumn].Value + ""))
  2518. {
  2519. if (MergeDDetailColumnNames != null)
  2520. {
  2521. for (int j = 0; j < MergeDDetailColumnNames.Count; j++)
  2522. {
  2523. if (this.Columns.Contains(MergeDDetailColumnNames[j]))
  2524. {
  2525. this.Rows[i].Cells[MergeDDetailColumnNames[j]].Style.BackColor = ColorTranslator.FromHtml(Constant.COLOR_RED);
  2526. this.Rows[i].Cells[MergeDDetailColumnNames[j]].Style.ForeColor = Color.White;
  2527. }
  2528. }
  2529. }
  2530. }
  2531. else if (!string.IsNullOrEmpty(onlyDetailColumnC) && onlyDetailColumnC.Equals(this.Rows[i].Cells[MergeDetailOnlyColumn].Value + ""))
  2532. {
  2533. if (MergeDetailColumnNames != null)
  2534. {
  2535. for (int j = 0; j < MergeDetailColumnNames.Count; j++)
  2536. {
  2537. if (this.Columns.Contains(MergeDetailColumnNames[j]))
  2538. {
  2539. this.Rows[i].Cells[MergeDetailColumnNames[j]].Style.BackColor = ColorTranslator.FromHtml(Constant.COLOR_RED);
  2540. this.Rows[i].Cells[MergeDetailColumnNames[j]].Style.ForeColor = Color.White;
  2541. }
  2542. }
  2543. }
  2544. }
  2545. else if (!string.IsNullOrEmpty(onlyColumnC) && onlyColumnC.Equals(this.Rows[i].Cells[MergeOnlyColumn].Value + ""))
  2546. {
  2547. for (int j = 0; j < MergeColumnNames.Count; j++)
  2548. {
  2549. if (this.Columns.Contains(MergeColumnNames[j]) && !tempColumns.Contains(MergeColumnNames[j]))
  2550. {
  2551. this.Rows[i].Cells[MergeColumnNames[j]].Style.BackColor = ColorTranslator.FromHtml(Constant.COLOR_RED);
  2552. this.Rows[i].Cells[MergeColumnNames[j]].Style.ForeColor = Color.White;
  2553. }
  2554. }
  2555. }
  2556. }
  2557. }
  2558. // 向下 ,如果有,就把背景颜色修改为选中
  2559. if (this.CurrentCell.RowIndex <= this.Rows.Count - 2)
  2560. {
  2561. for (int i = this.CurrentCell.RowIndex + 1; i <= this.Rows.Count - 1; i++)
  2562. {
  2563. if (!string.IsNullOrEmpty(onlyDDetailColumnC) && onlyDDetailColumnC.Equals(this.Rows[i].Cells[MergeDDetailOnlyColumn].Value + ""))
  2564. {
  2565. if (MergeDDetailColumnNames != null)
  2566. {
  2567. for (int j = 0; j < MergeDDetailColumnNames.Count; j++)
  2568. {
  2569. if (this.Columns.Contains(MergeDDetailColumnNames[j]))
  2570. {
  2571. this.Rows[i].Cells[MergeDDetailColumnNames[j]].Style.BackColor = ColorTranslator.FromHtml(Constant.COLOR_RED);
  2572. this.Rows[i].Cells[MergeDDetailColumnNames[j]].Style.ForeColor = Color.White;
  2573. }
  2574. }
  2575. }
  2576. }
  2577. else if (!string.IsNullOrEmpty(onlyDetailColumnC) && onlyDetailColumnC.Equals(this.Rows[i].Cells[MergeDetailOnlyColumn].Value + ""))
  2578. {
  2579. if (MergeDetailColumnNames != null)
  2580. {
  2581. for (int j = 0; j < MergeDetailColumnNames.Count; j++)
  2582. {
  2583. if (this.Columns.Contains(MergeDetailColumnNames[j]))
  2584. {
  2585. this.Rows[i].Cells[MergeDetailColumnNames[j]].Style.BackColor = ColorTranslator.FromHtml(Constant.COLOR_RED);
  2586. this.Rows[i].Cells[MergeDetailColumnNames[j]].Style.ForeColor = Color.White;
  2587. }
  2588. }
  2589. }
  2590. }
  2591. else if (!string.IsNullOrEmpty(onlyColumnC) && onlyColumnC.Equals(this.Rows[i].Cells[MergeOnlyColumn].Value + ""))
  2592. {
  2593. for (int j = 0; j < MergeColumnNames.Count; j++)
  2594. {
  2595. if (this.Columns.Contains(MergeColumnNames[j]) && !tempColumns.Contains(MergeColumnNames[j]))
  2596. {
  2597. this.Rows[i].Cells[MergeColumnNames[j]].Style.BackColor = ColorTranslator.FromHtml(Constant.COLOR_RED);
  2598. this.Rows[i].Cells[MergeColumnNames[j]].Style.ForeColor = Color.White;
  2599. }
  2600. }
  2601. }
  2602. }
  2603. }
  2604. }
  2605. }
  2606. catch (Exception ex)
  2607. {
  2608. throw ex;
  2609. }
  2610. }
  2611. protected override void OnHandleCreated(EventArgs e)
  2612. {
  2613. base.OnHandleCreated(e);
  2614. // this control is used to temporarly hide the vertical scroll bar
  2615. hideScrollBarControl = new Control();
  2616. hideScrollBarControl.Visible = false;
  2617. hideScrollBarControl.Enabled = false;
  2618. hideScrollBarControl.TabStop = false;
  2619. // control is disposed automatically when the grid is disposed
  2620. this.Controls.Add(hideScrollBarControl);
  2621. }
  2622. protected override void OnRowEnter(DataGridViewCellEventArgs e)
  2623. {
  2624. // ensure full row select
  2625. base.OnRowEnter(e);
  2626. if (this.SelectionMode == DataGridViewSelectionMode.CellSelect ||
  2627. (this.SelectionMode == DataGridViewSelectionMode.FullRowSelect &&
  2628. base.Rows[e.RowIndex].Selected == false))
  2629. {
  2630. this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  2631. base.Rows[e.RowIndex].Selected = true;
  2632. }
  2633. }
  2634. private void LockVerticalScrollBarUpdate(bool lockUpdate/*, bool delayed*/)
  2635. {
  2636. // Temporarly hide/show the vertical scroll bar by changing its parent
  2637. if (!this._inExpandCollapse)
  2638. {
  2639. if (lockUpdate)
  2640. {
  2641. this.VerticalScrollBar.Parent = hideScrollBarControl;
  2642. }
  2643. else
  2644. {
  2645. this.VerticalScrollBar.Parent = this;
  2646. }
  2647. }
  2648. }
  2649. protected override void OnPaint(PaintEventArgs e)
  2650. {
  2651. try
  2652. {
  2653. try
  2654. {
  2655. base.OnPaint(e);
  2656. }
  2657. catch (Exception ex)
  2658. {
  2659. ex.ToString();
  2660. }
  2661. // 头部分的重绘
  2662. if (this.HitTest(e.ClipRectangle.Left + 1, e.ClipRectangle.Top + 1).Type == DataGridViewHitTestType.TopLeftHeader)
  2663. {
  2664. // 属性设置需要显示总行数
  2665. if (IsDispRowCount)
  2666. {
  2667. // 当前选择行的Index
  2668. int currentIndex = 0;
  2669. if (this.CurrentRow == null)
  2670. {
  2671. return;
  2672. }
  2673. else
  2674. {
  2675. currentIndex = this.CurrentRow.Index;
  2676. }
  2677. // 内容的绘制
  2678. Rectangle rect = new Rectangle(0, 0, this.RowHeadersWidth - 4, this.ColumnHeadersHeight);
  2679. int rowCount = this.RowCount;
  2680. if (this.AllowUserToAddRows)
  2681. {
  2682. rowCount--;
  2683. }
  2684. string comment = string.Format("{0}/{1}",
  2685. currentIndex + 1,
  2686. rowCount
  2687. );
  2688. TextRenderer.DrawText(
  2689. e.Graphics,
  2690. comment, this.ColumnHeadersDefaultCellStyle.Font,
  2691. rect, this.ColumnHeadersDefaultCellStyle.ForeColor,
  2692. TextFormatFlags.VerticalCenter | TextFormatFlags.Right
  2693. );
  2694. }
  2695. }
  2696. }
  2697. catch (Exception ex)
  2698. {
  2699. throw ex;
  2700. //Debug.Assert(false, "Error", ex.Message);
  2701. }
  2702. }
  2703. /// <summary>
  2704. /// 滚动
  2705. /// </summary>
  2706. /// <param name="e"></param>
  2707. protected override void OnScroll(ScrollEventArgs e)
  2708. {
  2709. try
  2710. {
  2711. bool scrollDirection = (e.ScrollOrientation == ScrollOrientation.HorizontalScroll);
  2712. base.OnScroll(e);
  2713. if (_isOpenTotalRow)
  2714. {
  2715. SetTxtTop();
  2716. SetTxtLeft();
  2717. }
  2718. if (RefreshAtHscroll && scrollDirection)
  2719. {
  2720. this.Refresh();
  2721. }
  2722. }
  2723. catch (Exception ex)
  2724. {
  2725. throw ex;
  2726. }
  2727. }
  2728. protected override void OnRowHeadersWidthChanged(EventArgs e)
  2729. {
  2730. if (_isOpenTotalRow && _columnInfos != null)
  2731. {
  2732. if (findTextBox("txt" + this.Name) == null)
  2733. {
  2734. return;
  2735. }
  2736. findTextBox("txt" + this.Name).Width = this.RowHeadersWidth;
  2737. for (int i = 0; i < _columnInfos.Length; i++)
  2738. {
  2739. if (_columnInfos[i].Visible && findTextBox("txt" + _columnInfos[i].DataPropertyName) != null)
  2740. {
  2741. findTextBox("txt" + _columnInfos[i].DataPropertyName).Left = getLeft(_columnInfos[i].DataPropertyName, i);
  2742. }
  2743. }
  2744. }
  2745. base.OnRowHeadersWidthChanged(e);
  2746. }
  2747. /// <summary>
  2748. /// 列宽度改变时的重写
  2749. /// </summary>
  2750. /// <param name="e"></param>
  2751. protected override void OnColumnWidthChanged(DataGridViewColumnEventArgs e)
  2752. {
  2753. if (_isOpenTotalRow && _columnInfos != null && !IsAutoResizeColumns)
  2754. {
  2755. _columnInfos = GetDataGridViewColumnInfos();
  2756. //bool flag = false;
  2757. //for (int i = 0; i < _columnInfos.Length; i++)
  2758. //{
  2759. // if (_columnInfos[i].DataPropertyName.Equals(e.Column.DataPropertyName))
  2760. // {
  2761. // if (findTextBox("txt" + _columnInfos[i].DataPropertyName) == null)
  2762. // {
  2763. // return;
  2764. // }
  2765. // findTextBox("txt" + _columnInfos[i].DataPropertyName).Width = e.Column.Width;
  2766. // flag = true;
  2767. // }
  2768. // if (flag && _columnInfos[i].Visible)
  2769. // {
  2770. // findTextBox("txt" + _columnInfos[i].DataPropertyName).Left = getLeft(_columnInfos[i].DataPropertyName, i);
  2771. // }
  2772. //}
  2773. for (int i = 0; i < _columnInfos.Length; i++)
  2774. {
  2775. if (_columnInfos[i].Visible)
  2776. {
  2777. NewText(_columnInfos[i].DataPropertyName, i);
  2778. }
  2779. }
  2780. CountTotalRow();
  2781. }
  2782. Graphics g = Graphics.FromHwnd(this.Handle);
  2783. float uwh = g.MeasureString(e.Column.HeaderText, this.Font).Width;
  2784. if (uwh >= e.Column.Width)
  2785. {
  2786. e.Column.Width = Convert.ToInt16(uwh);
  2787. }
  2788. base.OnColumnWidthChanged(e);
  2789. if (RefreshAtHscroll)
  2790. {
  2791. this.Refresh();
  2792. }
  2793. }
  2794. protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
  2795. {
  2796. if (e.ColumnIndex < 0 || _columnDeep == 1)
  2797. {
  2798. // 周兴 2015-12-6 增加 合并单元格
  2799. if (e.RowIndex > -1 && e.ColumnIndex > -1)
  2800. {
  2801. DrawCell(e);
  2802. }
  2803. base.OnCellPainting(e);
  2804. return;
  2805. }
  2806. if (e.RowIndex == -1)
  2807. {
  2808. object[] colList = ColumnList.ToArray();
  2809. if (e.ColumnIndex >= colList.Length)
  2810. {
  2811. e.Handled = true;
  2812. return;
  2813. }
  2814. PaintUnitHeader((TreeNode)colList[e.ColumnIndex], e, _columnDeep);
  2815. e.Handled = true;
  2816. }
  2817. else
  2818. {
  2819. // 周兴 2015-12-6 增加 合并单元格
  2820. if (e.RowIndex > -1 && e.ColumnIndex > -1)
  2821. {
  2822. DrawCell(e);
  2823. }
  2824. base.OnCellPainting(e);
  2825. }
  2826. }
  2827. protected override void OnRowsAdded(DataGridViewRowsAddedEventArgs e)
  2828. {
  2829. if (_isOpenTotalRow && IsAutoCountSum)
  2830. {
  2831. CountTotalRow();
  2832. }
  2833. base.OnRowsAdded(e);
  2834. // Notify the row when it is added to the base grid
  2835. int count = e.RowCount - 1;
  2836. TreeGridNode row;
  2837. while (count >= 0)
  2838. {
  2839. row = base.Rows[e.RowIndex + count] as TreeGridNode;
  2840. if (row != null)
  2841. {
  2842. row.Sited();
  2843. }
  2844. count--;
  2845. }
  2846. }
  2847. internal protected void UnSiteAll()
  2848. {
  2849. this.UnSiteNode(this._root);
  2850. }
  2851. internal protected virtual void UnSiteNode(TreeGridNode node)
  2852. {
  2853. if (node != null && (node.IsSited || node.IsRoot))
  2854. {
  2855. // remove child rows first
  2856. foreach (TreeGridNode childNode in node.Nodes)
  2857. {
  2858. this.UnSiteNode(childNode);
  2859. }
  2860. // now remove this row except for the root
  2861. if (!node.IsRoot)
  2862. {
  2863. // zx 解决加载树形结构页面关闭时的bug
  2864. try
  2865. {
  2866. base.Rows.Remove(node);
  2867. // Row isn't sited in the grid anymore after remove. Note that we cannot
  2868. // Use the RowRemoved event since we cannot map from the row index to
  2869. // the index of the expandable row/node.
  2870. node.UnSited();
  2871. }
  2872. catch (Exception ex)
  2873. {
  2874. throw ex;
  2875. }
  2876. }
  2877. }
  2878. }
  2879. internal protected virtual bool CollapseNode(TreeGridNode node)
  2880. {
  2881. if (node.IsExpanded)
  2882. {
  2883. CollapsingEventArgs exp = new CollapsingEventArgs(node);
  2884. this.OnNodeCollapsing(exp);
  2885. if (!exp.Cancel)
  2886. {
  2887. this.LockVerticalScrollBarUpdate(true);
  2888. this.SuspendLayout();
  2889. _inExpandCollapse = true;
  2890. node.IsExpanded = false;
  2891. foreach (TreeGridNode childNode in node.Nodes)
  2892. {
  2893. Debug.Assert(childNode.RowIndex != -1, "Row is NOT in the grid.");
  2894. this.UnSiteNode(childNode);
  2895. }
  2896. CollapsedEventArgs exped = new CollapsedEventArgs(node);
  2897. this.OnNodeCollapsed(exped);
  2898. //TODO: Convert this to a specific NodeCell property
  2899. _inExpandCollapse = false;
  2900. this.LockVerticalScrollBarUpdate(false);
  2901. this.ResumeLayout(true);
  2902. this.InvalidateCell(node.Cells[0]);
  2903. }
  2904. return !exp.Cancel;
  2905. }
  2906. else
  2907. {
  2908. // row isn't expanded, so we didn't do anything.
  2909. return false;
  2910. }
  2911. }
  2912. internal protected virtual void SiteNode(TreeGridNode node)
  2913. {
  2914. //TODO: Raise exception if parent node is not the root or is not sited.
  2915. int rowIndex = -1;
  2916. TreeGridNode currentRow;
  2917. node._grid = this;
  2918. if (node.Parent != null && node.Parent.IsRoot == false)
  2919. {
  2920. // row is a child
  2921. Debug.Assert(node.Parent != null && node.Parent.IsExpanded == true);
  2922. if (node.Index > 0)
  2923. {
  2924. currentRow = node.Parent.Nodes[node.Index - 1];
  2925. }
  2926. else
  2927. {
  2928. currentRow = node.Parent;
  2929. }
  2930. }
  2931. else
  2932. {
  2933. // row is being added to the root
  2934. if (node.Index > 0)
  2935. {
  2936. currentRow = node.Parent.Nodes[node.Index - 1];
  2937. }
  2938. else
  2939. {
  2940. currentRow = null;
  2941. }
  2942. }
  2943. if (currentRow != null)
  2944. {
  2945. while (currentRow.Level >= node.Level)
  2946. {
  2947. if (currentRow.RowIndex < base.Rows.Count - 1)
  2948. {
  2949. currentRow = base.Rows[currentRow.RowIndex + 1] as TreeGridNode;
  2950. Debug.Assert(currentRow != null);
  2951. }
  2952. else
  2953. // no more rows, site this node at the end.
  2954. break;
  2955. }
  2956. if (currentRow == node.Parent)
  2957. rowIndex = currentRow.RowIndex + 1;
  2958. else if (currentRow.Level < node.Level)
  2959. rowIndex = currentRow.RowIndex;
  2960. else
  2961. rowIndex = currentRow.RowIndex + 1;
  2962. }
  2963. else
  2964. rowIndex = 0;
  2965. Debug.Assert(rowIndex != -1);
  2966. this.SiteNode(node, rowIndex);
  2967. Debug.Assert(node.IsSited);
  2968. node.IsExpanded = _isExpandAll;
  2969. if (node.IsExpanded)
  2970. {
  2971. // add all child rows to display
  2972. foreach (TreeGridNode childNode in node.Nodes)
  2973. {
  2974. //TODO: could use the more efficient SiteRow with index.
  2975. this.SiteNode(childNode);
  2976. }
  2977. }
  2978. }
  2979. private bool _isExpandAll = false;
  2980. /// <summary>
  2981. /// 是否打开所有树形结构的节点
  2982. /// </summary>
  2983. public bool IsExpandAll
  2984. {
  2985. set
  2986. {
  2987. if (HasNode && !string.IsNullOrEmpty(NodeColumnName))
  2988. {
  2989. _isExpandAll = value;
  2990. }
  2991. }
  2992. }
  2993. protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
  2994. {
  2995. if (typeof(TreeGridColumn).IsAssignableFrom(e.Column.GetType()))
  2996. {
  2997. if (_expandableColumn == null)
  2998. {
  2999. // identify the expanding column.
  3000. _expandableColumn = (TreeGridColumn)e.Column;
  3001. }
  3002. else
  3003. {
  3004. // this.Columns.Remove(e.Column);
  3005. //throw new InvalidOperationException("Only one TreeGridColumn per TreeGridView is supported.");
  3006. }
  3007. }
  3008. // Expandable Grid doesn't support sorting. This is just a limitation of the sample.
  3009. //e.Column.SortMode = DataGridViewColumnSortMode.Automatic;
  3010. base.OnColumnAdded(e);
  3011. e.Column.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
  3012. }
  3013. internal protected virtual void SiteNode(TreeGridNode node, int index)
  3014. {
  3015. if (index < base.Rows.Count)
  3016. {
  3017. base.Rows.Insert(index, node);
  3018. }
  3019. else
  3020. {
  3021. // for the last item.
  3022. base.Rows.Add(node);
  3023. }
  3024. }
  3025. internal protected virtual bool ExpandNode(TreeGridNode node)
  3026. {
  3027. if (!node.IsExpanded || this._virtualNodes)
  3028. {
  3029. ExpandingEventArgs exp = new ExpandingEventArgs(node);
  3030. this.OnNodeExpanding(exp);
  3031. if (!exp.Cancel)
  3032. {
  3033. this.LockVerticalScrollBarUpdate(true);
  3034. this.SuspendLayout();
  3035. _inExpandCollapse = true;
  3036. node.IsExpanded = true;
  3037. //TODO Convert this to a InsertRange
  3038. foreach (TreeGridNode childNode in node.Nodes)
  3039. {
  3040. Debug.Assert(childNode.RowIndex == -1, "Row is already in the grid.");
  3041. this.SiteNode(childNode);
  3042. //this.BaseRows.Insert(rowIndex + 1, childRow);
  3043. //TODO : remove -- just a test.
  3044. //childNode.Cells[0].Value = "child";
  3045. }
  3046. ExpandedEventArgs exped = new ExpandedEventArgs(node);
  3047. this.OnNodeExpanded(exped);
  3048. //TODO: Convert this to a specific NodeCell property
  3049. _inExpandCollapse = false;
  3050. this.LockVerticalScrollBarUpdate(false);
  3051. this.ResumeLayout(true);
  3052. this.InvalidateCell(node.Cells[0]);
  3053. }
  3054. return !exp.Cancel;
  3055. }
  3056. else
  3057. {
  3058. // row is already expanded, so we didn't do anything.
  3059. return false;
  3060. }
  3061. }
  3062. private void InitNode(DataTable dataTable, TreeGridNode node,
  3063. SortedList<int, string> sortedlist, DataRow[] dataRows)
  3064. {
  3065. DataRow[] dataRowsTmp = null;
  3066. TreeGridNode nodeTmp = null;
  3067. for (int i = 0; i < dataRows.Length; i++)
  3068. {
  3069. nodeTmp = node.Nodes.Add(dataRows[i], dataTable.Columns, this.Columns, sortedlist, out sortedlist);
  3070. dataRowsTmp = dataTable.Select(" ParentID = " + dataRows[i][NodeColumnName]);
  3071. InitNode(dataTable, nodeTmp, sortedlist, dataRowsTmp);
  3072. }
  3073. }
  3074. #endregion
  3075. #region 事件
  3076. #region 单元格事件
  3077. private void dkDataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
  3078. {
  3079. if (this.IsHandleCreated)
  3080. {
  3081. DispRowCount();
  3082. }
  3083. }
  3084. private void dkDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  3085. {
  3086. // 清除错误显示
  3087. }
  3088. private void dkDataGridView_CellValidated(object sender, DataGridViewCellEventArgs e)
  3089. {
  3090. DataTable dtblDataTable = GetDataTable();
  3091. if (dtblDataTable != null && this.IsCurrentRowDirty)
  3092. {
  3093. DataGridView dataGridView = (DataGridView)sender;
  3094. DataColumn dataColumn =
  3095. dtblDataTable.Columns[dataGridView.Columns[e.ColumnIndex].DataPropertyName];
  3096. if (dataColumn != null)
  3097. {
  3098. if (!dataColumn.AllowDBNull)
  3099. {
  3100. if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == DBNull.Value)
  3101. {
  3102. if (dataColumn.DataType.Equals(typeof(string)))
  3103. {
  3104. dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = string.Empty;
  3105. }
  3106. }
  3107. }
  3108. }
  3109. }
  3110. }
  3111. private bool IsSelectSameCell(int selectCellRowIndex, int selectCellColumnIndex)
  3112. {
  3113. if ((selectCellRowIndex == _selectCellRowCol[0]) && (selectCellColumnIndex == _selectCellRowCol[1]))
  3114. {
  3115. return true;
  3116. }
  3117. else
  3118. {
  3119. return false;
  3120. }
  3121. }
  3122. private void SetMnuFrozenText(int intRowIndex, int intColumnIndex)
  3123. {
  3124. bool isSelectSameCell = true;
  3125. isSelectSameCell = IsSelectSameCell(intRowIndex, intColumnIndex);
  3126. if (isSelectSameCell && _isFrozen)
  3127. {
  3128. _mnuFrozen.Text = ControlsTips.DK_DataGridView_UnFrozen;
  3129. }
  3130. else
  3131. {
  3132. _mnuFrozen.Text = ControlsTips.DK_DataGridView_Frozen;
  3133. }
  3134. }
  3135. private void SetGridViewColumnHeaderMenu()
  3136. {
  3137. _ctxColumnHeader.Items.Clear();
  3138. if (this.ContextMenuVisible != ContextMenuVisibleFlags.None)
  3139. {
  3140. AddToolStripSeparator(_ctxColumnHeader);
  3141. _ctxColumnHeader.Items.Add(this._mnuFrozen);
  3142. }
  3143. if ((this.ContextMenuVisible & ContextMenuVisibleFlags.Refine) ==
  3144. ContextMenuVisibleFlags.Refine)
  3145. {
  3146. AddToolStripSeparator(_ctxColumnHeader);
  3147. _ctxColumnHeader.Items.Add(this._mnuRefine);
  3148. _ctxColumnHeader.Items.Add(this._mnuUnfilter);
  3149. }
  3150. if (IsExportFlag && (this.ContextMenuVisible & ContextMenuVisibleFlags.FileOut) ==
  3151. ContextMenuVisibleFlags.FileOut)
  3152. {
  3153. AddToolStripSeparator(_ctxColumnHeader);
  3154. _ctxColumnHeader.Items.Add(this._mnuOutputFile);
  3155. _ctxColumnHeader.Items.Add(this._mnuOutputFileXls);
  3156. }
  3157. }
  3158. private void SetGridViewCellMenu(DataGridViewRow gridViewRow)
  3159. {
  3160. _ctxCell.Items.Clear();
  3161. // 右键菜单的Item设定
  3162. if (this.ContextMenuVisible != ContextMenuVisibleFlags.None)
  3163. {
  3164. AddToolStripSeparator(_ctxCell);
  3165. _ctxCell.Items.Add(this._mnuFrozen);
  3166. }
  3167. if (!gridViewRow.IsNewRow)
  3168. {
  3169. // 不存在新建行的情况
  3170. if ((this.ContextMenuVisible & ContextMenuVisibleFlags.Refine) ==
  3171. ContextMenuVisibleFlags.Refine)
  3172. {
  3173. AddToolStripSeparator(_ctxCell);
  3174. _ctxCell.Items.Add(this._mnuRefine);
  3175. _ctxCell.Items.Add(this._mnuFilter);
  3176. _ctxCell.Items.Add(this._mnuFilterExcept);
  3177. _ctxCell.Items.Add(this._mnuUnfilter);
  3178. }
  3179. }
  3180. else
  3181. {
  3182. // 新建行的情况
  3183. if ((this.ContextMenuVisible & ContextMenuVisibleFlags.Refine) ==
  3184. ContextMenuVisibleFlags.Refine)
  3185. {
  3186. AddToolStripSeparator(_ctxCell);
  3187. _ctxCell.Items.Add(this._mnuRefine);
  3188. _ctxCell.Items.Add(this._mnuUnfilter);
  3189. }
  3190. }
  3191. if (IsExportFlag && (this.ContextMenuVisible & ContextMenuVisibleFlags.FileOut) ==
  3192. ContextMenuVisibleFlags.FileOut)
  3193. {
  3194. AddToolStripSeparator(_ctxCell);
  3195. _ctxCell.Items.Add(this._mnuOutputFile);
  3196. _ctxCell.Items.Add(this._mnuOutputFileXls);
  3197. }
  3198. //if (IsPrintFlag && (this.ContextMenuVisible & ContextMenuVisibleFlags.Print) == ContextMenuVisibleFlags.Print)
  3199. //{
  3200. // AddToolStripSeparator(_ctxCell);
  3201. // _ctxCell.Items.Add(this._mnuPrint);
  3202. //}
  3203. }
  3204. private void DataGridViewMouseDownRight(DataGridView.HitTestInfo gridViewHitTestInfo, MouseEventArgs e)
  3205. {
  3206. try
  3207. {
  3208. // 如果是数值类型,数值合法时才跳到下一个可输入单元格
  3209. if (this.CurrentCell != null && this.CurrentCell.ValueType != null
  3210. && this.CurrentCell.ValueType.IsValueType
  3211. && this.CurrentCell.EditType != null
  3212. && "DataGridViewTextBoxEditingControl".Equals(this.CurrentCell.EditType.Name))
  3213. {
  3214. if ("int".Equals(this.CurrentCell.ValueType.Name.ToLower())
  3215. || "decimal".Equals(this.CurrentCell.ValueType.Name.ToLower())
  3216. || "double".Equals(this.CurrentCell.ValueType.Name.ToLower()))
  3217. {
  3218. string columnName = this.Columns[this.CurrentCell.ColumnIndex].Name;
  3219. decimal temp = -1;
  3220. bool flag = decimal.TryParse(this.CurrentRow.Cells[columnName].EditedFormattedValue + "", out temp);
  3221. // 说明有非数字
  3222. if (!flag)
  3223. {
  3224. return;
  3225. }
  3226. }
  3227. }
  3228. if (gridViewHitTestInfo.Type == DataGridViewHitTestType.ColumnHeader)
  3229. {
  3230. // 点击列头的情况下,设定右键菜单的Item
  3231. SetGridViewColumnHeaderMenu();
  3232. Point point = new Point(e.X, e.Y);
  3233. point = this.PointToScreen(point);
  3234. _ctxColumnHeader.Tag = gridViewHitTestInfo;
  3235. _ctxColumnHeader.Show(point);
  3236. _gridRowIndex = 0;
  3237. _gridColumnindex = gridViewHitTestInfo.ColumnIndex;
  3238. }
  3239. else if (gridViewHitTestInfo.Type == DataGridViewHitTestType.Cell)
  3240. {
  3241. // 点击单元格的情况
  3242. DataGridViewRow gridViewRow = this.Rows[gridViewHitTestInfo.RowIndex];
  3243. int intSelRowsCount = this.SelectedRows.Count;
  3244. if (gridViewRow != null)
  3245. {
  3246. if (_isShowDelete)
  3247. {
  3248. if (1 == intSelRowsCount)
  3249. {
  3250. _ctxCell.Items.Clear();
  3251. // 设定右键菜单的Item
  3252. if (this.ContextMenuVisible != ContextMenuVisibleFlags.None)
  3253. {
  3254. AddToolStripSeparator(_ctxCell);
  3255. _ctxCell.Items.Add(this._mnuDelete);
  3256. }
  3257. Point point = new Point(e.X, e.Y);
  3258. point = this.PointToScreen(point);
  3259. _ctxCell.Tag = gridViewHitTestInfo;
  3260. _ctxCell.Show(point);
  3261. }
  3262. }
  3263. else
  3264. {
  3265. if (!_isCanDelete)
  3266. {
  3267. if (!gridViewRow.IsNewRow)
  3268. {
  3269. this.Tag = false;
  3270. this.CurrentCell = gridViewRow.Cells[gridViewHitTestInfo.ColumnIndex];
  3271. this.Tag = true;
  3272. // 点击右键要调到相应的行去 周兴 2016-11-3 修改
  3273. if (IsOpenMergeCellFlag )
  3274. {
  3275. this.OnSelectionChanged(e);
  3276. }
  3277. }
  3278. // 设定右键菜单的Item
  3279. SetGridViewCellMenu(gridViewRow);
  3280. Point point = new Point(e.X, e.Y);
  3281. point = this.PointToScreen(point);
  3282. _ctxCell.Tag = gridViewHitTestInfo;
  3283. _ctxCell.Show(point);
  3284. _gridRowIndex = gridViewHitTestInfo.RowIndex;
  3285. _gridColumnindex = gridViewHitTestInfo.ColumnIndex;
  3286. }
  3287. }
  3288. }
  3289. }
  3290. }
  3291. catch (Exception ex)
  3292. {
  3293. throw ex;
  3294. // todo 捕获之后不操作
  3295. }
  3296. }
  3297. #endregion
  3298. #region 行事件
  3299. private void dkDataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
  3300. {
  3301. DataGridViewRow currentRow = ((DataGridView)sender).Rows[e.RowIndex];
  3302. if (currentRow != null)
  3303. {
  3304. if (!currentRow.IsNewRow)
  3305. {
  3306. if (currentRow.ReadOnly != !_isAllowUserToModifyRows)
  3307. {
  3308. currentRow.ReadOnly = !_isAllowUserToModifyRows;
  3309. }
  3310. }
  3311. }
  3312. if (this.IsCurrentCellDirty)
  3313. {
  3314. this.NotifyCurrentCellDirty(false);
  3315. }
  3316. }
  3317. private void Rows_CollectionChanged(object sender,
  3318. System.ComponentModel.CollectionChangeEventArgs e)
  3319. {
  3320. switch (e.Action)
  3321. {
  3322. case System.ComponentModel.CollectionChangeAction.Refresh:
  3323. if (this.Rows.Count > 0)
  3324. {
  3325. SetBackColorCell(-1, -1);
  3326. }
  3327. break;
  3328. case System.ComponentModel.CollectionChangeAction.Add:
  3329. break;
  3330. case System.ComponentModel.CollectionChangeAction.Remove:
  3331. break;
  3332. }
  3333. }
  3334. private void dkDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  3335. {
  3336. SetBackColorCell(e.RowIndex, -1);
  3337. }
  3338. #endregion
  3339. #region 鼠标、键盘事件
  3340. private void dkDataGridView_MouseDown(object sender, MouseEventArgs e)
  3341. {
  3342. DataGridView.HitTestInfo gridViewHitTestInfo = this.HitTest(e.X, e.Y);
  3343. if (gridViewHitTestInfo.Type == DataGridViewHitTestType.None)
  3344. {
  3345. if (this.IsClearSelected)
  3346. {
  3347. // 清除选择行
  3348. this.ClearSelection();
  3349. }
  3350. this.labelHideFocus.Focus();
  3351. if (MouseClickOnSpace != null)
  3352. {
  3353. MouseClickOnSpace(this, e);
  3354. }
  3355. return;
  3356. }
  3357. if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
  3358. {
  3359. // 右键点击
  3360. DataGridViewMouseDownRight(gridViewHitTestInfo, e);
  3361. SetMnuFrozenText(gridViewHitTestInfo.RowIndex, gridViewHitTestInfo.ColumnIndex);
  3362. }
  3363. }
  3364. /// <summary>
  3365. /// 按下ESC键的处理
  3366. /// </summary>
  3367. /// <param name="sender"></param>
  3368. /// <param name="e"></param>
  3369. private void dkDataGridView_EscKeyDown(object sender, KeyEventArgs e)
  3370. {
  3371. if (e.KeyCode == Keys.Escape)
  3372. {
  3373. if (this.IsClearSelected)
  3374. {
  3375. this.ClearSelection();
  3376. this.labelHideFocus.Focus();
  3377. if (MouseClickOnSpace != null)
  3378. {
  3379. MouseClickOnSpace(this, e);
  3380. }
  3381. }
  3382. }
  3383. }
  3384. private void AddToolStripSeparator(ContextMenuStrip contextMenuStrip)
  3385. {
  3386. if (contextMenuStrip.Items.Count == 0)
  3387. {
  3388. return;
  3389. }
  3390. if (contextMenuStrip.Items[contextMenuStrip.Items.Count - 1] is ToolStripSeparator)
  3391. {
  3392. return;
  3393. }
  3394. contextMenuStrip.Items.Add(new ToolStripSeparator());
  3395. }
  3396. private void dkDataGridView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  3397. {
  3398. ((DataGridView)sender).EndEdit();
  3399. }
  3400. #endregion
  3401. #region 右键菜单事件
  3402. private void mnuRefine_RefineTextFixed(object sender, EventArgs e)
  3403. {
  3404. if (string.IsNullOrEmpty(_mnuRefine.Text))
  3405. {
  3406. MessageBox.Show(ControlsTips.DK_DataGridView_RefineEmpty);
  3407. }
  3408. else
  3409. {
  3410. ToolStripItem toolStripItem = sender as ToolStripItem;
  3411. if (toolStripItem == null)
  3412. {
  3413. return;
  3414. }
  3415. ContextMenuStrip contextMenuStrip = toolStripItem.Owner as ContextMenuStrip;
  3416. if (contextMenuStrip == null)
  3417. {
  3418. return;
  3419. }
  3420. contextMenuStrip.Close();
  3421. HitTestInfo info = (HitTestInfo)contextMenuStrip.Tag;
  3422. this.Tag = false;
  3423. this.FilterRow(info.ColumnIndex, FilterType.Like, _mnuRefine.Text);
  3424. this.Tag = true;
  3425. this.OnSelectionChanged(e);
  3426. }
  3427. }
  3428. protected String ConvertRegularExpressionFilter(String sqlFilter)
  3429. {
  3430. if (String.IsNullOrEmpty(sqlFilter))
  3431. {
  3432. return sqlFilter;
  3433. }
  3434. if ("%".Equals(sqlFilter))
  3435. {
  3436. return ".";
  3437. }
  3438. Boolean isStartVague = (sqlFilter.Substring(0, 1) == "%");
  3439. Boolean isEndVague = (sqlFilter.Substring(sqlFilter.Length - 1, 1) == "%");
  3440. String escapedSqlFilter = sqlFilter.Substring((isStartVague) ? 1 : 0,
  3441. sqlFilter.Length - ((isEndVague) ? 1 : 0) - ((isStartVague) ? 1 : 0));
  3442. return String.Format("{0}{1}{2}", isStartVague ? String.Empty : "%",
  3443. escapedSqlFilter, isEndVague ? String.Empty : "%");
  3444. }
  3445. private void mnuFilter_Click(object sender, EventArgs e)
  3446. {
  3447. this.Tag = false;
  3448. this.FilterRow(FilterType.Equal);
  3449. this.Tag = true;
  3450. this.OnSelectionChanged(e);
  3451. }
  3452. private void mnuFilterExcept_Click(object sender, EventArgs e)
  3453. {
  3454. this.Tag = false;
  3455. this.FilterRow(FilterType.NotEqual);
  3456. this.Tag = true;
  3457. this.OnSelectionChanged(e);
  3458. }
  3459. private void mnuUnfilter_Click(object sender, EventArgs e)
  3460. {
  3461. this.Tag = false;
  3462. this.ResetFilter();
  3463. this.Tag = true;
  3464. this.OnSelectionChanged(e);
  3465. }
  3466. private void mnuOutputFile_Click(object sender, EventArgs e)
  3467. {
  3468. this.OutputFile();
  3469. }
  3470. private void mnuOutputFileXls_Click(object sender, EventArgs e)
  3471. {
  3472. this.OutputFileXls();
  3473. }
  3474. private void mnuFrozen_Click(object sender, EventArgs e)
  3475. {
  3476. this.Tag = false;
  3477. this.SetFrozen();
  3478. this.Tag = true;
  3479. }
  3480. private void mnuDelete_Click(object sender, EventArgs e)
  3481. {
  3482. if (RowsHeaderClick != null)
  3483. {
  3484. RowsHeaderClick(this, e);
  3485. }
  3486. }
  3487. private void mnuPrint_Click(object sender, EventArgs e)
  3488. {
  3489. VBprinter.DGVprint dGVprint = new VBprinter.DGVprint();
  3490. dGVprint.PrintType = VBprinter.DGVprint.mytype.GeneralPrint;//打印对齐
  3491. dGVprint.Alignment = StringAlignment.Center; //表格居中
  3492. dGVprint.MainTitle = this.Text;//主标题
  3493. dGVprint.AutoFormat = false;//自动调整行宽
  3494. dGVprint.IsShowUnvisibleColum = false;//是否显示隐藏列
  3495. if (this.ColumnTreeView == null || this.ColumnTreeView.Length == 0)
  3496. {
  3497. dGVprint.Print(this, true, "");
  3498. }
  3499. else
  3500. {
  3501. dGVprint.Print(this, true, "", this.ColumnTreeView[0]);
  3502. }
  3503. }
  3504. #endregion
  3505. #region dkDataGridView事件
  3506. private void dkDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
  3507. {
  3508. if (e.Context == DataGridViewDataErrorContexts.Commit)
  3509. {
  3510. e.Cancel = true;
  3511. }
  3512. }
  3513. private void dkDataGridView_ReadOnlyChanged(object sender, EventArgs e)
  3514. {
  3515. SetBackColorCell(-1, -1);
  3516. }
  3517. private void dkDataGridView_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  3518. {
  3519. if (e.KeyCode == Keys.Escape)
  3520. {
  3521. if (this.DataSource != null)
  3522. {
  3523. if (this.BindingContext[this.DataSource, this.DataMember].Count ==
  3524. 1 && this.CurrentRow.IsNewRow)
  3525. {
  3526. this.BindingContext[this.DataSource, this.DataMember].RemoveAt(0);
  3527. }
  3528. }
  3529. }
  3530. }
  3531. private void dkDataGridView_EditingControlShowing(object sender,
  3532. DataGridViewEditingControlShowingEventArgs e)
  3533. {
  3534. if (this.EditingControl != null)
  3535. {
  3536. this.EditingControl.PreviewKeyDown -=
  3537. new PreviewKeyDownEventHandler(EditingControl_PreviewKeyDown);
  3538. this.EditingControl.PreviewKeyDown +=
  3539. new PreviewKeyDownEventHandler(EditingControl_PreviewKeyDown);
  3540. }
  3541. }
  3542. private void EditingControl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  3543. {
  3544. if (e.KeyCode == Keys.Escape)
  3545. {
  3546. if (this.DataSource != null)
  3547. {
  3548. if (this.BindingContext[this.DataSource, this.DataMember].Count ==
  3549. 1 && this.CurrentRow.IsNewRow)
  3550. {
  3551. this.BindingContext[this.DataSource, this.DataMember].RemoveAt(0);
  3552. }
  3553. }
  3554. }
  3555. }
  3556. #endregion
  3557. #endregion
  3558. #region 设计自动生成代码
  3559. private void InitializeComponent()
  3560. {
  3561. this.components = new System.ComponentModel.Container();
  3562. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
  3563. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
  3564. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
  3565. this._ctxColumnHeader = new System.Windows.Forms.ContextMenuStrip(this.components);
  3566. this._ctxCell = new System.Windows.Forms.ContextMenuStrip(this.components);
  3567. this.labelHideFocus = new System.Windows.Forms.Label();
  3568. ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
  3569. this.SuspendLayout();
  3570. //
  3571. // _ctxColumnHeader
  3572. //
  3573. this._ctxColumnHeader.Name = "contextMenuStrip1";
  3574. this._ctxColumnHeader.Size = new System.Drawing.Size(61, 4);
  3575. //
  3576. // _ctxCell
  3577. //
  3578. this._ctxCell.Name = "ctxCell";
  3579. this._ctxCell.Size = new System.Drawing.Size(61, 4);
  3580. //
  3581. // labelHideFocus
  3582. //
  3583. this.labelHideFocus.AutoSize = true;
  3584. this.labelHideFocus.Location = new System.Drawing.Point(0, 0);
  3585. this.labelHideFocus.Name = "labelHideFocus";
  3586. this.labelHideFocus.Size = new System.Drawing.Size(0, 12);
  3587. this.labelHideFocus.TabIndex = 0;
  3588. //
  3589. // dkDataGridView
  3590. //
  3591. dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(235)))), ((int)(((byte)(235)))));
  3592. this.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
  3593. this.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
  3594. dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
  3595. dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(48)))), ((int)(((byte)(58)))), ((int)(((byte)(70)))));
  3596. dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  3597. dataGridViewCellStyle2.ForeColor = System.Drawing.Color.White;
  3598. dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
  3599. dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
  3600. dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
  3601. this.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
  3602. this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
  3603. this.ColumnHeadersHeight = 25;
  3604. this.Controls.Add(this.labelHideFocus);
  3605. this.EnableHeadersVisualStyles = false;
  3606. dataGridViewCellStyle3.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(159)))), ((int)(((byte)(39)))), ((int)(((byte)(39)))));
  3607. this.RowsDefaultCellStyle = dataGridViewCellStyle3;
  3608. this.RowTemplate.Height = 25;
  3609. this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dkDataGridView_MouseDown);
  3610. this.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dkDataGridView_RowEnter);
  3611. this.ColumnHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dkDataGridView_ColumnHeaderMouseClick);
  3612. this.ReadOnlyChanged += new System.EventHandler(this.dkDataGridView_ReadOnlyChanged);
  3613. this.CellValidated += new System.Windows.Forms.DataGridViewCellEventHandler(this.dkDataGridView_CellValidated);
  3614. this.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dkDataGridView_RowPostPaint);
  3615. this.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.dkDataGridView_PreviewKeyDown);
  3616. this.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dkDataGridView_CellEndEdit);
  3617. this.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dkDataGridView_EditingControlShowing);
  3618. this.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dkDataGridView_DataError);
  3619. this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dkDataGridView_EscKeyDown);
  3620. this.CellEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dkDataGridView_CellEnter);
  3621. this.RowHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dkDataGridView_RowHeaderMouseClick);
  3622. ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
  3623. this.ResumeLayout(false);
  3624. this.PerformLayout();
  3625. }
  3626. private void dkDataGridView_ColumnHeaderMouseClick(object sender,
  3627. DataGridViewCellMouseEventArgs e)
  3628. {
  3629. string sort = "ASC";
  3630. string sortColumns = "";
  3631. int columnIndex = e.ColumnIndex;
  3632. DataGridViewColumn column = this.Columns[columnIndex];
  3633. string colName = column.Name;
  3634. string colPropertyName = column.DataPropertyName;
  3635. if (this.AllowUserToSortRows == false)
  3636. {
  3637. return;
  3638. }
  3639. if (columnIndex < 0 || this.Columns.Count <= columnIndex)
  3640. {
  3641. throw new ArgumentException("列索引超出范围。");
  3642. }
  3643. if (string.IsNullOrEmpty(colPropertyName))
  3644. {
  3645. return;
  3646. }
  3647. // 点击同一列进行排序时,切换升序和降序
  3648. if (colName.Equals(_sortColumnName))
  3649. {
  3650. if (_sortOrderType == SortOrder.Ascending)
  3651. {
  3652. _sortOrderType = SortOrder.Descending;
  3653. sort = "DESC";
  3654. }
  3655. else
  3656. {
  3657. _sortOrderType = SortOrder.Ascending;
  3658. sort = "ASC";
  3659. }
  3660. }
  3661. else
  3662. {
  3663. // 不是同一列进行排序时,按升序排序
  3664. _sortOrderType = SortOrder.Ascending;
  3665. _sortColumnName = colName;
  3666. sort = "ASC";
  3667. }
  3668. if (!string.IsNullOrEmpty(SortOrderColumnName))
  3669. {
  3670. sortColumns = this.AnalysisSortColumn(_sortColumnName, sort);
  3671. if (!string.IsNullOrEmpty(sortColumns))
  3672. {
  3673. DataTable dataTable = (DataTable)this.DataSource;
  3674. DataView dataView = dataTable.DefaultView;
  3675. dataView.Sort = sortColumns;
  3676. this.DataSource = dataView.Table;
  3677. this.Columns[_sortColumnName].HeaderCell.SortGlyphDirection = _sortOrderType;
  3678. }
  3679. }
  3680. this.OnSelectionChanged(e);
  3681. }
  3682. private string AnalysisSortColumn(string currentColumn, string sortDirection)
  3683. {
  3684. string retColumn = "";
  3685. string[] orderColumn = this.SortOrderColumnName.Split(';');
  3686. int i = 0;
  3687. for (i = 0; i < orderColumn.Length; i++)
  3688. {
  3689. string[] tmpColumn = orderColumn[i].Split(':');
  3690. if (currentColumn.Equals(tmpColumn[0]))
  3691. {
  3692. retColumn = tmpColumn[1];
  3693. break;
  3694. }
  3695. }
  3696. string[] sortCols = retColumn.Split(',');
  3697. retColumn = sortCols[0] + " " + sortDirection;
  3698. for (i = 1; i < sortCols.Length; i++)
  3699. {
  3700. retColumn = retColumn + "," + sortCols[0] + " " + sortDirection;
  3701. }
  3702. return retColumn;
  3703. }
  3704. #endregion
  3705. }
  3706. #region 剪贴板处理
  3707. public partial class dkDataGridView
  3708. {
  3709. #region 成员变量
  3710. private ClipboardType _clipboardType = ClipboardType.InnerAndDisp;
  3711. #endregion
  3712. #region 属性
  3713. [System.ComponentModel.DefaultValue(ClipboardType.InnerAndDisp)]
  3714. public ClipboardType ClipboardType
  3715. {
  3716. get
  3717. {
  3718. return _clipboardType;
  3719. }
  3720. set
  3721. {
  3722. _clipboardType = value;
  3723. }
  3724. }
  3725. #endregion
  3726. #region 重写方法/函数
  3727. protected override void OnSizeChanged(EventArgs e)
  3728. {
  3729. try
  3730. {
  3731. if (_isOpenTotalRow)
  3732. {
  3733. SetTxtTop();
  3734. SetTxtLeft();
  3735. }
  3736. base.OnSizeChanged(e);
  3737. }
  3738. catch (Exception ex)
  3739. {
  3740. throw ex;
  3741. }
  3742. }
  3743. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  3744. {
  3745. //if (keyData == Keys.Enter)
  3746. //{
  3747. // SendKeys.Send("{Tab}");
  3748. // return true;
  3749. //}
  3750. const int WM_KEYDOWN = 0x0100;
  3751. const int WM_KEYUP = 0x0101;
  3752. Keys keyCode = (Keys)(int)keyData & Keys.KeyCode;
  3753. switch (msg.Msg)
  3754. {
  3755. case WM_KEYDOWN:
  3756. break;
  3757. case WM_KEYUP:
  3758. if (keyCode == Keys.Delete)
  3759. {
  3760. if (this.ReadOnly || !this.Enabled)
  3761. {
  3762. return true;
  3763. }
  3764. if (0 == this.SelectedRows.Count)
  3765. {
  3766. if (this.EditingControl != null)
  3767. {
  3768. break;
  3769. }
  3770. DataGridViewCell cell = this.CurrentCell;
  3771. if (cell != null && !cell.ReadOnly)
  3772. {
  3773. if (!Utility.IsNull(cell.Value))
  3774. {
  3775. if (this.BeginEdit(false))
  3776. {
  3777. if (this.EditingControl != null)
  3778. {
  3779. this.EditingControl.Text = null;
  3780. }
  3781. this.EndEdit();
  3782. }
  3783. }
  3784. }
  3785. }
  3786. else
  3787. {
  3788. if (this.AllowUserToDeleteRows)
  3789. {
  3790. return false;
  3791. }
  3792. }
  3793. }
  3794. else if (keyCode == Keys.Escape)
  3795. {
  3796. return false;
  3797. }
  3798. break;
  3799. }
  3800. return base.ProcessCmdKey(ref msg, keyData);
  3801. }
  3802. #endregion
  3803. #region 公开方法/函数
  3804. public bool IsCopyData()
  3805. {
  3806. // DataTable信息转换成csv形式
  3807. string csvInner = null;
  3808. string csvDisp = null;
  3809. if (this.ClipboardType == ClipboardType.InnerAndDisp)
  3810. {
  3811. csvInner = ToCsvString(false, true, false, false);
  3812. csvDisp = ToCsvString(true, true, true, false);
  3813. }
  3814. else
  3815. {
  3816. csvInner = ToCsvString(false, true, false, false);
  3817. csvDisp = csvInner;
  3818. }
  3819. if (string.IsNullOrEmpty(csvDisp) || string.IsNullOrEmpty(csvInner))
  3820. {
  3821. return false;
  3822. }
  3823. // 往剪贴板上拷贝数据
  3824. DataObject dataObject = new DataObject();
  3825. dataObject.SetData(DataFormats.Text, csvDisp);
  3826. dataObject.SetData(ControlsConst.CLIPBOARD_FORMAT, csvInner);
  3827. Clipboard.SetDataObject(dataObject, true);
  3828. return true;
  3829. }
  3830. public bool IsPasteData()
  3831. {
  3832. if (this.ReadOnly || !this.Enabled)
  3833. {
  3834. // 不允许粘贴
  3835. return true;
  3836. }
  3837. if (this.CurrentCell != null)
  3838. {
  3839. if (this.CurrentCell.IsInEditMode && !(
  3840. this.CurrentCell is DataGridViewCheckBoxCell))
  3841. {
  3842. return false;
  3843. }
  3844. }
  3845. if (CurrentDataTable == null)
  3846. {
  3847. return false;
  3848. }
  3849. DataColumnCollection columns = CurrentDataTable.Columns;
  3850. IDataObject dataObject = Clipboard.GetDataObject();
  3851. // 剪贴板的CVS数据转换成DataTable
  3852. DataTable tableCsv = null;
  3853. object data = dataObject.GetData(ControlsConst.CLIPBOARD_FORMAT);
  3854. if (data == null)
  3855. {
  3856. if (dataObject.GetDataPresent(DataFormats.Text, true))
  3857. {
  3858. data = dataObject.GetData(DataFormats.Text, true);
  3859. if (data == null)
  3860. {
  3861. return true;
  3862. }
  3863. }
  3864. }
  3865. string csv = data.ToString();
  3866. tableCsv = CsvManager.ToDataTable(csv, false, '\t', '\"');
  3867. if (tableCsv == null)
  3868. {
  3869. return true;
  3870. }
  3871. List<int> columnIndexList;
  3872. int rowIndexMin, rowIndexMax;
  3873. GetSelectedRange(out columnIndexList, out rowIndexMin, out rowIndexMax);
  3874. // 设置DataColumn信息
  3875. DataTable table = new DataTable();
  3876. foreach (int columnIndex in columnIndexList)
  3877. {
  3878. DataGridViewColumn column = this.Columns[columnIndex];
  3879. if (column == null)
  3880. {
  3881. return true;
  3882. }
  3883. if (column.CellType.Name == "DataGridViewSearchFileBoxCellEx")
  3884. {
  3885. continue;
  3886. }
  3887. table.Columns.Add(column.DataPropertyName);
  3888. }
  3889. DataRowView rowView;
  3890. DataRow drowRow;
  3891. int rowCount = tableCsv.Rows.Count;
  3892. int viewRowCount = Rows.Count;
  3893. int columnCount = tableCsv.Columns.Count;
  3894. int j = 0;
  3895. for (int i = rowIndexMin; i <= rowIndexMax; i++)
  3896. {
  3897. try
  3898. {
  3899. if (columnIndexList.Count < Columns.Count)
  3900. {
  3901. if (!Rows[i].IsNewRow)
  3902. {
  3903. rowView = GetDataRowView(i);
  3904. if (rowView == null)
  3905. {
  3906. continue;
  3907. }
  3908. for (int k = 0; k < columnIndexList.Count; k++)
  3909. {
  3910. int columnIndex = columnIndexList[k];
  3911. if (!this.Rows[i].Cells[columnIndex].ReadOnly)
  3912. {
  3913. rowView[this.Columns[columnIndex].DataPropertyName] =
  3914. ConvertValue(tableCsv.Rows[j][k].ToString());
  3915. }
  3916. }
  3917. j++;
  3918. }
  3919. else
  3920. {
  3921. for (; j < rowCount; j++)
  3922. {
  3923. drowRow = CurrentDataTable.NewRow();
  3924. for (int k = 0; k < columnIndexList.Count; k++)
  3925. {
  3926. int columnIndex = columnIndexList[k];
  3927. if (!this.Rows[i].Cells[columnIndex].ReadOnly)
  3928. {
  3929. drowRow[this.Columns[columnIndex].DataPropertyName] =
  3930. ConvertValue(tableCsv.Rows[j][k].ToString());
  3931. }
  3932. }
  3933. CurrentDataTable.Rows.Add(drowRow);
  3934. }
  3935. this.Rows.RemoveAt(this.CurrentCell.RowIndex);
  3936. }
  3937. }
  3938. else
  3939. {
  3940. if (!Rows[i].IsNewRow)
  3941. {
  3942. rowView = GetDataRowView(i);
  3943. if (rowView == null)
  3944. {
  3945. continue;
  3946. }
  3947. for (int k = 0; k < columnCount; k++)
  3948. {
  3949. try
  3950. {
  3951. if (!this.Rows[i].Cells[k].ReadOnly)
  3952. {
  3953. rowView[table.Columns[k].ColumnName] =
  3954. ConvertValue(tableCsv.Rows[j][k].ToString());
  3955. }
  3956. }
  3957. catch (ReadOnlyException)
  3958. {
  3959. }
  3960. }
  3961. rowView.EndEdit();
  3962. j++;
  3963. if (rowCount <= j)
  3964. {
  3965. break;
  3966. }
  3967. }
  3968. else
  3969. {
  3970. for (; j < rowCount; j++)
  3971. {
  3972. drowRow = CurrentDataTable.NewRow();
  3973. for (int k = 0; k < columnCount; k++)
  3974. {
  3975. try
  3976. {
  3977. if (!this.Rows[i].Cells[k].ReadOnly)
  3978. {
  3979. drowRow[table.Columns[k].ColumnName] =
  3980. ConvertValue(tableCsv.Rows[j][k].ToString());
  3981. }
  3982. }
  3983. catch (ReadOnlyException)
  3984. {
  3985. }
  3986. }
  3987. CurrentDataTable.Rows.Add(drowRow);
  3988. }
  3989. }
  3990. }
  3991. }
  3992. catch (IndexOutOfRangeException)
  3993. {
  3994. break;
  3995. }
  3996. catch (Exception ex)
  3997. {
  3998. Debug.Assert(false, "错误", ex.Message);
  3999. MessageBox.Show(ex.Message);
  4000. return true;
  4001. }
  4002. }
  4003. if (CurrentDataTable != null && CurrentDataTable.Rows.Count + 2 == this.Rows.Count)
  4004. {
  4005. this.Rows.RemoveAt(this.Rows.Count - 2);
  4006. }
  4007. if (this.CurrentCell != null)
  4008. {
  4009. this.Refresh();
  4010. }
  4011. return true;
  4012. }
  4013. /// <summary>
  4014. /// 打开合计行
  4015. /// </summary>
  4016. public void OpenTotalRow()
  4017. {
  4018. try
  4019. {
  4020. _isOpenTotalRow = true;
  4021. NewText(string.Empty, 0);//设置RowHeader对应的TextBox;
  4022. SetTxtLeft();
  4023. SetTxtTop();
  4024. this.HorizontalScrollBar.VisibleChanged += new EventHandler(HorizontalScrollBar_VisibleChanged);//水平
  4025. this.VerticalScrollBar.VisibleChanged += new EventHandler(VerticalScrollBar_VisibleChanged); //竖直
  4026. this.HorizontalScrollBar.ValueChanged += new EventHandler(HorizontalScrollBar_ValueChanged);
  4027. _columnInfos = this.GetDataGridViewColumnInfos();
  4028. if (_columnInfos != null)
  4029. {
  4030. for (int i = 0; i < _columnInfos.Length; i++)
  4031. {
  4032. if (_columnInfos[i].Visible)
  4033. {
  4034. NewText(_columnInfos[i].DataPropertyName, i);
  4035. }
  4036. }
  4037. }
  4038. }
  4039. catch (Exception ex)
  4040. {
  4041. throw ex;
  4042. }
  4043. }
  4044. void HorizontalScrollBar_ValueChanged(object sender, EventArgs e)
  4045. {
  4046. try
  4047. {
  4048. SetTxtLeft();
  4049. }
  4050. catch (Exception ex)
  4051. {
  4052. throw ex;
  4053. }
  4054. }
  4055. void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
  4056. {
  4057. try
  4058. {
  4059. if (VerticalScrollBar.Visible)
  4060. {
  4061. int width = 0;
  4062. if (findTextBox("txt" + this.Name) != null)
  4063. {
  4064. width = findTextBox("txt" + this.Name).Width;
  4065. }
  4066. this.VerticalScrollBar.Maximum = this.VerticalScrollBar.Maximum + width;
  4067. }
  4068. }
  4069. catch (Exception ex)
  4070. {
  4071. throw ex;
  4072. }
  4073. }
  4074. void HorizontalScrollBar_VisibleChanged(object sender, EventArgs e)
  4075. {
  4076. try
  4077. {
  4078. SetTxtTop();
  4079. }
  4080. catch (Exception ex)
  4081. {
  4082. throw ex;
  4083. }
  4084. }
  4085. //求和
  4086. protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
  4087. {
  4088. if (_isOpenTotalRow && IsAutoCountSum)
  4089. {
  4090. if (this.dt == null) return;
  4091. string s = this.CurrentCell.Value + "";
  4092. bool isValue = false;
  4093. foreach (DataColumn dc in dt.Columns)
  4094. {
  4095. if (dc.ColumnName == this.Columns[e.ColumnIndex].DataPropertyName)
  4096. {
  4097. if (TotalSumColumns != null && TotalSumColumns.Count > 0)
  4098. {
  4099. if (TotalSumColumns.Contains(dc.ColumnName))
  4100. {
  4101. isValue = dc.DataType.IsValueType;
  4102. break;
  4103. }
  4104. }
  4105. else
  4106. {
  4107. isValue = dc.DataType.IsValueType;
  4108. break;
  4109. }
  4110. }
  4111. }
  4112. decimal sumValue = 0;
  4113. if (isValue)
  4114. {
  4115. try
  4116. {
  4117. if (this.Columns[e.ColumnIndex].Visible)
  4118. {
  4119. object o = dt.Compute("SUM(" + this.Columns[e.ColumnIndex].DataPropertyName + ")", filter);
  4120. decimal.TryParse(o.ToString(), out sumValue);
  4121. string format = "F0";
  4122. if (!string.IsNullOrEmpty(this.Columns[e.ColumnIndex].DefaultCellStyle.Format))
  4123. {
  4124. format = this.Columns[e.ColumnIndex].DefaultCellStyle.Format;
  4125. }
  4126. if (IsSubTotalFlag)
  4127. {
  4128. if (!this.Columns[e.ColumnIndex].Name.Contains("Box") &&
  4129. !this.Columns[e.ColumnIndex].Name.Contains("Piece"))
  4130. {
  4131. findTextBox("txt" + this.Columns[e.ColumnIndex].Name.ToString()).Text
  4132. = (sumValue / 2).ToString(format);
  4133. }
  4134. else
  4135. {
  4136. findTextBox("txt" + this.Columns[e.ColumnIndex].Name.ToString()).Text
  4137. = sumValue.ToString(format);
  4138. }
  4139. }
  4140. else
  4141. {
  4142. findTextBox("txt" + this.Columns[e.ColumnIndex].Name.ToString()).Text
  4143. = sumValue.ToString(format);
  4144. }
  4145. }
  4146. }
  4147. catch (DataException)
  4148. { }
  4149. }
  4150. }
  4151. base.OnCellEndEdit(e);
  4152. }
  4153. protected override void OnDataSourceChanged(EventArgs e)
  4154. {
  4155. try
  4156. {
  4157. base.OnDataSourceChanged(e);
  4158. if (_isOpenTotalRow)
  4159. {
  4160. if (dt == null || _columnInfos == null) return;
  4161. // 解决合计行遮住最后一行的bug
  4162. if (dt.Rows.Count > 0)
  4163. {
  4164. int height = dt.Rows.Count * this.CellHeight + this.ColumnHeadersHeight;
  4165. if (findTextBox("txt" + this.Name) != null)
  4166. {
  4167. height += findTextBox("txt" + this.Name).Height;
  4168. }
  4169. if (height > this.Height)
  4170. {
  4171. if (!this.AllowUserToAddRows)
  4172. {
  4173. this.AllowUserToAddRows = true;
  4174. this.ReadOnly = true;
  4175. }
  4176. }
  4177. else
  4178. {
  4179. if (this.AllowUserToAddRows)
  4180. {
  4181. this.AllowUserToAddRows = false;
  4182. }
  4183. }
  4184. }
  4185. }
  4186. }
  4187. catch (Exception ex)
  4188. {
  4189. throw ex;
  4190. }
  4191. }
  4192. /// <summary>
  4193. /// 重新计算合计行
  4194. /// </summary>
  4195. public void CountTotalRow()
  4196. {
  4197. try
  4198. {
  4199. if (_isOpenTotalRow)
  4200. {
  4201. if (dt == null || _columnInfos == null) return;
  4202. //for (int j = 0; j < dt.Rows.Count; j++)
  4203. //{
  4204. // for (int i = 0; i < dt.Columns.Count; i++)
  4205. // {
  4206. // if (("Decimal".Equals(dt.Columns[i].DataType.Name)
  4207. // || "Int".Equals(dt.Columns[i].DataType.Name)
  4208. // || "Double".Equals(dt.Columns[i].DataType.Name)))
  4209. // {
  4210. // DataGridViewCell cell = this.Rows[this.Rows.Count - 1].Cells[dt.Columns[i].ColumnName];
  4211. // dt.Rows[j][i] = cell.FormattedValue;
  4212. // }
  4213. // }
  4214. //}
  4215. // 解决合计行遮住最后一行的bug
  4216. //if (dt.Rows.Count > 0)
  4217. //{
  4218. // int height = dt.Rows.Count * this.CellHeight + this.ColumnHeadersHeight + findTextBox("txt").Height;
  4219. // if (height > this.Height)
  4220. // {
  4221. // if (!this.AllowUserToAddRows)
  4222. // {
  4223. // this.AllowUserToAddRows = true;
  4224. // this.ReadOnly = true;
  4225. // }
  4226. // }
  4227. // else
  4228. // {
  4229. // if (this.AllowUserToAddRows)
  4230. // {
  4231. // this.AllowUserToAddRows = false;
  4232. // }
  4233. // }
  4234. //}
  4235. string totalCountNameFormat = string.Empty;
  4236. for (int i = 0; i < _columnInfos.Length; i++)
  4237. {
  4238. if (!string.IsNullOrEmpty(TotalCountName) && TotalCountName.Equals(_columnInfos[i].DataPropertyName))
  4239. {
  4240. totalCountNameFormat = _columnInfos[i].DefaultCellStyle.Format;
  4241. }
  4242. bool isValue = false;
  4243. foreach (DataColumn dc in dt.Columns)
  4244. {
  4245. if (dc.ColumnName == _columnInfos[i].DataPropertyName)
  4246. {
  4247. if (TotalSumColumns != null && TotalSumColumns.Count > 0)
  4248. {
  4249. if (TotalSumColumns.Contains(dc.ColumnName))
  4250. {
  4251. isValue = dc.DataType.IsValueType;
  4252. break;
  4253. }
  4254. }
  4255. else
  4256. {
  4257. isValue = dc.DataType.IsValueType;
  4258. break;
  4259. }
  4260. }
  4261. }
  4262. decimal sumValue = 0;
  4263. if (isValue)
  4264. {
  4265. try
  4266. {
  4267. if (_columnInfos[i].Visible)
  4268. {
  4269. object o = dt.Compute("SUM([" + _columnInfos[i].DataPropertyName + "])", filter);
  4270. decimal.TryParse(o.ToString(), out sumValue);
  4271. TextBox tb = findTextBox("txt" + _columnInfos[i].DataPropertyName);
  4272. if (tb != null)
  4273. {
  4274. if (dt.Rows.Count == 0 && sumValue == 0)
  4275. {
  4276. tb.Text = string.Empty;
  4277. }
  4278. else
  4279. {
  4280. // 周兴 2016-10-10 修改 合计行去掉小数位数后多余的0
  4281. //if (!string.IsNullOrEmpty(_columnInfos[i].DefaultCellStyle.Format))
  4282. //{
  4283. // if (IsSubTotalFlag)
  4284. // {
  4285. // if (!_columnInfos[i].DataPropertyName.Contains("Box") &&
  4286. // !_columnInfos[i].DataPropertyName.Contains("Piece"))
  4287. // {
  4288. // tb.Text = (sumValue / 2).ToString(_columnInfos[i].DefaultCellStyle.Format);
  4289. // }
  4290. // else
  4291. // {
  4292. // tb.Text = sumValue.ToString(_columnInfos[i].DefaultCellStyle.Format);
  4293. // }
  4294. // }
  4295. // else
  4296. // {
  4297. // tb.Text = sumValue.ToString(_columnInfos[i].DefaultCellStyle.Format);
  4298. // }
  4299. //}
  4300. //else
  4301. //{
  4302. // if (IsSubTotalFlag)
  4303. // {
  4304. // if (!_columnInfos[i].DataPropertyName.Contains("Box") &&
  4305. // !_columnInfos[i].DataPropertyName.Contains("Piece"))
  4306. // {
  4307. // tb.Text = (sumValue / 2) + "";
  4308. // }
  4309. // else
  4310. // {
  4311. // tb.Text = sumValue + "";
  4312. // }
  4313. // }
  4314. // else
  4315. // {
  4316. // tb.Text = sumValue + "";
  4317. // }
  4318. //}
  4319. if (IsSubTotalFlag)
  4320. {
  4321. if (!_columnInfos[i].DataPropertyName.Contains("Box") &&
  4322. !_columnInfos[i].DataPropertyName.Contains("Piece"))
  4323. {
  4324. tb.Text = (sumValue / 2).ToString("####.######");
  4325. }
  4326. else
  4327. {
  4328. tb.Text = sumValue.ToString("####.######");
  4329. }
  4330. }
  4331. else
  4332. {
  4333. tb.Text = sumValue.ToString("####.######");
  4334. }
  4335. }
  4336. }
  4337. }
  4338. }
  4339. catch (DataException)
  4340. { }
  4341. }
  4342. }
  4343. // 用合计的值进行重新计算
  4344. if (!string.IsNullOrEmpty(TotalCountName))
  4345. {
  4346. if (!string.IsNullOrEmpty(TotalCountFormula))
  4347. {
  4348. if (TotalCountFormula.IndexOf("|") >= 0)
  4349. {
  4350. string[] formulas = TotalCountFormula.Split('|');
  4351. string formula = formulas[0];
  4352. string formula1 = formulas[1]; // A1:ss,A2:tt
  4353. string[] formula2s = formula1.Split(',');
  4354. TextBox tb = null;
  4355. TextBox tboo = findTextBox("txt" + TotalCountName);
  4356. if (tboo != null)
  4357. {
  4358. for (int i = 0; i < formula2s.Length; i++)
  4359. {
  4360. string[] ss = formula2s[i].Split(':');
  4361. tb = findTextBox("txt" + ss[1]);
  4362. if (tb != null && !string.IsNullOrEmpty(tb.Text))
  4363. {
  4364. formula = formula.Replace(ss[0], tb.Text);
  4365. }
  4366. else
  4367. {
  4368. tboo.Text = string.Empty;
  4369. break;
  4370. }
  4371. }
  4372. try
  4373. {
  4374. // 计算值
  4375. MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
  4376. sc.Language = "JavaScript";
  4377. string result = sc.Eval(formula) + "";
  4378. if (!string.IsNullOrEmpty(result))
  4379. {
  4380. tboo.Text = Convert.ToDecimal(result).ToString("F2");
  4381. }
  4382. }
  4383. catch (Exception)
  4384. {
  4385. tboo.Text = string.Empty;
  4386. }
  4387. }
  4388. }
  4389. TextBox tbo = findTextBox("txt" + TotalCountName);
  4390. try
  4391. {
  4392. string fomulastring = TotalCountFormula;
  4393. string[] formula = fomulastring.Split(new char[] { '+', '-', '*', '/', '(', ')' });
  4394. TextBox tb = null;
  4395. for (int i = 0; i < formula.Length; i++)
  4396. {
  4397. tb = findTextBox("txt" + formula[i]);
  4398. if (tb != null && !string.IsNullOrEmpty(tb.Text))
  4399. {
  4400. fomulastring = fomulastring.Replace(formula[i], tb.Text);
  4401. }
  4402. else
  4403. {
  4404. tbo.Text = string.Empty;
  4405. break;
  4406. }
  4407. }
  4408. // 计算值
  4409. MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
  4410. sc.Language = "JavaScript";
  4411. string result = sc.Eval(fomulastring) + "";
  4412. if (!string.IsNullOrEmpty(result))
  4413. {
  4414. if (string.IsNullOrEmpty(totalCountNameFormat))
  4415. {
  4416. totalCountNameFormat = "####.######";
  4417. }
  4418. tbo.Text = Convert.ToDecimal(result).ToString(totalCountNameFormat);
  4419. }
  4420. }
  4421. catch (Exception)
  4422. {
  4423. tbo.Text = string.Empty;
  4424. }
  4425. }
  4426. }
  4427. }
  4428. }
  4429. catch (Exception ex)
  4430. {
  4431. throw ex;
  4432. }
  4433. }
  4434. /// <summary>
  4435. /// 清除合计行
  4436. /// </summary>
  4437. public void ClearTotalRow()
  4438. {
  4439. try
  4440. {
  4441. //if (_columnInfos != null)
  4442. //{
  4443. // //for (int i = 0; i < _columnInfos.Length; i++)
  4444. // //{
  4445. // // this.Controls.RemoveByKey("txt" + _columnInfos[i].DataPropertyName);
  4446. // //}
  4447. //}
  4448. for (int i = this.Controls.Count - 1; i >= 0; i--)
  4449. {
  4450. if (this.Controls[i].Name.StartsWith("txt") && !"txt".Equals(this.Controls[i].Name))
  4451. {
  4452. this.Controls.RemoveAt(i);
  4453. }
  4454. }
  4455. }
  4456. catch (Exception ex)
  4457. {
  4458. throw ex;
  4459. }
  4460. }
  4461. /// <summary>
  4462. /// 清除合计行
  4463. /// </summary>
  4464. public void ClearTotalRowData()
  4465. {
  4466. try
  4467. {
  4468. for (int i = this.Controls.Count - 1; i >= 0; i--)
  4469. {
  4470. if (this.Controls[i].Name.StartsWith("txt") && !"txt".Equals(this.Controls[i].Name))
  4471. {
  4472. TextBox tb = this.Controls[i] as TextBox;
  4473. if (tb != null)
  4474. {
  4475. tb.Clear();
  4476. }
  4477. }
  4478. }
  4479. }
  4480. catch (Exception ex)
  4481. {
  4482. throw ex;
  4483. }
  4484. }
  4485. /// <summary>
  4486. /// 重新生成合计行并计算合计行
  4487. /// </summary>
  4488. public void CountNewTextAndTotalRow()
  4489. {
  4490. try
  4491. {
  4492. if (_columnInfos != null)
  4493. {
  4494. for (int i = 0; i < _columnInfos.Length; i++)
  4495. {
  4496. this.Controls.RemoveByKey("txt" + _columnInfos[i].DataPropertyName);
  4497. }
  4498. }
  4499. _isOpenTotalRow = true;
  4500. _columnInfos = GetDataGridViewColumnInfos();
  4501. if (_columnInfos != null)
  4502. {
  4503. this.HorizontalScrollingOffset = 0;
  4504. NewText(string.Empty, 0);
  4505. for (int i = 0; i < _columnInfos.Length; i++)
  4506. {
  4507. if (_columnInfos[i].Visible)
  4508. {
  4509. NewText(_columnInfos[i].DataPropertyName, i);
  4510. }
  4511. }
  4512. CountTotalRow();
  4513. }
  4514. IsAutoResizeColumns = false;
  4515. }
  4516. catch (Exception ex)
  4517. {
  4518. throw ex;
  4519. }
  4520. }
  4521. //设置TextBox的Top,使之保持在DagtaGridView最下方;
  4522. private void SetTxtTop()
  4523. {
  4524. try
  4525. {
  4526. if (_columnInfos == null) return;
  4527. int topTxt = 0;
  4528. if (HorizontalScrollBar.Visible)
  4529. topTxt = this.Height - this.HorizontalScrollBar.Height;
  4530. else
  4531. topTxt = this.Height;
  4532. TextBox t = findTextBox("txt" + this.Name);
  4533. if (t != null)
  4534. {
  4535. t.Top = topTxt - t.Height;
  4536. for (int i = 0; i < _columnInfos.Length; i++)
  4537. {
  4538. if (this._columnInfos[i].Visible)
  4539. {
  4540. TextBox t1 = findTextBox("txt" + this._columnInfos[i].DataPropertyName);
  4541. if (t1 != null)
  4542. {
  4543. t1.Top = t.Top;
  4544. t1.TextAlign = HorizontalAlignment.Right;
  4545. }
  4546. }
  4547. }
  4548. }
  4549. }
  4550. catch (Exception ex)
  4551. {
  4552. throw ex;
  4553. }
  4554. }
  4555. //根据TextBox的名字找到TextBox;
  4556. private TextBox findTextBox(string txtName)
  4557. {
  4558. try
  4559. {
  4560. foreach (Control ctl in this.Controls)
  4561. {
  4562. if (ctl.Name == txtName)
  4563. return ctl as TextBox;
  4564. }
  4565. return null;
  4566. }
  4567. catch (Exception ex)
  4568. {
  4569. throw ex;
  4570. }
  4571. }
  4572. //设置TextBox的Left,使之与对应的Column的Left对应
  4573. private void SetTxtLeft()
  4574. {
  4575. try
  4576. {
  4577. if (_columnInfos == null) return;
  4578. int left = 0;
  4579. bool flag = false;
  4580. if (!_isFrozen)
  4581. {
  4582. flag = true;
  4583. }
  4584. for (int i = 0; i < _columnInfos.Length; i++)
  4585. {
  4586. if (_isFrozen)
  4587. {
  4588. if (!_columnInfos[i].Frozen)
  4589. {
  4590. flag = true;
  4591. }
  4592. }
  4593. if (flag)
  4594. {
  4595. TextBox tt = findTextBox("txt" + this._columnInfos[i].DataPropertyName);
  4596. if (tt != null)
  4597. {
  4598. left = getLeft(_columnInfos[i].DataPropertyName, i);
  4599. if (left < RowHeadersWidth - HorizontalScrollingOffset)
  4600. {
  4601. tt.Visible = false;
  4602. }
  4603. else
  4604. {
  4605. tt.TextAlign = HorizontalAlignment.Right;
  4606. tt.Visible = true;
  4607. tt.Left = left;
  4608. }
  4609. }
  4610. }
  4611. }
  4612. //for (int i = 0; i < this.FirstDisplayedScrollingColumnIndex; i++)
  4613. //{
  4614. // TextBox tt = findTextBox("txt" + this._columnInfos[i].DataPropertyName);
  4615. // if (tt != null)
  4616. // {
  4617. // tt.Visible = false;
  4618. // }
  4619. //}
  4620. //int firstIndex = FirstDisplayedScrollingColumnIndex > 0 ? FirstDisplayedScrollingColumnIndex : 0;
  4621. //for (int j = firstIndex; j < _columnInfos.Length; j++)
  4622. //{
  4623. // if (_columnInfos != null && _columnInfos[j].Visible)
  4624. // {
  4625. // TextBox t = findTextBox("txt" + _columnInfos[j].DataPropertyName);
  4626. // if (t != null)
  4627. // {
  4628. // t.TextAlign = HorizontalAlignment.Right;
  4629. // t.Visible = true;
  4630. // t.Left = getLeft(_columnInfos[j].DataPropertyName, j);
  4631. // }
  4632. // }
  4633. //}
  4634. }
  4635. catch (Exception ex)
  4636. {
  4637. throw ex;
  4638. }
  4639. }
  4640. //得到Column的Left
  4641. public int getLeft(string columnName, int endIndex)
  4642. {
  4643. try
  4644. {
  4645. if (string.IsNullOrEmpty(columnName) || _columnInfos == null) return 0;
  4646. int left = this.RowHeadersWidth;
  4647. //int left = this.RowHeadersWidth - this.FirstDisplayedScrollingColumnHiddenWidth;
  4648. //int firstIndex = FirstDisplayedScrollingColumnIndex > 0 ? FirstDisplayedScrollingColumnIndex : 0;
  4649. //for (int i = firstIndex; i < infos[columnName].Index; i++)
  4650. //{
  4651. // left += Columns[i].Width;
  4652. //}
  4653. for (int i = 0; i < endIndex; i++)
  4654. {
  4655. left += _columnInfos[i].Width;
  4656. }
  4657. left -= this.HorizontalScrollingOffset;
  4658. return left;
  4659. }
  4660. catch (Exception ex)
  4661. {
  4662. throw ex;
  4663. }
  4664. }
  4665. //生成TextBox;
  4666. private void NewText(string columnName, int columnIndex)
  4667. {
  4668. try
  4669. {
  4670. TextBox t = new TextBox();
  4671. t.ReadOnly = true;
  4672. if (string.IsNullOrEmpty(columnName))
  4673. {
  4674. TextBox t1 = findTextBox("txt" + this.Name);
  4675. if (t1 == null)
  4676. {
  4677. t.Name = "txt" + this.Name;
  4678. t.Width = this.RowHeadersWidth;
  4679. t.Text = "合计";
  4680. }
  4681. else
  4682. {
  4683. return;
  4684. }
  4685. }
  4686. else
  4687. {
  4688. t.TextAlign = HorizontalAlignment.Right;
  4689. t.Name = "txt" + columnName;
  4690. t.Width = _columnInfos[columnIndex].Width;
  4691. }
  4692. t.BackColor = System.Drawing.Color.LightBlue;
  4693. int topTxt = 0;
  4694. if (HorizontalScrollBar.Visible)
  4695. topTxt = this.Height - this.HorizontalScrollBar.Height;
  4696. else
  4697. topTxt = this.Height;
  4698. t.Top = topTxt - t.Height;
  4699. t.Left = getLeft(columnName, columnIndex);
  4700. if (findTextBox(t.Name) == null)
  4701. {
  4702. this.Controls.Add(t);
  4703. t.Show();
  4704. }
  4705. else
  4706. {
  4707. if (!("txt" + this.Name).Equals(t.Name))
  4708. {
  4709. this.Controls.RemoveByKey(t.Name);
  4710. this.Controls.Add(t);
  4711. t.Show();
  4712. }
  4713. else
  4714. {
  4715. this.Controls.Add(t);
  4716. t.Show();
  4717. }
  4718. }
  4719. }
  4720. catch (Exception ex)
  4721. {
  4722. throw ex;
  4723. }
  4724. }
  4725. #endregion
  4726. #region 受保护的方法/函数
  4727. protected void GetSelectedRange(out List<int> columnIndexList,
  4728. out int rowIndexMin, out int rowIndexMax)
  4729. {
  4730. List<int> list = new List<int>();
  4731. columnIndexList = new List<int>();
  4732. rowIndexMin = -1;
  4733. rowIndexMax = -1;
  4734. foreach (DataGridViewCell cell in this.SelectedCells)
  4735. {
  4736. if (rowIndexMin < 0)
  4737. {
  4738. // 第一次的情况
  4739. rowIndexMin = cell.RowIndex;
  4740. rowIndexMax = cell.RowIndex;
  4741. }
  4742. else
  4743. {
  4744. // 第一次以后的情况
  4745. if (cell.RowIndex < rowIndexMin)
  4746. {
  4747. rowIndexMin = cell.RowIndex;
  4748. }
  4749. if (rowIndexMax < cell.RowIndex)
  4750. {
  4751. rowIndexMax = cell.RowIndex;
  4752. }
  4753. }
  4754. if (!list.Contains(cell.ColumnIndex))
  4755. {
  4756. list.Add(cell.ColumnIndex);
  4757. }
  4758. }
  4759. while (true)
  4760. {
  4761. int minValue = int.MaxValue;
  4762. int minValueIndex = -1;
  4763. for (int i = 0; i < list.Count; i++)
  4764. {
  4765. if (this.Columns[list[i]].DisplayIndex < minValue)
  4766. {
  4767. minValue = this.Columns[list[i]].DisplayIndex;
  4768. minValueIndex = i;
  4769. }
  4770. }
  4771. if (minValueIndex < 0)
  4772. {
  4773. return;
  4774. }
  4775. int dataGridViewInnerColumnIndex = list[minValueIndex];
  4776. columnIndexList.Add(dataGridViewInnerColumnIndex);
  4777. list.RemoveAt(minValueIndex);
  4778. }
  4779. }
  4780. protected object ConvertValue(string value)
  4781. {
  4782. if (string.IsNullOrEmpty(value))
  4783. {
  4784. return DBNull.Value;
  4785. }
  4786. else
  4787. {
  4788. return value.Replace("\r\n", "\n").Replace("\n", "\r\n");
  4789. }
  4790. }
  4791. #endregion
  4792. }
  4793. #endregion
  4794. #region DataGridView信息的保存和复原
  4795. public partial class dkDataGridView
  4796. {
  4797. #region 成员变量
  4798. private bool _isSaveDataGridViewData = false; // 需要保存数据标识
  4799. private bool _isSaveDataGridSetting = true; // DataGridView设定数据
  4800. private DataGridViewSettings _settings = null;
  4801. #endregion
  4802. #region 属性
  4803. /// <summary>
  4804. /// 设定或者获取DataGridView设定数据(列的顺序·宽度)保存的标识
  4805. /// </summary>
  4806. [System.ComponentModel.Browsable(true)]
  4807. [System.ComponentModel.DefaultValue(true)]
  4808. [System.ComponentModel.Description("设定或者获取DataGridView设定数据(列的顺序·宽度)保存的标识")]
  4809. public bool IsSaveDataGridViewSetting
  4810. {
  4811. get
  4812. {
  4813. return _isSaveDataGridSetting;
  4814. }
  4815. set
  4816. {
  4817. _isSaveDataGridSetting = value;
  4818. }
  4819. }
  4820. [System.ComponentModel.Browsable(true)]
  4821. [System.ComponentModel.DefaultValue(false)]
  4822. [System.ComponentModel.Description("设定或者获取DataGridView数据保存的标识")]
  4823. public bool IsSaveDataGridViewData
  4824. {
  4825. get
  4826. {
  4827. return _isSaveDataGridViewData;
  4828. }
  4829. set
  4830. {
  4831. _isSaveDataGridViewData = value;
  4832. }
  4833. }
  4834. private DataGridViewSettings DataGridViewSettings
  4835. {
  4836. get
  4837. {
  4838. if (_settings == null)
  4839. {
  4840. _settings = new DataGridViewSettings();
  4841. System.Text.StringBuilder sbKey = new System.Text.StringBuilder();
  4842. Control control = this;
  4843. while (control != null)
  4844. {
  4845. sbKey.Append("_");
  4846. sbKey.Append(control.Name);
  4847. control = control.Parent;
  4848. }
  4849. _settings.SettingsKey = sbKey.ToString();
  4850. }
  4851. return _settings;
  4852. }
  4853. }
  4854. #endregion
  4855. #region 方法或者函数
  4856. public void SaveDataGridViewData()
  4857. {
  4858. if (!IsSaveDataGridViewData)
  4859. {
  4860. return;
  4861. }
  4862. this.DataGridViewSettings.DataGridViewColumnInfos = GetDataGridViewColumnInfos();
  4863. this.DataGridViewSettings.Save();
  4864. }
  4865. public void RestoreDataGridViewData()
  4866. {
  4867. if (!IsSaveDataGridViewData)
  4868. {
  4869. return;
  4870. }
  4871. DataGridViewColumnInfo[] infos = this.DataGridViewSettings.DataGridViewColumnInfos;
  4872. if (infos == null || infos.Length == 0)
  4873. {
  4874. return;
  4875. }
  4876. for (int i = 0; i < infos.Length; i++)
  4877. {
  4878. DataGridViewColumnInfo info = infos[i];
  4879. string strColumnName = info.ColumnName;
  4880. if (this.Columns.Contains(strColumnName))
  4881. {
  4882. if (this.Columns[strColumnName].DisplayIndex != i)
  4883. {
  4884. this.Columns[strColumnName].DisplayIndex = i;
  4885. }
  4886. this.Columns[strColumnName].Width = info.Width;
  4887. }
  4888. }
  4889. }
  4890. private DataGridViewColumnInfo[] GetDataGridViewColumnInfos()
  4891. {
  4892. List<DataGridViewColumnInfo> list = new List<DataGridViewColumnInfo>();
  4893. for (int i = 0; i < this.Columns.Count; i++)
  4894. {
  4895. foreach (DataGridViewColumn column in this.Columns)
  4896. {
  4897. if (column.DisplayIndex == i && column.Visible)
  4898. {
  4899. DataGridViewColumnInfo info = new DataGridViewColumnInfo();
  4900. info.ColumnName = column.Name;
  4901. info.ColumnText = column.HeaderText;
  4902. info.TreeColumnText = GetTreeColumnText(column);
  4903. info.Width = column.Width;
  4904. info.Visible = column.Visible;
  4905. info.DataPropertyName = column.DataPropertyName;
  4906. info.DefaultCellStyle = column.DefaultCellStyle;
  4907. info.Frozen = column.Frozen;
  4908. list.Add(info);
  4909. break;
  4910. }
  4911. }
  4912. }
  4913. return list.ToArray();
  4914. }
  4915. private string GetTreeColumnText(DataGridViewColumn column)
  4916. {
  4917. if (this.ColumnTreeViewNode != null)
  4918. {
  4919. object[] colList = ColumnList.ToArray();
  4920. TreeNode tn = (TreeNode)colList[column.Index];
  4921. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  4922. while (tn != null)
  4923. {
  4924. if (sb.Length > 0)
  4925. {
  4926. sb.Insert(0, tn.Name + "→");
  4927. }
  4928. else
  4929. {
  4930. sb.Append(column.HeaderText);
  4931. }
  4932. tn = tn.Parent;
  4933. }
  4934. return sb.ToString();
  4935. }
  4936. return column.HeaderText;
  4937. }
  4938. #endregion
  4939. }
  4940. #region DataGridView的列信息
  4941. [Serializable]
  4942. internal sealed class DataGridViewColumnInfo
  4943. {
  4944. public string ColumnName
  4945. {
  4946. get;
  4947. set;
  4948. }
  4949. public string DataPropertyName
  4950. {
  4951. get;
  4952. set;
  4953. }
  4954. public string ColumnText
  4955. {
  4956. get;
  4957. set;
  4958. }
  4959. public string TreeColumnText
  4960. {
  4961. get;
  4962. set;
  4963. }
  4964. public int Width
  4965. {
  4966. get;
  4967. set;
  4968. }
  4969. public bool Visible
  4970. {
  4971. get;
  4972. set;
  4973. }
  4974. public DataGridViewCellStyle DefaultCellStyle
  4975. {
  4976. get;
  4977. set;
  4978. }
  4979. public bool Frozen
  4980. {
  4981. get;
  4982. set;
  4983. }
  4984. }
  4985. #endregion
  4986. #region DataGridView信息保存类
  4987. [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
  4988. [global::System.CodeDom.Compiler.GeneratedCodeAttribute(
  4989. "Dongke.IBOSS.Framework.Controls.dkDataGridView", "0.0.0.0")]
  4990. internal sealed class DataGridViewSettings :
  4991. global::System.Configuration.ApplicationSettingsBase
  4992. {
  4993. private static DataGridViewSettings _defaultInstance = ((DataGridViewSettings)(
  4994. global::System.Configuration.ApplicationSettingsBase.Synchronized(
  4995. new DataGridViewSettings())));
  4996. public static DataGridViewSettings Default
  4997. {
  4998. get
  4999. {
  5000. return _defaultInstance;
  5001. }
  5002. }
  5003. [global::System.Configuration.UserScopedSettingAttribute()]
  5004. [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
  5005. [global::System.Configuration.DefaultSettingValueAttribute(null)]
  5006. [global::System.Configuration.SettingsSerializeAs(
  5007. System.Configuration.SettingsSerializeAs.Binary)]
  5008. public DataGridViewColumnInfo[] DataGridViewColumnInfos
  5009. {
  5010. get
  5011. {
  5012. return (DataGridViewColumnInfo[])this["DataGridViewColumnInfos"];
  5013. }
  5014. set
  5015. {
  5016. this["DataGridViewColumnInfos"] = value;
  5017. }
  5018. }
  5019. }
  5020. #endregion
  5021. #endregion
  5022. #region 过滤处理
  5023. public partial class dkDataGridView
  5024. {
  5025. #region 公开方法/函数
  5026. public void FilterRow(FilterType filterType)
  5027. {
  5028. try
  5029. {
  5030. if (this.CurrentCell == null)
  5031. {
  5032. return;
  5033. }
  5034. FilterRow(this.CurrentCell.ColumnIndex, filterType, this.CurrentCell.Value);
  5035. }
  5036. catch (Exception ex)
  5037. {
  5038. throw ex;
  5039. }
  5040. }
  5041. public void FilterRow(int columnIndex, FilterType filterType, object value)
  5042. {
  5043. try
  5044. {
  5045. // DataTable不存在的情况,返回
  5046. if (this.CurrentDataTable == null || this.CurrentDataTable.Rows.Count < 1)
  5047. {
  5048. return;
  5049. }
  5050. DataGridViewColumn column = this.Columns[columnIndex];
  5051. if (column == null)
  5052. {
  5053. return;
  5054. }
  5055. if (string.IsNullOrEmpty(column.DataPropertyName))
  5056. {
  5057. return;
  5058. }
  5059. if (!(this.DataSource is DataView))
  5060. {
  5061. this.CurrentDataTable.DefaultView.AllowDelete = this.AllowUserToDeleteRows;
  5062. this.CurrentDataTable.DefaultView.AllowEdit = this.AllowUserToModifyRows;
  5063. this.CurrentDataTable.DefaultView.AllowNew = this.AllowUserToAddRows;
  5064. //this.DataSource = this.CurrentDataTable.DefaultView.ToTable();
  5065. this.DataMember = string.Empty;
  5066. }
  5067. string strValueString = value as string;
  5068. if (!string.IsNullOrEmpty(strValueString))
  5069. {
  5070. if (2 < strValueString.Length)
  5071. {
  5072. if (!System.Text.RegularExpressions.Regex.IsMatch(strValueString, "^.[^\\%]*.$"))
  5073. {
  5074. MessageBox.Show(ControlsTips.DK_DataGridView_LikeError);
  5075. return;
  5076. }
  5077. }
  5078. }
  5079. bool isNumber = false;
  5080. string filter = string.Empty;
  5081. if (!string.IsNullOrEmpty(strValueString))
  5082. {
  5083. if (("Decimal".Equals(column.ValueType.Name)
  5084. || "Int".Equals(column.ValueType.Name)
  5085. || "Double".Equals(column.ValueType.Name)))
  5086. {
  5087. if (strValueString.StartsWith(">")
  5088. || strValueString.StartsWith("<")
  5089. || strValueString.StartsWith("="))
  5090. {
  5091. string tempValue = strValueString.Substring(1);
  5092. decimal tmpValueDec = 0;
  5093. bool flag = decimal.TryParse(tempValue, out tmpValueDec);
  5094. if (flag)
  5095. {
  5096. isNumber = true;
  5097. filter = column.DataPropertyName + strValueString;
  5098. }
  5099. }
  5100. else
  5101. {
  5102. if (strValueString.IndexOf("-") > 0)
  5103. {
  5104. string[] values = strValueString.Split('-');
  5105. decimal minValue = 0;
  5106. decimal maxValue = 0;
  5107. bool flag1 = decimal.TryParse(values[0], out minValue);
  5108. bool flag2 = decimal.TryParse(values[1], out maxValue);
  5109. if (flag1 && flag2)
  5110. {
  5111. isNumber = true;
  5112. filter = column.DataPropertyName + " > " + minValue + " AND " + column.DataPropertyName + " < " + maxValue;
  5113. }
  5114. }
  5115. }
  5116. }
  5117. }
  5118. if (!isNumber)
  5119. {
  5120. bool isHavedata = false;
  5121. if (!string.IsNullOrEmpty(strValueString)
  5122. && (strValueString.IndexOf(",") >= 0 || strValueString.IndexOf(",") >= 0))
  5123. {
  5124. string[] values = strValueString.Split(new char[] { ',', ',' });
  5125. if (("Decimal".Equals(column.ValueType.Name)
  5126. || "Int".Equals(column.ValueType.Name)
  5127. || "Double".Equals(column.ValueType.Name)))
  5128. {
  5129. decimal tmpValue = 0;
  5130. for (int i = 0; i < values.Length; i++)
  5131. {
  5132. bool flag = decimal.TryParse(values[i], out tmpValue);
  5133. if (flag)
  5134. {
  5135. isHavedata = true;
  5136. filter += column.DataPropertyName + " = " + tmpValue + " OR ";
  5137. }
  5138. }
  5139. if (isHavedata)
  5140. {
  5141. filter = filter.Substring(0, filter.Length - 4);
  5142. }
  5143. }
  5144. else
  5145. {
  5146. isHavedata = true;
  5147. for (int i = 0; i < values.Length; i++)
  5148. {
  5149. filter += column.DataPropertyName + " = '" + values[i] + "' OR ";
  5150. }
  5151. filter = filter.Substring(0, filter.Length - 4);
  5152. }
  5153. }
  5154. if (!isHavedata)
  5155. {
  5156. string format;
  5157. if (Utility.IsNull(value))
  5158. {
  5159. format = "CONVERT([{0}], 'System.String') {1}";
  5160. }
  5161. else
  5162. {
  5163. format = "ISNULL(CONVERT([{0}], 'System.String'), '') {1}";
  5164. }
  5165. filter = string.Format(format, column.DataPropertyName.Replace("]", "]]"),
  5166. GetOperatorValue(filterType, value, null));
  5167. if (!string.IsNullOrEmpty(this.CurrentDataTable.DefaultView.RowFilter))
  5168. {
  5169. filter += string.Format(" AND {0}",
  5170. this.CurrentDataTable.DefaultView.RowFilter);
  5171. }
  5172. }
  5173. }
  5174. if (this.CurrentCell != null)
  5175. {
  5176. this.CurrentCell = this.Rows[0].Cells[columnIndex];
  5177. }
  5178. this.CurrentDataTable.DefaultView.RowFilter = filter;
  5179. if (_isOpenTotalRow)
  5180. {
  5181. // 重新计算合计行
  5182. dt = this.CurrentDataTable.DefaultView.ToTable();
  5183. CountTotalRow();
  5184. }
  5185. }
  5186. catch (Exception ex)
  5187. {
  5188. throw ex;
  5189. }
  5190. }
  5191. public void ResetFilter()
  5192. {
  5193. try
  5194. {
  5195. if (this.CurrentDataTable == null)
  5196. {
  5197. return;
  5198. }
  5199. if (this.CurrentCell != null)
  5200. {
  5201. this.CurrentCell = this.Rows[0].Cells[this.CurrentCell.ColumnIndex];
  5202. }
  5203. if (_isChildDGV && !string.IsNullOrEmpty(_viewRowFilterString))
  5204. {
  5205. this.CurrentDataTable.DefaultView.RowFilter = _viewRowFilterString;
  5206. }
  5207. else
  5208. {
  5209. this.CurrentDataTable.DefaultView.RowFilter = string.Empty;
  5210. }
  5211. if (_isOpenTotalRow)
  5212. {
  5213. // 重新计算合计行
  5214. dt = this.CurrentDataTable.DefaultView.ToTable();
  5215. CountTotalRow();
  5216. }
  5217. }
  5218. catch (Exception ex)
  5219. {
  5220. throw ex;
  5221. }
  5222. }
  5223. #endregion
  5224. #region 受保护的方法/函数
  5225. protected string GetOperatorValue(FilterType filterType, object value, Type columnType)
  5226. {
  5227. if (Utility.IsNull(value))
  5228. {
  5229. switch (filterType)
  5230. {
  5231. case FilterType.NotEqual:
  5232. case FilterType.NotLike:
  5233. return "IS NOT NULL";
  5234. case FilterType.Equal:
  5235. case FilterType.Like:
  5236. default:
  5237. return "IS NULL";
  5238. }
  5239. }
  5240. else
  5241. {
  5242. string ope = "=";
  5243. switch (filterType)
  5244. {
  5245. case FilterType.NotEqual:
  5246. ope = "<>";
  5247. break;
  5248. case FilterType.Like:
  5249. ope = "LIKE";
  5250. value = ConvertRegularExpressionFilter(value + "");
  5251. break;
  5252. case FilterType.NotLike:
  5253. ope = "NOT LIKE";
  5254. value = ConvertRegularExpressionFilter(value + "");
  5255. break;
  5256. case FilterType.Equal:
  5257. default:
  5258. ope = "=";
  5259. break;
  5260. }
  5261. return string.Format("{0} '{1}'", ope, value);
  5262. }
  5263. }
  5264. #endregion
  5265. }
  5266. #endregion
  5267. #region 右键菜单处理
  5268. #region 右键菜单(过滤功能)
  5269. public class ToolStripRefine : ToolStripControlHost
  5270. {
  5271. #region 构造函数
  5272. public ToolStripRefine()
  5273. : base(new RefineControl())
  5274. {
  5275. }
  5276. #endregion
  5277. #region 公开方法/函数
  5278. public new string Text
  5279. {
  5280. get
  5281. {
  5282. return RefineControlInstance.TextBox.Text;
  5283. }
  5284. }
  5285. #endregion
  5286. #region 受保护方法/函数
  5287. RefineControl RefineControlInstance
  5288. {
  5289. get
  5290. {
  5291. return (RefineControl)Control;
  5292. }
  5293. }
  5294. #endregion
  5295. #region 委托
  5296. public event EventHandler RefineTextFixed;
  5297. #endregion
  5298. #region 重写方法
  5299. protected override void OnSubscribeControlEvents(Control c)
  5300. {
  5301. base.OnSubscribeControlEvents(c);
  5302. RefineControlInstance.TextBox.KeyDown += new KeyEventHandler(RefineControl_KeyDown);
  5303. }
  5304. protected override void OnUnsubscribeControlEvents(Control c)
  5305. {
  5306. base.OnUnsubscribeControlEvents(c);
  5307. RefineControlInstance.TextBox.KeyDown -= new KeyEventHandler(RefineControl_KeyDown);
  5308. }
  5309. #endregion
  5310. #region 事件
  5311. private void RefineControl_KeyDown(object sender, KeyEventArgs e)
  5312. {
  5313. if (e.KeyCode == Keys.Enter)
  5314. {
  5315. if (RefineTextFixed != null)
  5316. {
  5317. RefineTextFixed(this, e);
  5318. }
  5319. }
  5320. }
  5321. #endregion
  5322. }
  5323. #endregion
  5324. #region 过滤控件
  5325. class RefineControl : Control
  5326. {
  5327. #region 成员变量
  5328. private Label lblRefine = new Label();
  5329. private TextBox txtRefine = new TextBox();
  5330. #endregion
  5331. #region 属性
  5332. public TextBox TextBox
  5333. {
  5334. get
  5335. {
  5336. return txtRefine;
  5337. }
  5338. }
  5339. #endregion
  5340. #region 构造函数
  5341. public RefineControl()
  5342. {
  5343. // lblRefine
  5344. lblRefine.AutoSize = true;
  5345. lblRefine.Location = new System.Drawing.Point(0, 5);
  5346. lblRefine.Name = "label1";
  5347. lblRefine.Size = new System.Drawing.Size(40, 12);
  5348. lblRefine.TabIndex = 1;
  5349. lblRefine.Text = ControlsTips.DK_DataGridView_Refine;
  5350. // txtRefine
  5351. txtRefine.Location = new System.Drawing.Point(62, 2);
  5352. txtRefine.Name = "textBox1";
  5353. txtRefine.Size = new System.Drawing.Size(100, 19);
  5354. txtRefine.TabIndex = 2;
  5355. txtRefine.PreviewKeyDown += new PreviewKeyDownEventHandler(txtRefine_PreviewKeyDown);
  5356. // RefineControl
  5357. this.Height = 23;
  5358. this.Width = 144;
  5359. this.Controls.Add(lblRefine);
  5360. this.Controls.Add(txtRefine);
  5361. }
  5362. #endregion
  5363. #region 事件
  5364. private void txtRefine_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  5365. {
  5366. if (e.Alt)
  5367. {
  5368. e.IsInputKey = true;
  5369. }
  5370. }
  5371. #endregion
  5372. }
  5373. #endregion
  5374. #endregion
  5375. #region CSV转换事件的参数类
  5376. public class CsvConvertingEventArgs : EventArgs
  5377. {
  5378. #region 成员变量
  5379. private int _columnIndex;
  5380. private object _value;
  5381. #endregion
  5382. #region 属性
  5383. public int ColumnIndex
  5384. {
  5385. get
  5386. {
  5387. return _columnIndex;
  5388. }
  5389. set
  5390. {
  5391. _columnIndex = value;
  5392. }
  5393. }
  5394. public object Value
  5395. {
  5396. get
  5397. {
  5398. return _value;
  5399. }
  5400. set
  5401. {
  5402. _value = value;
  5403. }
  5404. }
  5405. #endregion
  5406. #region 构造函数
  5407. public CsvConvertingEventArgs(int columnIndex, object value)
  5408. {
  5409. this.ColumnIndex = columnIndex;
  5410. this.Value = value;
  5411. }
  5412. #endregion
  5413. }
  5414. #endregion
  5415. }