DataGridViewEx.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.ComponentModel;
  9. using System.Data;
  10. namespace LYFZ.ComponentLibrary
  11. {
  12. public class DataGridViewEx : System.Windows.Forms.DataGridView
  13. {
  14. /// <summary>
  15. /// 实现分页后
  16. /// </summary>
  17. public DataGridViewEx(): base()
  18. {
  19. try
  20. { RefreshTheme(); }
  21. catch { }
  22. this.SetStyle(ControlStyles.DoubleBuffer, true);
  23. this.SetStyle(ControlStyles.UserPaint, true);
  24. this.SetStyle(ControlStyles.ResizeRedraw, true);
  25. this.UpdateStyles();
  26. this.ColumnDisplayIndexChanged += DataGridViewEx_ColumnDisplayIndexChanged;
  27. this.ColumnWidthChanged += DataGridViewEx_ColumnWidthChanged;
  28. this.Paint += DataGridViewEx_Paint;
  29. this.CellMouseDown += DataGridViewEx_CellMouseDown;
  30. this.RowsAdded += DataGridViewEx_RowsAdded;
  31. this.CellPainting += DataGridViewEx_CellPainting;
  32. this.KeyDown += DataGridViewEx_KeyDown;
  33. }
  34. /// <summary>
  35. /// 设置要隐藏的手机号字段名
  36. /// </summary>
  37. void SetHidePhoneCellsName()
  38. {
  39. if (this.Columns != null)
  40. {
  41. HidePhoneFindList.Clear();
  42. for (int i = 0; i < this.Columns.Count; i++)
  43. {
  44. DataGridViewColumn Column = this.Columns[i];
  45. if (_HidePhoneCellsNameList.Contains(Column.Name) || _HidePhoneCellsNameList.Contains(Column.HeaderText))
  46. {
  47. HidePhoneFindList.Add(Column.Name);
  48. }
  49. }
  50. }
  51. }
  52. // string[] HidePhoneFinds = "客户电话,电话,固定电话,介绍人电话,手机号码,本人手机,老公手机".Split(',');
  53. List<string> HidePhoneFindList = new List<string>();
  54. /// <summary>
  55. ///
  56. /// </summary>
  57. static List<string> _HidePhoneCellsNameList = new List<string>();//当前手机号字段列名
  58. /// <summary>
  59. /// 要将字段值显示为“*”号的字段列名(当前手机号字段列名)
  60. /// </summary>
  61. public static List<string> HidePhoneCellsNameList
  62. {
  63. get { return DataGridViewEx._HidePhoneCellsNameList; }
  64. }
  65. void DataGridViewEx_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
  66. {
  67. if (_HidePhoneCellsNameList.Count > 0)
  68. {
  69. if (e.RowIndex == 0)
  70. {
  71. SetHidePhoneCellsName();
  72. }
  73. if (LYFZ.BLL.BLL_ErpUser.GetRights(LYFZ.BLL.BLL_ErpUser.CurrentUserRights.OtherPermissionsSet, LYFZ.CustomAttributes.OperatingAuthority.ProhibitViewCustomerPhoneNumber) && !LYFZ.BLL.BLL_ErpUser.IsAdministrator() && HidePhoneCellsNameList.Count > 0)
  74. {
  75. try
  76. {
  77. foreach (string HidePhoneCellsName in HidePhoneFindList)
  78. {
  79. this.Rows[e.RowIndex].Cells[HidePhoneCellsName].Tag = this.Rows[e.RowIndex].Cells[HidePhoneCellsName].Value;
  80. this.Rows[e.RowIndex].Cells[HidePhoneCellsName].Value = LYFZ.Command.Command_Validate.HidePhone(this.Rows[e.RowIndex].Cells[HidePhoneCellsName].Value);
  81. }
  82. }
  83. catch { }
  84. }
  85. }
  86. }
  87. bool _EraseCell = false;
  88. /// <summary>
  89. /// 是否开启擦除单元格功能
  90. /// </summary>
  91. public bool EraseCell
  92. {
  93. get { return _EraseCell; }
  94. set { _EraseCell = value; }
  95. }
  96. public string EraseCellDrawLine
  97. {
  98. get { return "@EraseCellDrawLine@"; }
  99. }
  100. public string EraseCellDrawLineVEnd
  101. {
  102. get { return "@EraseCellDrawLine@VEnd"; }
  103. }
  104. public string EraseCellDrawLineHEnd
  105. {
  106. get { return "@EraseCellDrawLine@HEnd"; }
  107. }
  108. public string EraseCellDrawLineVHEnd
  109. {
  110. get { return "@EraseCellDrawLine@VHEnd"; }
  111. }
  112. /// <summary>
  113. /// 擦除单元格
  114. /// </summary>
  115. /// <param name="e"></param>
  116. void EraseCellPainting(DataGridViewCellPaintingEventArgs e)
  117. {
  118. if (EraseCell)
  119. {
  120. if (e.RowIndex < 0 || e.ColumnIndex < 0) { return; }
  121. Brush gridBrush = new SolidBrush(this.GridColor);
  122. SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
  123. object EraseCellValue = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag;
  124. if (EraseCellValue != null && (EraseCellValue.ToString().ToLower().Contains(EraseCellDrawLine.ToLower())))
  125. {
  126. // cellwidth = e.CellBounds.Width;
  127. Pen gridLinePen = new Pen(gridBrush);
  128. if (this.Rows[e.RowIndex].Selected)
  129. {
  130. backBrush.Color = e.CellStyle.SelectionBackColor;
  131. }
  132. //以背景色填充
  133. e.Graphics.FillRectangle(backBrush, e.CellBounds);
  134. if (EraseCellValue.ToString().ToLower() == EraseCellDrawLineHEnd.ToLower() || EraseCellValue.ToString().ToLower() == EraseCellDrawLineVHEnd.ToLower())
  135. {
  136. //画下面的线
  137. e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
  138. }
  139. if (EraseCellValue.ToString().ToLower() == EraseCellDrawLineVEnd.ToLower() || EraseCellValue.ToString().ToLower() == EraseCellDrawLineVHEnd.ToLower())
  140. {
  141. // 画右边线
  142. e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
  143. }
  144. e.Handled = true;
  145. }
  146. }
  147. }
  148. /// <summary>
  149. /// 纵向合并,即合并数据项的值
  150. /// </summary>
  151. /// <param name="e"></param>
  152. private void DataGridViewEx_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  153. {
  154. EraseCellPainting(e);
  155. }
  156. void DataGridViewEx_KeyDown(object sender, KeyEventArgs e)
  157. {
  158. if (e.KeyData == (Keys.Control | Keys.C))
  159. {
  160. e.Handled = true;
  161. }
  162. }
  163. /// <summary>
  164. /// 绘制合并以后的值(纵向) 暂不使用
  165. /// </summary>
  166. /// <param name="e"></param>
  167. /// <param name="cellwidth"></param>
  168. /// <param name="UpRows"></param>
  169. /// <param name="DownRows"></param>
  170. /// <param name="count"></param>
  171. private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count)
  172. {
  173. string stValue = "";
  174. if (e.Value != DBNull.Value)
  175. {
  176. stValue = e.Value.ToString();
  177. }
  178. int fontheight = 0;
  179. int fontwidth = 0;
  180. using (SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor))
  181. {
  182. fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
  183. fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
  184. int cellheight = e.CellBounds.Height;
  185. if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
  186. {
  187. e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y + cellheight * DownRows - fontheight);
  188. }
  189. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
  190. {
  191. e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y + cellheight * DownRows - fontheight);
  192. }
  193. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
  194. {
  195. e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y + cellheight * DownRows - fontheight);
  196. }
  197. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
  198. {
  199. e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
  200. }
  201. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
  202. {
  203. e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
  204. }
  205. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
  206. {
  207. e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
  208. }
  209. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
  210. {
  211. e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1));
  212. }
  213. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
  214. {
  215. e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1));
  216. }
  217. else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
  218. {
  219. e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1));
  220. }
  221. else
  222. {
  223. e.Graphics.DrawString(stValue, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2);
  224. }
  225. }
  226. }
  227. bool _isCopyCellValue = true;
  228. /// <summary>
  229. /// 是否能复制单元格的Value值
  230. /// </summary>
  231. [
  232. Category("CollapseDataGridViewProperties"),
  233. Description("是否能复制单元格的Value值"),
  234. Bindable(true)
  235. ]
  236. public bool IsCopyCellValue
  237. {
  238. get { return _isCopyCellValue; }
  239. set { _isCopyCellValue = value; }
  240. }
  241. public ContextMenuStripEx conMenu = new ContextMenuStripEx();
  242. void DataGridViewEx_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
  243. {
  244. if (this.Rows.Count > 0)
  245. {
  246. if (this.CurrentRow != null)
  247. {
  248. if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
  249. {
  250. if (e.Button == System.Windows.Forms.MouseButtons.Right)
  251. {
  252. this.conMenu.Items.Clear();
  253. if (IsCopyCellValue)
  254. {
  255. // conMenu.Show(MousePosition.X, MousePosition.Y);//弹出操作菜单
  256. conMenu.ItemClicked -= conMenu_ItemClicked;
  257. conMenu.ItemClicked += conMenu_ItemClicked;
  258. ToolStripMenuItem item = new ToolStripMenuItem();
  259. item.Text = "复制" + this.Columns[e.ColumnIndex].HeaderText;
  260. item.Tag = this.CurrentRow.Index + "|" + e.ColumnIndex;
  261. item.Name = "复制列";
  262. conMenu.Items.Add(item);
  263. }
  264. if (LYFZ.ComponentLibrary.DataGridViewEx.HidePhoneCellsNameList.Contains(this.Columns[e.ColumnIndex].HeaderText.Trim()))
  265. {
  266. if (LYFZ.BLL.OtherCommonModel.GetUserCanViewPhoneCount(LYFZ.BLL.BLL_ErpUser.UsersModel.User_EmployeeID) > 0)
  267. {
  268. //conMenu.Show(MousePosition.X, MousePosition.Y);//弹出操作菜单
  269. conMenu.ItemClicked -= conMenu_ItemClicked;
  270. conMenu.ItemClicked += conMenu_ItemClicked;
  271. ToolStripMenuItem item = new ToolStripMenuItem();
  272. item.Text = "查看完整" + this.Columns[e.ColumnIndex].HeaderText;
  273. item.Tag = this.CurrentRow.Index + "|" + e.ColumnIndex;
  274. item.Name = "查看列";
  275. conMenu.Items.Add(item);
  276. }
  277. }
  278. if (conMenu.Items.Count > 0)
  279. {
  280. conMenu.Show(MousePosition.X, MousePosition.Y);//弹出操作菜单
  281. }
  282. }
  283. }
  284. else if (e.RowIndex == -1)
  285. {
  286. if (this.SelectedRows.Count > 3)
  287. {
  288. if (e.Button == System.Windows.Forms.MouseButtons.Right)
  289. {
  290. try
  291. {
  292. decimal decAoumnt = 0;
  293. for (int i = 0; i < this.Rows.Count; i++)
  294. {
  295. decAoumnt += Convert.ToDecimal(this.Rows[i].Cells[e.ColumnIndex].Value);
  296. }
  297. MessageBoxCustom.Show(decAoumnt.ToString().Trim());
  298. }
  299. catch { }
  300. }
  301. }
  302. }
  303. }
  304. }
  305. }
  306. public delegate void DataGridView_ConMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e);
  307. /// <summary>
  308. /// 用event 关键字声明事件对象
  309. /// </summary>
  310. [Category("控件扩展事件"), Description("列表右击显示菜单事件")]
  311. public event DataGridView_ConMenu_ItemClicked DataGridViewConMenu_ItemClicked;
  312. void conMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  313. {
  314. ToolStripItem item = e.ClickedItem;
  315. if (!string.IsNullOrEmpty(item.Name) && item.Name.Trim() == "复制列")
  316. {
  317. if (!string.IsNullOrEmpty(item.Text.Trim()) && item.Tag != null)
  318. {
  319. string[] StrTag = item.Tag.ToString().Trim().Split('|');
  320. Clipboard.SetDataObject(this.Rows[Convert.ToInt32(StrTag[0])].Cells[Convert.ToInt32(StrTag[1])].Value.ToString().Trim());
  321. }
  322. }
  323. else if (!string.IsNullOrEmpty(item.Name) && item.Name.Trim() == "查看列")
  324. {
  325. if (!string.IsNullOrEmpty(item.Text.Trim()) && item.Tag != null)
  326. {
  327. string[] StrTag = item.Tag.ToString().Trim().Split('|');
  328. if (LYFZ.BLL.OtherCommonModel.UserCanViewPhoneCount == -2)
  329. {
  330. DataTable dt = new LYFZ.BLL.BLL_ErpOrder().GetView_Custom("tb_ErpUser", StrWhere: "User_EmployeeID='" + LYFZ.BLL.BLL_ErpUser.UsersModel.User_EmployeeID + "'", ShowColumnName: "User_LookPhoneQuantity").Tables[0];
  331. if (dt.Rows.Count > 0)
  332. { LYFZ.BLL.OtherCommonModel.UserCanViewPhoneCount = Convert.ToInt32(dt.Rows[0]["User_LookPhoneQuantity"]); }
  333. else
  334. { LYFZ.BLL.OtherCommonModel.UserCanViewPhoneCount = 0; }
  335. }
  336. LYFZ.ComponentLibrary.frmFullPhoneNumber frm = new frmFullPhoneNumber();
  337. frm.StrUserNumber = LYFZ.BLL.BLL_ErpUser.UsersModel.User_EmployeeID;
  338. frm.StrPhone = this.Rows[Convert.ToInt32(StrTag[0])].Cells[Convert.ToInt32(StrTag[1])].Tag.ToString().Trim();
  339. frm.ShowDialog();
  340. }
  341. }
  342. else
  343. {
  344. if (this.DataGridViewConMenu_ItemClicked != null)
  345. { this.DataGridViewConMenu_ItemClicked(sender, e); }
  346. }
  347. }
  348. /// <summary>
  349. /// 刷新主题
  350. /// </summary>
  351. public void RefreshTheme()
  352. {
  353. try
  354. {
  355. // BackgroundColor = Color.White; //背景颜色
  356. // BorderStyle = BorderStyle.Fixed3D; //边框样式
  357. // ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None; //列标题边框样式
  358. // GridColor = Color.Silver; //单元格网状颜色
  359. // AllowUserToAddRows = false; //是否显示增加列
  360. // AllowUserToDeleteRows = false; //是否允许删除
  361. // AllowUserToOrderColumns = true; //是否手动放置
  362. SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  363. AllowUserToResizeRows = false;//不可设置行高
  364. // RowsDefaultCellStyle.SelectionBackColor = Color.SkyBlue;
  365. // //改变标题的高度;
  366. // ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
  367. // ColumnHeadersHeight = 50;
  368. // //设置标题内容居中显示;
  369. // ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  370. //// RowHeadersDefaultCellStyle.Alignment =DataGridViewCellStyle;
  371. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
  372. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
  373. AllowUserToAddRows = false;
  374. AllowUserToDeleteRows = false;
  375. dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightCyan;
  376. AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
  377. BackgroundColor = System.Drawing.Color.White;
  378. BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  379. ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
  380. dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240
  381. dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(43, 108, 150);//标题背景色
  382. dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  383. dataGridViewCellStyle2.ForeColor = System.Drawing.Color.White; //标题文字色
  384. dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
  385. dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
  386. ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;
  387. ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  388. this.ColumnHeadersHeight = 24;
  389. EnableHeadersVisualStyles = false;
  390. GridColor = System.Drawing.SystemColors.GradientInactiveCaption;
  391. //ReadOnly = true;
  392. RowHeadersVisible = false;
  393. RowTemplate.Height = 23;
  394. RowTemplate.ReadOnly = true;
  395. }
  396. catch { }
  397. }
  398. /// <summary>
  399. ///
  400. /// </summary>
  401. protected override void CreateHandle()
  402. {
  403. if (!IsHandleCreated)
  404. {
  405. try
  406. { base.CreateHandle(); }
  407. catch { }
  408. finally
  409. {
  410. if (!IsHandleCreated)
  411. { base.RecreateHandle(); }
  412. }
  413. }
  414. }
  415. /// <summary>
  416. ///
  417. /// </summary>
  418. /// <param name="sender"></param>
  419. /// <param name="e"></param>
  420. void DataGridViewEx_Paint(object sender, PaintEventArgs e)
  421. {
  422. try
  423. {
  424. Pen p = new Pen(LYFZ.ComponentLibrary.GetUIResources.DataGridViewExBorderColor);
  425. // p.Color = LYFZ.ComponentLibrary.GetUIResources.DataGridViewExBorderColor;
  426. e.Graphics.DrawRectangle(p, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
  427. }
  428. catch
  429. { }
  430. }
  431. /// <summary>
  432. ///
  433. /// </summary>
  434. /// <param name="sender"></param>
  435. /// <param name="e"></param>
  436. void DataGridViewEx_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
  437. { SaveGridViewDisplayIndex(); }
  438. /// <summary>
  439. ///
  440. /// </summary>
  441. /// <param name="sender"></param>
  442. /// <param name="e"></param>
  443. void cmsGridViewDisplayMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  444. {
  445. System.Windows.Forms.ToolStripMenuItem item = (System.Windows.Forms.ToolStripMenuItem)e.ClickedItem;
  446. item.Checked = !item.Checked;
  447. this.Columns[item.Text].Visible = item.Checked;
  448. string tempValue = LYFZ.WinAPI.CustomPublicMethod.GetColumnDisplayString(this.Columns);
  449. string _GridViewUniquelyIdentify = GridViewUniquelyIdentify;
  450. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate(object obj, BackgroundWorker backgroundWorker)
  451. {
  452. try
  453. {
  454. LYFZ.Model.Model_GridViewDisplayIndex model = LYFZ.DAL.DAL_GridViewDisplayIndex.GetGridViewDisplayIndexModel
  455. (LYFZ.BLL.BLL_ErpUser.UsersModel.User_Account, _GridViewUniquelyIdentify);
  456. model.DisplayIndexValue = tempValue;
  457. LYFZ.DAL.DAL_GridViewDisplayIndex.SaveGridViewDisplayIndex(model);
  458. }
  459. catch
  460. { }
  461. });
  462. // MessageBoxCustom.Show(tempValue);
  463. //throw new NotImplementedException();
  464. }
  465. /// <summary>
  466. /// 数据是否完成邦定
  467. /// </summary>
  468. bool isCompleteBonding = false;
  469. /// <summary>
  470. /// 数据是否完成邦定
  471. /// </summary>
  472. public bool IsCompleteBonding
  473. {
  474. get { return isCompleteBonding; }
  475. set { isCompleteBonding = value; }
  476. }
  477. //创建一个委托,是为访问DataGridVie控件服务的。
  478. public delegate void UpdateDataGridView();
  479. /// <summary>
  480. /// 清空所有行
  481. /// </summary>
  482. public void ClearAllRows()
  483. {
  484. System.Data.DataTable dtble = (System.Data.DataTable)this.DataSource;
  485. if (dtble != null)
  486. { dtble.Rows.Clear(); }
  487. }
  488. /// <summary>
  489. /// 邦定数据源
  490. /// </summary>
  491. /// <param name="ds"></param>
  492. /// <param name="sortModel"></param>
  493. /// <param name="cmsGridViewDisplayMenu"></param>
  494. /// <param name="isDisplayID"></param>
  495. /// <param name="hiddenFields">隐藏字段例表,多个时用“,”号分隔</param>
  496. public void DataGridViewBind(System.Data.DataSet ds, SortedColumnModel sortModel, System.Windows.Forms.ContextMenuStrip cmsGridViewDisplayMenu = null, bool isDisplayID = false, string hiddenFields = "")
  497. {
  498. if (ds != null)
  499. {
  500. try
  501. {
  502. LYFZ.Model.Model_GridViewDisplayIndex gDindexModel = LYFZ.DAL.DAL_GridViewDisplayIndex.GetGridViewDisplayIndexModel
  503. (LYFZ.BLL.BLL_ErpUser.UsersModel.User_Account, this.GridViewUniquelyIdentify);
  504. this.Invoke(new UpdateDataGridView(delegate()
  505. {
  506. if (this.ColumnHeadersHeight <= 30)
  507. { this.ColumnHeadersHeight = 30; }
  508. this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  509. this.IsCompleteBonding = false;
  510. this.DataSource = ds.Tables[0];
  511. try
  512. {
  513. this.Columns["rowId"].Visible = false;
  514. this.Columns["id"].Visible = isDisplayID;
  515. }
  516. catch
  517. { }
  518. ////this.SetSort(sortModel.ColumnName, sortModel.Direction);
  519. this.SetColumnDisplayIndex(gDindexModel.DisplayIndexValue, cmsGridViewDisplayMenu, hiddenFields);
  520. isCompleteBonding = true;
  521. }));
  522. }
  523. catch
  524. { }
  525. }
  526. }
  527. /// <summary>
  528. /// 显示标题菜单
  529. /// </summary>
  530. System.Windows.Forms.ContextMenuStrip displayTitleMenu = null;
  531. /// <summary>
  532. /// 设置字段显示序顺序和隐藏不显示字段
  533. /// </summary>
  534. public void SetColumnDisplayIndex(string _displayIndexValueStr, System.Windows.Forms.ContextMenuStrip cmsGridViewDisplayMenu = null, string hiddenFields = "")
  535. {
  536. try
  537. {
  538. isCompleteBonding = false;
  539. if (cmsGridViewDisplayMenu != null)
  540. {
  541. if (this.displayTitleMenu == null)
  542. {
  543. this.displayTitleMenu = cmsGridViewDisplayMenu;
  544. this.displayTitleMenu.ItemClicked += cmsGridViewDisplayMenu_ItemClicked;
  545. }
  546. }
  547. try
  548. { this._DisplayIndexValue = _displayIndexValueStr; }
  549. catch
  550. { }
  551. LYFZ.WinAPI.CustomPublicMethod.SetColumnDisplayIndex(this._DisplayIndexValue, this.Columns, this.displayTitleMenu, hiddenFields);
  552. }
  553. catch
  554. { }
  555. }
  556. /// <summary>
  557. /// 设置排序
  558. /// </summary>
  559. /// <param name="columnName"></param>
  560. /// <param name="direction"></param>
  561. public void SetSort(string columnName, ListSortDirection direction)
  562. { this.Sort(this.Columns[columnName], direction); }
  563. string _DisplayIndexValue = "";
  564. /// <summary>
  565. ///
  566. /// </summary>
  567. /// <param name="sender"></param>
  568. /// <param name="e"></param>
  569. void DataGridViewEx_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e)
  570. { SaveGridViewDisplayIndex(); }
  571. /// <summary>
  572. ///
  573. /// </summary>
  574. void SaveGridViewDisplayIndex()
  575. {
  576. try
  577. {
  578. if (isCompleteBonding)
  579. {
  580. string tempValue = LYFZ.WinAPI.CustomPublicMethod.GetColumnDisplayString(this.Columns);
  581. if (!this._DisplayIndexValue.Equals(tempValue))
  582. {
  583. this._DisplayIndexValue = tempValue;
  584. string _GridViewUniquelyIdentify = this.GridViewUniquelyIdentify;
  585. if (gridViewUniquelyIdentify.Trim() == "")
  586. {
  587. MessageBoxCustom.Show("DataGridView的唯一标识 GridViewUniquelyIdentify 值不能为空");
  588. return;
  589. }
  590. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate(object obj, BackgroundWorker backgroundWorker)
  591. {
  592. try
  593. {
  594. LYFZ.Model.Model_GridViewDisplayIndex model = LYFZ.DAL.DAL_GridViewDisplayIndex.GetGridViewDisplayIndexModel
  595. (LYFZ.BLL.BLL_ErpUser.UsersModel.User_Account, _GridViewUniquelyIdentify);
  596. model.DisplayIndexValue = _DisplayIndexValue;
  597. LYFZ.DAL.DAL_GridViewDisplayIndex.SaveGridViewDisplayIndex(model);
  598. }
  599. catch
  600. { }
  601. });
  602. }
  603. }
  604. }
  605. catch
  606. { }
  607. }
  608. string gridViewUniquelyIdentify = "";
  609. /// <summary>
  610. /// GridViewUniquelyIdentify唯一标识不能为空
  611. /// </summary>
  612. public string GridViewUniquelyIdentify
  613. {
  614. get { return gridViewUniquelyIdentify; }
  615. set
  616. {
  617. try
  618. {
  619. try
  620. { gridViewUniquelyIdentify = value + "_" + this.Parent.Name + "_" + this.Name; }
  621. catch
  622. { gridViewUniquelyIdentify = value + "_" + this.Name; }
  623. }
  624. catch
  625. { gridViewUniquelyIdentify = value; }
  626. }
  627. }
  628. /// <summary>
  629. /// 获取排序对象 请在邦定数据前获取
  630. /// </summary>
  631. /// <param name="defaultSortField"></param>
  632. /// <param name="defaultSortDirection"></param>
  633. /// <returns></returns>
  634. public SortedColumnModel GetSortedColumnModel(string defaultSortField = "id", ListSortDirection defaultSortDirection = ListSortDirection.Descending)
  635. {
  636. SortedColumnModel model = new SortedColumnModel();
  637. try
  638. {
  639. DataGridViewColumn sortColumn = this.SortedColumn;
  640. string orderStr = " " + defaultSortField + " desc ";
  641. string columnName = defaultSortField;
  642. if (defaultSortDirection == ListSortDirection.Descending)
  643. { orderStr = " " + defaultSortField + " desc "; }
  644. else
  645. { orderStr = " " + defaultSortField + " asc "; }
  646. ListSortDirection Direction = defaultSortDirection;
  647. if (sortColumn != null)
  648. {
  649. columnName = sortColumn.Name;
  650. switch (this.SortOrder)
  651. {
  652. case SortOrder.Ascending:
  653. orderStr = "[" + sortColumn.Name + "] asc ";
  654. Direction = ListSortDirection.Ascending;
  655. break;
  656. case SortOrder.Descending:
  657. orderStr = "[" + sortColumn.Name + "] desc ";
  658. Direction = ListSortDirection.Descending;
  659. break;
  660. default: break;
  661. }
  662. }
  663. model.Direction = Direction;
  664. model.OrderStr = orderStr.Trim();
  665. model.SortColumn = sortColumn;
  666. model.ColumnName = columnName;
  667. }
  668. catch { }
  669. return model;
  670. }
  671. /// <summary>
  672. /// 导出数据
  673. /// </summary>
  674. public void ExportDataTable()
  675. {
  676. DataGridViewExport.DataToExcel(this);
  677. }
  678. /// <summary>
  679. /// 导出数据
  680. /// </summary>
  681. public void DataToExcelPayroll(string StrExeclName = "")
  682. {
  683. DataGridViewExport.DataToExcelPayroll(this, StrExeclName);
  684. }
  685. /// <summary>
  686. /// 排序对象
  687. /// </summary>
  688. public class SortedColumnModel
  689. {
  690. public SortedColumnModel()
  691. { }
  692. DataGridViewColumn sortColumn = new DataGridViewColumn();
  693. /// <summary>
  694. /// 当前的排序字段,请在邦定数据前获取
  695. /// </summary>
  696. public DataGridViewColumn SortColumn
  697. {
  698. get { return sortColumn; }
  699. set { sortColumn = value; }
  700. }
  701. string orderStr = " id desc ";
  702. /// <summary>
  703. /// 获取排序字符串
  704. /// </summary>
  705. public string OrderStr
  706. {
  707. get { return orderStr; }
  708. set { orderStr = value; }
  709. }
  710. string columnName = "id";
  711. /// <summary>
  712. /// 排序字段名
  713. /// </summary>
  714. public string ColumnName
  715. {
  716. get { return columnName; }
  717. set { columnName = value; }
  718. }
  719. ListSortDirection direction = ListSortDirection.Descending;
  720. /// <summary>
  721. /// 排序方式
  722. /// </summary>
  723. public ListSortDirection Direction
  724. {
  725. get { return direction; }
  726. set { direction = value; }
  727. }
  728. }
  729. }
  730. #region
  731. public class DataGridViewExport
  732. {
  733. /// <summary>
  734. /// 导出DataGridView数据 以文本形式导出
  735. /// </summary>
  736. /// <param name="m_DataView"></param>
  737. public static void DataTableToExcel(DataTable m_DataView, bool IsShowSave = true, string StrFileName = "", string ExportOKPrompt = "")
  738. {
  739. string FileName = "";
  740. if (IsShowSave)
  741. {
  742. SaveFileDialog saveFile = new SaveFileDialog();
  743. saveFile.Title = "导出数据报表文件";
  744. saveFile.Filter = "EXECL文件(*.xls) |*.xls |文本文件(*.txt) |*.txt |所有文件(*.*) |*.*";
  745. saveFile.FilterIndex = 1;
  746. DialogResult drst = saveFile.ShowDialog();
  747. if (drst == DialogResult.No || drst == DialogResult.Cancel || drst == DialogResult.No || drst == DialogResult.Abort)
  748. { return; }
  749. FileName = saveFile.FileName;
  750. }
  751. else
  752. { FileName = StrFileName; }
  753. //定义表格内数据的行数和列数
  754. int rowscount = m_DataView.Rows.Count;
  755. int colscount = m_DataView.Columns.Count;
  756. //行数必须大于0 列数必须大于0
  757. if (rowscount <= 0 || colscount <= 0)
  758. {
  759. MessageBoxCustom.Show("没有数据可供保存 ");
  760. return;
  761. }
  762. //行数不可以大于65536
  763. if (rowscount > 65536)
  764. {
  765. MessageBoxCustom.Show("数据记录数太多(最多不能超过65536条),不能保存 ");
  766. return;
  767. }
  768. //列数不可以大于255
  769. if (colscount > 255)
  770. {
  771. MessageBoxCustom.Show("数据记录列数太多,不能保存 ");
  772. return;
  773. }
  774. if (File.Exists(FileName))
  775. File.Delete(FileName);
  776. FileStream objFileStream;
  777. StreamWriter objStreamWriter;
  778. string strLine = "";
  779. objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
  780. objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
  781. for (int i = 0; i < m_DataView.Columns.Count; i++)
  782. {
  783. strLine = strLine + m_DataView.Columns[i].ColumnName.Trim() + Convert.ToChar(9);
  784. }
  785. objStreamWriter.WriteLine(strLine);
  786. strLine = "";
  787. for (int i = 0; i < m_DataView.Rows.Count; i++)
  788. {
  789. for (int j = 0; j < m_DataView.Columns.Count; j++)
  790. {
  791. if (m_DataView.Rows[i][j].ToString().Trim() == null)
  792. strLine = strLine + " " + Convert.ToChar(9);
  793. else
  794. {
  795. string rowstr = "";
  796. rowstr = m_DataView.Rows[i][j].ToString().Trim();
  797. rowstr = rowstr.Replace("\r\n", " ");
  798. rowstr = rowstr.Replace("\t", " ");
  799. rowstr = rowstr.Replace("\r", " ");
  800. rowstr = rowstr.Replace("\n", " ");
  801. rowstr = rowstr.Trim();
  802. try
  803. {
  804. string tempValue = rowstr.Split(',')[0].Trim();
  805. if (tempValue.Length == 11)
  806. {
  807. long tempI = 0;
  808. if (long.TryParse(tempValue, out tempI))
  809. { rowstr = "'" + rowstr; }
  810. }
  811. }
  812. catch
  813. { }
  814. strLine = strLine + rowstr + Convert.ToChar(9);
  815. }
  816. }
  817. objStreamWriter.WriteLine(strLine);
  818. strLine = "";
  819. }
  820. objStreamWriter.Close();
  821. objFileStream.Close();
  822. if (string.IsNullOrEmpty(ExportOKPrompt))
  823. { MessageBoxCustom.Show("导出成功"); }
  824. else
  825. { MessageBoxCustom.Show(ExportOKPrompt); }
  826. }
  827. /// <summary>
  828. /// 导出DataGridView数据 以文本形式导出
  829. /// </summary>
  830. /// <param name="m_DataView"></param>
  831. public static void DataToExcel(DataGridView m_DataView)
  832. {
  833. SaveFileDialog saveFile = new SaveFileDialog();
  834. saveFile.Title = "导出数据报表文件";
  835. saveFile.Filter = "EXECL文件(*.xls) |*.xls |文本文件(*.txt) |*.txt |所有文件(*.*) |*.*";
  836. saveFile.FilterIndex = 1;
  837. if (saveFile.ShowDialog() == DialogResult.OK)
  838. {
  839. //定义表格内数据的行数和列数
  840. int rowscount = m_DataView.Rows.GetRowCount(DataGridViewElementStates.Visible);
  841. int colscount = m_DataView.Columns.GetColumnCount(DataGridViewElementStates.Visible);
  842. //行数必须大于0 列数必须大于0
  843. if (rowscount <= 0 || colscount <= 0)
  844. {
  845. MessageBoxCustom.Show("没有数据可供保存 ");
  846. return;
  847. }
  848. //行数不可以大于65536
  849. if (rowscount > 65536)
  850. {
  851. MessageBoxCustom.Show("数据记录数太多(最多不能超过65536条),不能保存 ");
  852. return;
  853. }
  854. //列数不可以大于255
  855. if (colscount > 255)
  856. {
  857. MessageBoxCustom.Show("数据记录列数太多,不能保存 ");
  858. return;
  859. }
  860. string FileName = saveFile.FileName;
  861. try
  862. {
  863. if (LYFZ.WinAPI.CustomPublicMethod.DataGridViewToExcel(FileName, m_DataView))
  864. {
  865. MessageBoxCustom.Show("导出成功");
  866. }
  867. else
  868. {
  869. MessageBoxCustom.Show("导出失败,请重试!");
  870. }
  871. }
  872. catch (Exception ex)
  873. {
  874. MessageBoxCustom.Show("导出失败,原因:" + ex.Message);
  875. }
  876. }
  877. }
  878. /// <summary>
  879. /// 导出DataGridView数据 以文本形式导出(只用于工资管理)
  880. /// </summary>
  881. /// <param name="m_DataView"></param>
  882. public static void DataToExcelPayroll(DataGridView m_DataView, string StrExeclName)
  883. {
  884. SaveFileDialog saveFileDialog = new SaveFileDialog();
  885. saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
  886. saveFileDialog.FilterIndex = 0;
  887. saveFileDialog.RestoreDirectory = true;
  888. saveFileDialog.CreatePrompt = true;
  889. saveFileDialog.FileName = StrExeclName;
  890. saveFileDialog.Title = "保存为Excel文件";
  891. saveFileDialog.CreatePrompt = false;
  892. saveFileDialog.OverwritePrompt = true;
  893. saveFileDialog.ShowDialog();
  894. Stream myStream;
  895. if (saveFileDialog.FileName.IndexOf(":") < 0) return; //被点了"取消"
  896. myStream = saveFileDialog.OpenFile();
  897. StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
  898. string str = "";
  899. Cursor.Current = Cursors.WaitCursor;
  900. try
  901. {
  902. //写标题
  903. for (int i = 0; i < m_DataView.ColumnCount; i++)
  904. {
  905. if (m_DataView.Columns[i].Visible)
  906. {
  907. if (i > 0)
  908. { str += "\t"; }
  909. str += m_DataView.Columns[i].HeaderText;
  910. }
  911. }
  912. sw.WriteLine(str);
  913. //写所有内容
  914. decimal decAllAmount = 0;
  915. for (int j = 0; j < m_DataView.Rows.Count; j++)
  916. {
  917. decAllAmount += Convert.ToDecimal(m_DataView.Rows[j].Cells["Column3"].Value);
  918. PaintedColumn(m_DataView, j, sw);
  919. }
  920. sw.WriteLine("总计:" + decAllAmount.ToString("n2"));
  921. sw.WriteLine("\r\n");
  922. sw.WriteLine("******以下为分类统计******");
  923. sw.WriteLine("\r\n");
  924. string StrSortColumnName = "Column8";// "Column17";
  925. m_DataView.Sort(m_DataView.Columns[StrSortColumnName], ListSortDirection.Ascending);
  926. decAllAmount = 0;
  927. string StrSortName = "";
  928. DataTable dt_tbl = new DataTable();
  929. for (int i = 0; i < m_DataView.Columns.Count; i++)
  930. {
  931. if (m_DataView.Columns[i].Visible)
  932. { dt_tbl.Columns.Add(m_DataView.Columns[i].Name.Trim(), typeof(string)); }
  933. }
  934. for (int j = 0; j < m_DataView.Rows.Count; j++)
  935. {
  936. switch (m_DataView.Rows[j].Cells[StrSortColumnName].Value.ToString().Trim())
  937. {
  938. case "全款":
  939. case "预约收款":
  940. case "预约补款":
  941. DataRow newRow = dt_tbl.NewRow();
  942. for (int i = 0; i < m_DataView.Columns.Count; i++)
  943. {
  944. try
  945. {
  946. if (m_DataView.Columns[i].Visible)
  947. {
  948. if (m_DataView.Rows[j].Cells[i].Value != null)
  949. { newRow[m_DataView.Columns[i].Name] = m_DataView.Rows[j].Cells[i].Value.ToString().Trim(); }
  950. else
  951. { newRow[m_DataView.Columns[i].Name] = ""; }
  952. }
  953. }
  954. catch
  955. { }
  956. }
  957. dt_tbl.Rows.Add(newRow);
  958. break;
  959. default:
  960. if (j == 0)
  961. {
  962. StrSortName = m_DataView.Rows[j].Cells[StrSortColumnName].Value.ToString().Trim();
  963. decAllAmount += Convert.ToDecimal(m_DataView.Rows[j].Cells["Column3"].Value);
  964. PaintedColumn(m_DataView, j, sw);
  965. }
  966. else
  967. {
  968. if (m_DataView.Rows[j].Cells[StrSortColumnName].Value.ToString().Trim() == m_DataView.Rows[j - 1].Cells[StrSortColumnName].Value.ToString().Trim())
  969. {
  970. PaintedColumn(m_DataView, j, sw);
  971. }
  972. else
  973. {
  974. sw.WriteLine(StrSortName + "收款统计:" + decAllAmount.ToString("n2"));
  975. StrSortName = "";
  976. decAllAmount = 0;
  977. sw.WriteLine("\r\n");
  978. PaintedColumn(m_DataView, j, sw);
  979. }
  980. StrSortName = m_DataView.Rows[j].Cells[StrSortColumnName].Value.ToString().Trim();
  981. decAllAmount += Convert.ToDecimal(m_DataView.Rows[j].Cells["Column3"].Value);
  982. }
  983. break;
  984. }
  985. }
  986. sw.WriteLine(StrSortName + "收款统计:" + decAllAmount.ToString("n2"));
  987. sw.WriteLine("\r\n");
  988. decAllAmount = 0;
  989. dt_tbl.DefaultView.Sort = StrSortColumnName + " ASC";
  990. for (int j = 0; j < dt_tbl.Rows.Count; j++)
  991. {
  992. if (j == 0)
  993. {
  994. try
  995. { StrSortName = dt_tbl.Rows[j][StrSortColumnName].ToString().Trim(); }
  996. catch { }
  997. decAllAmount += Convert.ToDecimal(dt_tbl.Rows[j]["Column3"]);
  998. PaintedColumn(dt_tbl, j, sw);
  999. }
  1000. else
  1001. {
  1002. if (dt_tbl.Rows[j][StrSortColumnName].ToString().Trim() == dt_tbl.Rows[j - 1][StrSortColumnName].ToString().Trim())
  1003. {
  1004. PaintedColumn(dt_tbl, j, sw);
  1005. }
  1006. else
  1007. {
  1008. sw.WriteLine(StrSortName + "收款统计:" + decAllAmount.ToString("n2"));
  1009. StrSortName = "";
  1010. decAllAmount = 0;
  1011. sw.WriteLine("\r\n");
  1012. PaintedColumn(dt_tbl, j, sw);
  1013. }
  1014. StrSortName = dt_tbl.Rows[j][StrSortColumnName].ToString().Trim();
  1015. decAllAmount += Convert.ToDecimal(dt_tbl.Rows[j]["Column3"]);
  1016. }
  1017. }
  1018. sw.WriteLine(StrSortName + "收款统计:" + decAllAmount.ToString("n2"));
  1019. sw.Close();
  1020. myStream.Close();
  1021. MessageBoxCustom.Show("导出完毕!");
  1022. }
  1023. catch (Exception l)
  1024. { MessageBox.Show(l.ToString()); }
  1025. finally
  1026. {
  1027. sw.Close();
  1028. myStream.Close();
  1029. }
  1030. }
  1031. static void PaintedColumn(DataGridView m_DataView, int RowI, StreamWriter sw)
  1032. {
  1033. string tempStr = "";
  1034. for (int k = 0; k < m_DataView.ColumnCount; k++)
  1035. {
  1036. if (m_DataView.Columns[k].Visible)
  1037. {
  1038. if (k > 0)
  1039. { tempStr += "\t"; }
  1040. if (m_DataView.Rows[RowI].Cells[k].Value != null)
  1041. { tempStr += m_DataView.Rows[RowI].Cells[k].Value.ToString(); }
  1042. else
  1043. { tempStr += ""; }
  1044. }
  1045. }
  1046. sw.WriteLine(tempStr);
  1047. }
  1048. static void PaintedColumn(DataTable dt_DataView, int RowI, StreamWriter sw)
  1049. {
  1050. string tempStr = "";
  1051. for (int k = 0; k < dt_DataView.Columns.Count; k++)
  1052. {
  1053. if (k > 0)
  1054. { tempStr += "\t"; }
  1055. if (dt_DataView.Rows[RowI][k] != null && !string.IsNullOrEmpty(dt_DataView.Rows[RowI][k].ToString().Trim()))
  1056. { tempStr += dt_DataView.Rows[RowI][k].ToString(); }
  1057. else
  1058. { tempStr += ""; }
  1059. }
  1060. sw.WriteLine(tempStr);
  1061. }
  1062. }
  1063. #endregion
  1064. }