DataGridViewListViewHelper.cs
上传用户:jx_fiona
上传日期:2014-03-08
资源大小:1387k
文件大小:5k
源码类别:

打印编程

开发平台:

Others

  1. using System;
  2. using System.Windows.Forms;
  3. namespace GoldPrinter
  4. {
  5. //注意,为支持VS2005的DataGridView方法,需要设置条件编译常数Vs2005。方法是进入配置属性生成,将条件编译常数DEBUG;TRACE改为DEBUG;TRACE;Vs2005即可。
  6. //但是大家可以把此类专门放到你自己的VS2005工程中(#if Vs2005 #endif去掉),而无需放到金质打印通中,这样,金质打印通即使在VS2003下编译也可以为通用于VS2005。
  7. /// <summary>
  8. /// 提供将ListView、VS2005DataGridView转换成二维数组的方法并提取列宽,供金质打印通打印。
  9. /// </summary>
  10. public class DataGridViewListViewHelper
  11. {
  12. //打印VS2005DataGridView方法示例
  13. // private void btnPrintEasy_Click(object sender, System.EventArgs e)
  14. // {
  15. // GoldPrinter.MisGoldPrinter webmis = new GoldPrinter.MisGoldPrinter();   //打印组件
  16. // webmis.Title = "MIS金质打印通nWWW.WebMIS.COM.CN";                      //标题,还可设置子标题
  17. // (webmis.Title as GoldPrinter.Title).Font = new System.Drawing.Font("宋体", 12, System.Drawing.FontStyle.Bold);
  18. //
  19. // //下面这一句就可以打印DataGridView
  20. // //(webmis.Body as GoldPrinter.Body).DataSource = ToStringArray(dataGridView1, true);
  21. //            
  22. // //为人特性化,自定义表体,可以设置字体、列宽、列对齐方式
  23. // GoldPrinter.Body gridBody = new GoldPrinter.Body();
  24. // //任意二维的数据通通打印,或者是设置GridText属性
  25. // gridBody.DataSource = ToStringArray(dataGridView1, true);
  26. // gridBody.Font = dataGridView1.Font;
  27. // gridBody.ColsWidth = GetColsWidth(dataGridView1);
  28. // webmis.Body = gridBody;
  29. //
  30. // webmis.Preview();
  31. // webmis.Dispose();
  32. // }
  33. #if Vs2005
  34. /// <summary>
  35. /// 获取VS.Net 2005 DataGridView控件的列宽。
  36. /// </summary>
  37. /// <param name="dataGridView">VS.Net 2005 DataGridView控件。</param>
  38. /// <returns>列宽数组。</returns>
  39. public static int[] GetColsWidth(DataGridView dataGridView)
  40. {
  41. #region 实现...
  42. int[] arrReturn = null;
  43. int colsCount = dataGridView.ColumnCount;
  44. arrReturn = new int[colsCount];
  45. for (int i = 0; i < colsCount ; i++)
  46. {
  47. arrReturn[i] = dataGridView.Columns[i].Width;
  48. }
  49. return arrReturn;
  50. #endregion 实现
  51. }
  52. /// <summary>
  53. /// 将VS.Net 2005 DataGridView控件的数据导出到二维数组。
  54. /// </summary>
  55. /// <param name="dataGridView">VS.Net 2005 DataGridView控件。</param>
  56. /// <param name="includeColumnText">是否要把列标题文本也导到数组中。</param>
  57. /// <remarks>
  58. ///  <作者>长江支流</作者>
  59. ///  <日期>2005-12-14</日期>
  60. ///  <修改></修改>
  61. /// </remarks>
  62. /// <returns>二维数组。</returns>
  63. public static string[,] ToStringArray(DataGridView dataGridView, bool includeColumnText)
  64. {
  65. #region 实现...
  66. string[,] arrReturn = null;
  67. int rowsCount = dataGridView.Rows.Count;
  68. int colsCount = dataGridView.Columns.Count;
  69. if (rowsCount > 0)
  70. {
  71. //最后一行是供输入的行时,不用读数据。
  72. if (dataGridView.Rows[rowsCount - 1].IsNewRow)
  73. {
  74. rowsCount--;
  75. }
  76. }
  77. int i = 0;
  78. //包括列标题
  79. if (includeColumnText)
  80. {
  81. rowsCount++;
  82. arrReturn = new string[rowsCount, colsCount];
  83. for (i = 0; i < colsCount; i++)
  84. {
  85. arrReturn[0, i] = dataGridView.Columns[i].HeaderText;
  86. }
  87. i = 1;
  88. }
  89. else
  90. {
  91. arrReturn = new string[rowsCount, colsCount];
  92. }
  93. //读取单元格数据
  94. int rowIndex = 0;
  95. for (; i < rowsCount; i++, rowIndex++)
  96. {
  97. for (int j = 0; j < colsCount; j++)
  98. {
  99. arrReturn[i, j] = dataGridView.Rows[rowIndex].Cells[j].Value.ToString();
  100. }
  101. }
  102. return arrReturn;
  103. #endregion 实现
  104. }
  105. #endif
  106. /// <summary>
  107. /// 获取ListView控件的列宽。
  108. /// </summary>
  109. /// <param name="listView">二维数据视图</param>
  110. /// <returns>列宽数组。</returns>
  111. public static int[] GetColsWidth(ListView listView)
  112. {
  113. #region 实现...
  114. int[] arrReturn = null;
  115. int colsCount = listView.Columns.Count;
  116. arrReturn = new int[colsCount];
  117. for (int i = 0; i < colsCount; i++)
  118. {
  119. arrReturn[i] = listView.Columns[i].Width;
  120. }
  121. return arrReturn;
  122. #endregion 实现
  123. }
  124. /// <summary>
  125. /// 将ListView的数据导出到二维数组。
  126. /// </summary>
  127. /// <param name="listView">二维数据视图</param>
  128. /// <param name="includeColumnText">是否要把列标题文本也导到数组中。</param>
  129. /// <remarks>
  130. ///  <作者>长江支流</作者>
  131. ///  <日期>2005-08-21</日期>
  132. ///  <修改></修改>
  133. /// </remarks>
  134. /// <returns>二维数组。</returns>
  135. public static string[,] ToStringArray(ListView listView, bool includeColumnText)
  136. {
  137. #region 实现...
  138. ListView lvw = listView;
  139. int rowsCount = lvw.Items.Count;
  140. int colsCount = lvw.Columns.Count;
  141. //包括列标题
  142. if (includeColumnText)
  143. {
  144. rowsCount++;
  145. }
  146. string[,] arrReturn = null;
  147. arrReturn = new string[rowsCount, colsCount];
  148. int i = 0;
  149. if (includeColumnText)
  150. {
  151. //写标题
  152. for (i = 0; i < colsCount; i++)
  153. {
  154. arrReturn[0, i] = lvw.Columns[i].Text;
  155. }
  156. i = 1;
  157. }
  158. //写数据行Items
  159. int rowIndex = 0;
  160. for (; i < rowsCount; i++, rowIndex++)
  161. {
  162. for (int j = 0; j < colsCount; j++)
  163. {
  164. arrReturn[i, j] = lvw.Items[rowIndex].SubItems[j].Text;
  165. }
  166. }
  167. return arrReturn;
  168. #endregion 实现
  169. }
  170. }//End Class
  171. }//End Namespace