WinGridViewPager.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using ProjectBase.Data.Pager;
  12. using DevExpress.XtraGrid.Columns;
  13. using DevExpress.XtraGrid.Views.Grid;
  14. using System.IO;
  15. using DevExpress.Utils;
  16. using DevExpress.XtraGrid.Views.Grid.ViewInfo;
  17. using ProjectBase.Controls.Others;
  18. using System.Diagnostics;
  19. namespace ProjectBase.Controls
  20. {
  21. /// <summary>
  22. /// 带分页导航的分页控件
  23. /// </summary>
  24. public partial class WinGridViewPager : DevExpress.XtraEditors.XtraUserControl
  25. {
  26. /// <summary>
  27. /// 是否显示CheckBox列
  28. /// </summary>
  29. public bool ShowCheckBox { get; set; }
  30. private object dataSource;//数据源
  31. private string displayColumns = "";//显示的列
  32. private string printTitle = "";//报表标题
  33. private Dictionary<string, int> columnDict = new Dictionary<string, int>();//字段列顺序
  34. private PagerInfo pagerInfo = null;
  35. private SaveFileDialog saveFileDialog = new SaveFileDialog();
  36. private bool isExportAllPage = false;//是否导出所有页
  37. private Dictionary<string, string> columnNameAlias = new Dictionary<string, string>();//字段别名字典集合
  38. private ContextMenuStrip appendedMenu;//右键菜单
  39. private bool m_ShowExportButton = true;//是否显示导出按钮
  40. private bool m_ShowAddMenu = true;//是否显示新建菜单
  41. private bool m_ShowEditMenu = true;//是否显示编辑菜单
  42. private bool m_ShowDeleteMenu = true;//是否显示删除菜单
  43. #region 菜单显示文本
  44. /// <summary>
  45. /// 新建菜单的显示内容
  46. /// </summary>
  47. public string AddMenuText = "新建(&N)";
  48. /// <summary>
  49. /// 编辑菜单的显示内容
  50. /// </summary>
  51. public string EditMenuText = "编辑选定项(&E)";
  52. /// <summary>
  53. /// 删除菜单的显示内容
  54. /// </summary>
  55. public string DeleteMenuText = "删除选定项(&D)";
  56. /// <summary>
  57. /// 刷新菜单的显示内容
  58. /// </summary>
  59. public string RefreshMenuText = "刷新列表(&R)";
  60. #endregion
  61. /// <summary>
  62. /// 导出全部的数据源
  63. /// </summary>
  64. public object AllToExport;
  65. /// <summary>
  66. /// 是否显示行号
  67. /// </summary>
  68. public bool ShowLineNumber = false;
  69. /// <summary>
  70. /// 获取或设置奇数行的背景色
  71. /// </summary>
  72. public Color EventRowBackColor = Color.LightCyan;
  73. /// <summary>
  74. /// 是否使用最佳宽度
  75. /// </summary>
  76. public bool BestFitColumnWith = true;
  77. /// <summary>
  78. /// 冻结列的固定样式,默认为左边
  79. /// </summary>
  80. public FixedStyle Fixed = FixedStyle.Left;
  81. /// <summary>
  82. /// 冻结列的字段,多个字段逗号分开
  83. /// </summary>
  84. public string FixedColumns { get; set; }
  85. #region 权限功能控制
  86. /// <summary>
  87. /// 是否显示导出按钮
  88. /// </summary>
  89. [Category("分页"), Description("是否显示导出按钮。"), Browsable(true)]
  90. public bool ShowExportButton
  91. {
  92. get { return m_ShowExportButton; }
  93. set
  94. {
  95. m_ShowExportButton = value;
  96. if (this.pager != null)
  97. {
  98. this.pager.ShowExportButton = this.ShowExportButton;
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// 是否显示新建菜单
  104. /// </summary>
  105. [Category("分页"), Description("是否显示新建菜单。"), Browsable(true)]
  106. public bool ShowAddMenu
  107. {
  108. get { return m_ShowAddMenu; }
  109. set { m_ShowAddMenu = value; }
  110. }
  111. /// <summary>
  112. /// 是否显示编辑菜单
  113. /// </summary>
  114. [Category("分页"), Description("是否显示编辑菜单。"), Browsable(true)]
  115. public bool ShowEditMenu
  116. {
  117. get { return m_ShowEditMenu; }
  118. set { m_ShowEditMenu = value; }
  119. }
  120. /// <summary>
  121. /// 是否显示删除菜单
  122. /// </summary>
  123. [Category("分页"), Description("是否显示删除菜单。"), Browsable(true)]
  124. public bool ShowDeleteMenu
  125. {
  126. get { return m_ShowDeleteMenu; }
  127. set { m_ShowDeleteMenu = value; }
  128. }
  129. #endregion
  130. /// <summary>
  131. /// 列名的别名字典集合
  132. /// </summary>
  133. public Dictionary<string, string> ColumnNameAlias
  134. {
  135. get { return columnNameAlias; }
  136. set
  137. {
  138. if (value != null)
  139. {
  140. foreach (string key in value.Keys)
  141. {
  142. AddColumnAlias(key, value[key]);
  143. }
  144. }
  145. }
  146. }
  147. #region 事件处理
  148. /// <summary>
  149. /// 导出Excel前执行的操作
  150. /// </summary>
  151. public event EventHandler OnStartExport;
  152. /// <summary>
  153. /// 导出Excel后执行的操作
  154. /// </summary>
  155. public event EventHandler OnEndExport;
  156. /// <summary>
  157. /// 页面变化的操作
  158. /// </summary>
  159. public event EventHandler OnPageChanged;
  160. /// <summary>
  161. /// 双击控件实现的操作,实现后出现右键菜单“编辑选定项”
  162. /// </summary>
  163. public event EventHandler OnEditSelected;
  164. /// <summary>
  165. /// 实现事件后出现“删除选定项”菜单项
  166. /// </summary>
  167. public event EventHandler OnDeleteSelected;
  168. /// <summary>
  169. /// 实现事件后出现“更新”菜单项
  170. /// </summary>
  171. public event EventHandler OnRefresh;
  172. /// <summary>
  173. /// 实现事件后,出现“新建”菜单项
  174. /// </summary>
  175. public event EventHandler OnAddNew;
  176. /// <summary>
  177. /// 实现对单击GirdView控件的响应
  178. /// </summary>
  179. public event EventHandler OnGridViewMouseClick;
  180. /// <summary>
  181. /// 实现对双击GirdView控件的响应
  182. /// </summary>
  183. public event EventHandler OnGridViewMouseDoubleClick;
  184. /// <summary>
  185. /// 实现对复选框选择变化的响应
  186. /// </summary>
  187. public event SelectionChangedEventHandler OnCheckBoxSelectionChanged;
  188. #endregion
  189. /// <summary>
  190. /// 追加的菜单项目
  191. /// </summary>
  192. public ContextMenuStrip AppendedMenu
  193. {
  194. get
  195. {
  196. return appendedMenu;
  197. }
  198. set
  199. {
  200. if (value != null)
  201. {
  202. appendedMenu = value;
  203. for (int i = 0; appendedMenu.Items.Count > 0; i++)
  204. {
  205. this.contextMenuStrip1.Items.Insert(i, appendedMenu.Items[0]);
  206. }
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// 构造函数
  212. /// </summary>
  213. public WinGridViewPager()
  214. {
  215. InitializeComponent();
  216. }
  217. private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
  218. {
  219. this.menu_Add.Visible = (this.OnAddNew != null && this.ShowAddMenu);
  220. this.menu_Delete.Visible = (this.OnDeleteSelected != null && this.ShowDeleteMenu);
  221. this.menu_Edit.Visible = (this.OnEditSelected != null && this.ShowEditMenu);
  222. this.menu_Refresh.Visible = (this.OnRefresh != null);
  223. this.menu_Add.Text = AddMenuText;
  224. this.menu_Edit.Text = EditMenuText;
  225. this.menu_Delete.Text = DeleteMenuText;
  226. this.menu_Refresh.Text = RefreshMenuText;
  227. }
  228. /// <summary>
  229. /// 封装的GridView对象
  230. /// </summary>
  231. public GridView GridView1
  232. {
  233. get
  234. {
  235. return this.gridView1;
  236. }
  237. }
  238. private void pager_PageChanged(object sender, EventArgs e)
  239. {
  240. if (OnPageChanged != null)
  241. {
  242. OnPageChanged(this, new EventArgs());
  243. }
  244. }
  245. /// <summary>
  246. /// 获取或设置数据源
  247. /// </summary>
  248. public object DataSource
  249. {
  250. get { return dataSource; }
  251. set
  252. {
  253. if (this.gridView1.Columns != null)
  254. {
  255. this.gridView1.Columns.Clear();
  256. }
  257. dataSource = value;
  258. this.gridControl1.DataSource = dataSource;
  259. this.pager.InitPageInfo(PagerInfo.RecordCount, PagerInfo.PageSize);
  260. }
  261. }
  262. private Dictionary<string, string> GetColumnNameTypes(DataTable dt)
  263. {
  264. Dictionary<string, string> dict = new Dictionary<string, string>();
  265. foreach (DataColumn col in dt.Columns)
  266. {
  267. if (!dict.ContainsKey(col.ColumnName))
  268. {
  269. dict.Add(col.ColumnName, col.DataType.FullName);
  270. }
  271. }
  272. return dict;
  273. }
  274. /// <summary>
  275. /// 显示的列内容,需要指定以防止GridView乱序
  276. /// 使用"|"或者","分开每个列,如“ID|Name”
  277. /// </summary>
  278. public string DisplayColumns
  279. {
  280. get { return displayColumns; }
  281. set
  282. {
  283. displayColumns = value;
  284. columnDict = new Dictionary<string, int>();
  285. string[] items = displayColumns.Split(new char[] { '|', ',' });
  286. for (int i = 0; i < items.Length; i++)
  287. {
  288. string str = items[i];
  289. if (!string.IsNullOrEmpty(str))
  290. {
  291. str = str.Trim();
  292. if (!columnDict.ContainsKey(str.ToUpper()))
  293. {
  294. columnDict.Add(str.ToUpper(), i);
  295. }
  296. }
  297. }
  298. }
  299. }
  300. /// <summary>
  301. /// 返回对应字段的显示顺序,如果没有,返回-1
  302. /// </summary>
  303. /// <param name="columnName"></param>
  304. /// <returns></returns>
  305. private int GetDisplayColumnIndex(string columnName)
  306. {
  307. int result = -1;
  308. if (columnDict.ContainsKey(columnName.ToUpper()))
  309. {
  310. result = columnDict[columnName.ToUpper()];
  311. }
  312. return result;
  313. }
  314. /// <summary>
  315. /// 添加列名的别名
  316. /// </summary>
  317. /// <param name="key">列的原始名称</param>
  318. /// <param name="alias">列的别名</param>
  319. public void AddColumnAlias(string key, string alias)
  320. {
  321. if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(alias))
  322. {
  323. if (!columnNameAlias.ContainsKey(key.ToUpper()))
  324. {
  325. columnNameAlias.Add(key.ToUpper(), alias);
  326. }
  327. else
  328. {
  329. columnNameAlias[key.ToUpper()] = alias;
  330. }
  331. }
  332. }
  333. /// <summary>
  334. /// 分页信息
  335. /// </summary>
  336. public PagerInfo PagerInfo
  337. {
  338. get
  339. {
  340. if (pagerInfo == null)
  341. {
  342. pagerInfo = new PagerInfo();
  343. pagerInfo.RecordCount = this.pager.RecordCount;
  344. pagerInfo.CurrenetPageIndex = this.pager.CurrentPageIndex;
  345. pagerInfo.PageSize = this.pager.PageSize;
  346. }
  347. else
  348. {
  349. pagerInfo.CurrenetPageIndex = this.pager.CurrentPageIndex;
  350. }
  351. return pagerInfo;
  352. }
  353. }
  354. /// <summary>
  355. /// 打印报表的抬头(标题)
  356. /// </summary>
  357. public string PrintTitle
  358. {
  359. get { return printTitle; }
  360. set { printTitle = value; }
  361. }
  362. /// <summary>
  363. /// 导出所有记录的事件
  364. /// </summary>
  365. private void pager_ExportAll(object sender, EventArgs e)
  366. {
  367. isExportAllPage = true;
  368. ExportToExcel();
  369. }
  370. /// <summary>
  371. /// 导出当前页记录的事件
  372. /// </summary>
  373. private void pager_ExportCurrent(object sender, EventArgs e)
  374. {
  375. isExportAllPage = false;
  376. ExportToExcel();
  377. }
  378. #region 导出Excel操作
  379. private void ExportToExcel()
  380. {
  381. saveFileDialog = new SaveFileDialog();
  382. saveFileDialog.Filter = "Excel (*.xls)|*.xls";
  383. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  384. {
  385. if (!saveFileDialog.FileName.Equals(String.Empty))
  386. {
  387. FileInfo f = new FileInfo(saveFileDialog.FileName);
  388. if (f.Extension.ToLower().Equals(".xls"))
  389. {
  390. StartExport(saveFileDialog.FileName);
  391. }
  392. else
  393. {
  394. MessageBox.Show("文件格式不正确");
  395. }
  396. }
  397. else
  398. {
  399. MessageBox.Show("需要指定一个保存的目录");
  400. }
  401. }
  402. }
  403. /// <summary>
  404. /// starts the export to new excel document
  405. /// </summary>
  406. /// <param name="filepath">the file to export to</param>
  407. private void StartExport(String filepath)
  408. {
  409. if (OnStartExport != null)
  410. {
  411. OnStartExport(this, new EventArgs());
  412. }
  413. BackgroundWorker bg = new BackgroundWorker();
  414. bg.DoWork += new DoWorkEventHandler(bg_DoWork);
  415. bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
  416. bg.RunWorkerAsync(filepath);
  417. }
  418. /// <summary>
  419. /// 使用背景线程导出Excel文档
  420. /// </summary>
  421. private void bg_DoWork(object sender, DoWorkEventArgs e)
  422. {
  423. DataTable table = new DataTable();
  424. if (AllToExport != null && isExportAllPage)
  425. {
  426. if (AllToExport is DataView)
  427. {
  428. DataView dv = (DataView)AllToExport;//默认导出显示内容
  429. table = dv.ToTable();
  430. }
  431. else if (AllToExport is DataTable)
  432. {
  433. table = AllToExport as DataTable;
  434. }
  435. else
  436. {
  437. table = ReflectionUtil.CreateTable(AllToExport);
  438. }
  439. //解析标题
  440. string originalName = string.Empty;
  441. foreach (DataColumn column in table.Columns)
  442. {
  443. originalName = column.Caption;
  444. if (columnNameAlias.ContainsKey(originalName.ToUpper()))
  445. {
  446. column.Caption = columnNameAlias[originalName.ToUpper()];
  447. column.ColumnName = columnNameAlias[originalName.ToUpper()];
  448. }
  449. }
  450. //for (int i = 0; i < this.gridView1.Columns.Count; i++)
  451. //{
  452. // if (!this.gridView1.Columns[i].Visible)
  453. // {
  454. // table.Columns.Remove(this.gridView1.Columns[i].FieldName);
  455. // }
  456. //}
  457. }
  458. else
  459. {
  460. DataColumn column;
  461. DataRow row;
  462. for (int i = 0; i < this.gridView1.Columns.Count; i++)
  463. {
  464. if (this.gridView1.Columns[i].Visible)
  465. {
  466. column = new DataColumn(this.gridView1.Columns[i].FieldName, typeof(string));
  467. column.Caption = this.gridView1.Columns[i].Caption;
  468. table.Columns.Add(column);
  469. }
  470. }
  471. object cellValue = "";
  472. string fieldName = "";
  473. for (int i = 0; i < gridView1.RowCount; i++)
  474. {
  475. row = table.NewRow();
  476. for (int j = 0; j < gridView1.Columns.Count; j++)
  477. {
  478. if (this.gridView1.Columns[j].Visible)
  479. {
  480. fieldName = gridView1.Columns[j].FieldName;
  481. cellValue = gridView1.GetRowCellValue(i, fieldName);
  482. row[fieldName] = cellValue ?? "";
  483. }
  484. }
  485. table.Rows.Add(row);
  486. }
  487. }
  488. string outError = "";
  489. AsposeExcelTools.DataTableToExcel2(table, (String)e.Argument, out outError);
  490. }
  491. //show a message to the user when the background worker has finished
  492. //and re-enable the export buttons
  493. private void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  494. {
  495. if (OnEndExport != null)
  496. {
  497. OnEndExport(this, new EventArgs());
  498. }
  499. if (MessageBox.Show("导出操作完成, 您想打开该Excel文件么?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
  500. {
  501. Process.Start(saveFileDialog.FileName);
  502. }
  503. }
  504. #endregion
  505. #region 菜单操作
  506. private void menu_Delete_Click(object sender, EventArgs e)
  507. {
  508. if (OnDeleteSelected != null && this.ShowDeleteMenu)
  509. {
  510. OnDeleteSelected(this.gridView1, new EventArgs());
  511. }
  512. }
  513. private void menu_Refresh_Click(object sender, EventArgs e)
  514. {
  515. if (this.OnRefresh != null)
  516. {
  517. OnRefresh(this.gridView1, new EventArgs());
  518. }
  519. }
  520. private void menu_Edit_Click(object sender, EventArgs e)
  521. {
  522. if (OnEditSelected != null && this.ShowEditMenu)
  523. {
  524. OnEditSelected(this.gridView1, new EventArgs());
  525. }
  526. }
  527. private void menu_Print_Click(object sender, EventArgs e)
  528. {
  529. PrintDGV.Print_GridView(this.gridView1, this.printTitle);
  530. }
  531. private void menu_Add_Click(object sender, EventArgs e)
  532. {
  533. if (this.OnAddNew != null && this.ShowAddMenu)
  534. {
  535. this.OnAddNew(this.gridView1, new EventArgs());
  536. }
  537. }
  538. private void menu_CopyInfo_Click(object sender, EventArgs e)
  539. {
  540. int[] selectedRow = this.gridView1.GetSelectedRows();
  541. if (selectedRow == null || selectedRow.Length == 0)
  542. return;
  543. StringBuilder sbHeader = new StringBuilder();
  544. StringBuilder sb = new StringBuilder();
  545. if (selectedRow.Length == 1)
  546. {
  547. //单行复制的时候
  548. foreach (GridColumn gridCol in this.gridView1.Columns)
  549. {
  550. if (gridCol.Visible)
  551. {
  552. sbHeader.AppendFormat("{0}:{1} \r\n", gridCol.Caption, this.gridView1.GetRowCellDisplayText(selectedRow[0], gridCol.FieldName));
  553. }
  554. }
  555. sb.AppendLine();
  556. }
  557. else
  558. {
  559. //多行复制的时候
  560. foreach (GridColumn gridCol in this.gridView1.Columns)
  561. {
  562. if (gridCol.Visible)
  563. {
  564. sbHeader.AppendFormat("{0}\t", gridCol.Caption);
  565. }
  566. }
  567. foreach (int row in selectedRow)
  568. {
  569. foreach (GridColumn gridCol in this.gridView1.Columns)
  570. {
  571. if (gridCol.Visible)
  572. {
  573. sb.AppendFormat("{0}\t", this.gridView1.GetRowCellDisplayText(row, gridCol.FieldName));
  574. }
  575. }
  576. sb.AppendLine();
  577. }
  578. }
  579. Clipboard.SetText(sbHeader.ToString() + "\r\n" + sb.ToString());
  580. }
  581. private void menu_SetColumn_Click(object sender, EventArgs e)
  582. {
  583. FrmSelectColumnDisplay dlg = new FrmSelectColumnDisplay();
  584. dlg.DisplayColumNames = this.displayColumns;
  585. dlg.ColumnNameAlias = columnNameAlias;
  586. dlg.DataGridView = this.gridView1;
  587. dlg.ShowDialog();
  588. }
  589. #endregion
  590. private void gridView1_DataSourceChanged(object sender, EventArgs e)
  591. {
  592. #region 修改别名及可见
  593. //先判断设置显示的列(如果没有则显示全部)
  594. string originalName = string.Empty;
  595. string tempColumns = string.Empty;
  596. if (string.IsNullOrEmpty(this.DisplayColumns))
  597. {
  598. for (int i = 0; i < this.gridView1.Columns.Count; i++)
  599. {
  600. originalName = this.gridView1.Columns[i].FieldName;
  601. tempColumns += string.Format("{0},", originalName);
  602. }
  603. tempColumns = tempColumns.Trim(',');
  604. this.DisplayColumns = tempColumns;//全部显示
  605. }
  606. //转换为大写列表
  607. List<string> fixedList = new List<string>();
  608. if (!string.IsNullOrEmpty(FixedColumns))
  609. {
  610. fixedList = FixedColumns.ToUpper().ToDelimitedList<string>(",");
  611. }
  612. //字段的排序顺序,先记录(使用排序的字典)
  613. SortedDictionary<int, string> colIndexList = new SortedDictionary<int, string>();
  614. foreach (GridColumn col in this.gridView1.Columns)
  615. {
  616. //设置列标题
  617. originalName = col.FieldName;
  618. if (columnNameAlias.ContainsKey(originalName.ToUpper()))
  619. {
  620. col.Caption = columnNameAlias[originalName.ToUpper()];
  621. }
  622. else
  623. {
  624. col.Caption = originalName;//如果没有别名用原始字段名称,如ID
  625. }
  626. //设置不显示字段
  627. if (!columnDict.ContainsKey(originalName.ToUpper()))
  628. {
  629. col.Visible = false;
  630. }
  631. //这里先记录每个字段名称,以及它的真实顺序位置
  632. //col.VisibleIndex = GetDisplayColumnIndex(originalName.ToUpper());
  633. int VisibleIndex = GetDisplayColumnIndex(originalName.ToUpper());
  634. if (VisibleIndex == -1)
  635. {
  636. //如果是不显示的,则设置可见顺序为-1
  637. col.VisibleIndex = VisibleIndex;
  638. }
  639. else
  640. {
  641. //否则记录起来后面一并按顺序设置
  642. if (!colIndexList.ContainsKey(VisibleIndex))
  643. {
  644. colIndexList.Add(VisibleIndex, originalName);
  645. }
  646. }
  647. }
  648. //统一设置所有可见的字段顺序
  649. foreach (int index in colIndexList.Keys)
  650. {
  651. originalName = colIndexList[index];
  652. this.gridView1.Columns[originalName].VisibleIndex = index;
  653. }
  654. //设置列固定(大写判断)
  655. for (int i = 0; i < this.gridView1.VisibleColumns.Count; i++)
  656. {
  657. GridColumn col = this.gridView1.VisibleColumns[i];
  658. originalName = col.FieldName;
  659. if (fixedList != null && fixedList.Contains(originalName.ToUpper()))
  660. {
  661. col.Fixed = Fixed;
  662. }
  663. }
  664. #endregion
  665. #region 设置特殊内容显示
  666. object cellValue = "";
  667. string fieldName = "";
  668. for (int i = 0; i < gridView1.RowCount; i++)
  669. {
  670. for (int j = 0; j < gridView1.Columns.Count; j++)
  671. {
  672. fieldName = gridView1.Columns[j].FieldName;
  673. cellValue = gridView1.GetRowCellValue(i, fieldName);
  674. if (cellValue != null && cellValue.GetType() == typeof(DateTime))
  675. {
  676. if (cellValue != DBNull.Value)
  677. {
  678. DateTime dtTemp = DateTime.MinValue;
  679. DateTime.TryParse(cellValue.ToString(), out dtTemp);
  680. TimeSpan ts = dtTemp.Subtract(Convert.ToDateTime("1753/1/1"));
  681. if (ts.TotalDays < 1)
  682. {
  683. gridView1.SetRowCellValue(i, fieldName, DBNull.Value);
  684. }
  685. }
  686. }
  687. }
  688. }
  689. #endregion
  690. if (this.ShowLineNumber)
  691. {
  692. this.gridView1.IndicatorWidth = 40;
  693. }
  694. this.gridView1.OptionsView.ColumnAutoWidth = BestFitColumnWith;
  695. if (BestFitColumnWith)
  696. {
  697. this.gridView1.BestFitColumns();
  698. }
  699. if (ShowCheckBox)
  700. {
  701. GridCheckMarksSelection selection = new GridCheckMarksSelection(gridView1);
  702. selection.CheckMarkColumn.VisibleIndex = 0;
  703. selection.CheckMarkColumn.Width = 60;
  704. selection.SelectionChanged += new SelectionChangedEventHandler(selection_SelectionChanged);
  705. this.gridView1.OptionsBehavior.Editable = true;
  706. this.gridView1.OptionsBehavior.ReadOnly = false;
  707. }
  708. }
  709. void selection_SelectionChanged(object sender, EventArgs e)
  710. {
  711. if (this.OnCheckBoxSelectionChanged != null)
  712. {
  713. this.OnCheckBoxSelectionChanged(sender, e);
  714. }
  715. }
  716. /// <summary>
  717. /// 获取勾选上的行索引列表
  718. /// </summary>
  719. /// <returns></returns>
  720. public List<int> GetCheckedRows()
  721. {
  722. List<int> list = new List<int>();
  723. if (this.ShowCheckBox)
  724. {
  725. for (int rowIndex = 0; rowIndex < this.gridView1.RowCount; rowIndex++)
  726. {
  727. object objValue = this.gridView1.GetRowCellValue(rowIndex, "CheckMarkSelection");
  728. if (objValue != null)
  729. {
  730. bool check = false;
  731. bool.TryParse(objValue.ToString(), out check);
  732. if (check)
  733. {
  734. list.Add(rowIndex);
  735. }
  736. }
  737. }
  738. }
  739. return list;
  740. }
  741. private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
  742. {
  743. if (OnGridViewMouseClick != null)
  744. {
  745. OnGridViewMouseClick(sender, e);
  746. }
  747. }
  748. private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
  749. {
  750. if (this.OnEditSelected != null && this.ShowEditMenu)
  751. {
  752. this.OnEditSelected(this.gridView1, new EventArgs());
  753. }
  754. else if (OnGridViewMouseDoubleClick != null)
  755. {
  756. OnGridViewMouseDoubleClick(this.gridView1, new EventArgs());
  757. }
  758. }
  759. private void toolTipController1_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
  760. {
  761. if (e.SelectedControl != gridControl1) return;
  762. ToolTipControlInfo info = null;
  763. //Get the view at the current mouse position
  764. GridView view = gridControl1.GetViewAt(e.ControlMousePosition) as GridView;
  765. if (view == null) return;
  766. //Get the view's element information that resides at the current position
  767. GridHitInfo hi = view.CalcHitInfo(e.ControlMousePosition);
  768. //Display a hint for row indicator cells
  769. if (hi.HitTest == GridHitTest.RowIndicator)
  770. {
  771. //An object that uniquely identifies a row indicator cell
  772. object o = hi.HitTest.ToString() + hi.RowHandle.ToString();
  773. StringBuilder sb = new StringBuilder();
  774. sb.AppendLine("行数据基本信息:");
  775. foreach (GridColumn gridCol in view.Columns)
  776. {
  777. if (gridCol.Visible)
  778. {
  779. sb.AppendFormat(" {0}:{1}\r\n", gridCol.Caption, view.GetRowCellDisplayText(hi.RowHandle, gridCol.FieldName));
  780. }
  781. }
  782. info = new ToolTipControlInfo(o, sb.ToString());
  783. }
  784. //Supply tooltip information if applicable, otherwise preserve default tooltip (if any)
  785. if (info != null)
  786. {
  787. e.Info = info;
  788. }
  789. }
  790. private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
  791. {
  792. if (ShowLineNumber)
  793. {
  794. e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
  795. if (e.Info.IsRowIndicator)
  796. {
  797. if (e.RowHandle >= 0)
  798. {
  799. e.Info.DisplayText = (e.RowHandle + 1).ToString();
  800. }
  801. }
  802. }
  803. }
  804. private void WinGridViewPager_Load(object sender, EventArgs e)
  805. {
  806. if (!this.DesignMode)
  807. {
  808. this.pager.PageChanged += new PageChangedEventHandler(pager_PageChanged);
  809. this.pager.ExportCurrent += new ExportCurrentEventHandler(pager_ExportCurrent);
  810. this.pager.ExportAll += new ExportAllEventHandler(pager_ExportAll);
  811. this.contextMenuStrip1.Opening += new CancelEventHandler(contextMenuStrip1_Opening);
  812. this.gridControl1.MouseClick += new MouseEventHandler(dataGridView1_MouseClick);
  813. this.gridControl1.MouseDoubleClick += new MouseEventHandler(dataGridView1_MouseDoubleClick);
  814. this.gridView1.Appearance.EvenRow.BackColor = EventRowBackColor;
  815. }
  816. }
  817. private void menu_ColumnWidth_Click(object sender, EventArgs e)
  818. {
  819. this.BestFitColumnWith = !this.BestFitColumnWith;
  820. ShowWidthStatus();
  821. if (OnRefresh != null)
  822. {
  823. OnRefresh(sender, e);
  824. }
  825. }
  826. private void ShowWidthStatus()
  827. {
  828. if (this.BestFitColumnWith)
  829. {
  830. this.menu_ColumnWidth.Text = "设置列固定宽度(&W)";
  831. }
  832. else
  833. {
  834. this.menu_ColumnWidth.Text = "设置列自动适应宽度(&W)";
  835. }
  836. }
  837. }
  838. }