TestListViewEx.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. namespace LYFZ.ComponentLibrary
  11. {
  12. public partial class TestListViewEx : UserControl
  13. {
  14. public TestListViewEx()
  15. {
  16. InitializeComponent();
  17. }
  18. #region 属性
  19. DataTable _Bindtable;
  20. /// <summary>
  21. /// 要绑定数据
  22. /// </summary>
  23. public DataTable Bindtable
  24. {
  25. get { return _Bindtable; }
  26. set
  27. {
  28. _Bindtable = value;
  29. if (_Bindtable != null && _Bindtable.Rows.Count>0)
  30. {
  31. DataForLsitViewAdd();
  32. }
  33. }
  34. }
  35. string _ShowColumnsName;
  36. /// <summary>
  37. /// 要显示出来列名标题名,多个列时,中间用“,”号连接
  38. /// 可以是中文,也可以是英文
  39. /// </summary>
  40. public string ShowColumnsName
  41. {
  42. get { return _ShowColumnsName; }
  43. set { _ShowColumnsName = value; }
  44. }
  45. string _ShowRowsName;
  46. /// <summary>
  47. /// 要显示出来的DataTable的字段名,多个列时,中间用“,”号连接
  48. /// 必须与DataTable中的列名一致
  49. /// </summary>
  50. public string ShowRowsName
  51. {
  52. get { return _ShowRowsName; }
  53. set { _ShowRowsName = value; }
  54. }
  55. string _ShowColumnWhith;
  56. /// <summary>
  57. /// 要显示出来列名Name,多个列时,中间用“,”号连接
  58. /// 可以是中文,也可以是英文
  59. /// </summary>
  60. public string ShowColumnWhith
  61. {
  62. get { return _ShowColumnWhith; }
  63. set { _ShowColumnWhith = value; }
  64. }
  65. string _Text;
  66. /// <summary>
  67. /// 选择后返回的值
  68. /// </summary>
  69. public new string Text
  70. {
  71. get { return _Text; }
  72. set { _Text = value; }
  73. }
  74. #endregion
  75. private void TestListViewEx_Load(object sender, EventArgs e)
  76. {
  77. //lstviw.View;
  78. }
  79. private void DataForLsitViewAdd()
  80. {
  81. lstviw.Columns.Clear();
  82. if (ShowColumnsName != null && ShowColumnsName != "")
  83. {
  84. if (ShowColumnsName.ToString().LastIndexOf(',') == ShowColumnsName.Length - 1)
  85. {
  86. ShowColumnsName.Remove(ShowColumnsName.Length - 1, 1);
  87. }
  88. if (ShowColumnWhith.ToString().LastIndexOf(',') == ShowColumnWhith.Length - 1)
  89. {
  90. ShowColumnWhith.Remove(ShowColumnWhith.Length - 1, 1);
  91. }
  92. string[] splitColumnArry = ShowColumnsName.Split(',');
  93. string[] splitColumnWhithArry = ShowColumnWhith.Split(',');
  94. for (int i = 0; i < splitColumnArry.Length; i++)
  95. {
  96. if (ShowColumnWhith != null && ShowColumnWhith != "" && splitColumnArry.Length == splitColumnWhithArry.Length)
  97. {
  98. try
  99. {
  100. int lvWidth = Convert.ToInt32(splitColumnWhithArry[i].ToString());
  101. lstviw.Columns.Add(splitColumnArry[i].ToString(), lvWidth, HorizontalAlignment.Left);
  102. LVSetAllColWidths(lstviw, lvWidth);
  103. }
  104. catch
  105. {
  106. int lvWidth = this.Width / splitColumnArry.Length;
  107. lstviw.Columns.Add(splitColumnArry[i].ToString(), lvWidth, HorizontalAlignment.Left);
  108. LVSetAllColWidths(lstviw, lvWidth);
  109. }
  110. }
  111. else
  112. {
  113. int lvWidth = this.Width / splitColumnArry.Length;
  114. lstviw.Columns.Add(splitColumnArry[i].ToString(), lvWidth, HorizontalAlignment.Left);
  115. LVSetAllColWidths(lstviw, lvWidth);
  116. }
  117. }
  118. }
  119. if (Bindtable.Rows.Count > 0)
  120. {
  121. if (ShowRowsName.ToString().LastIndexOf(',') == ShowRowsName.Length - 1)
  122. {
  123. ShowRowsName.Remove(ShowRowsName.Length - 1, 1);
  124. }
  125. string[] splitShowRowsNameArry = ShowRowsName.Split(',');
  126. for (int i = 0; i < Bindtable.Rows.Count; i++)
  127. {
  128. ListViewItem item = new ListViewItem(Bindtable.Rows[i][splitShowRowsNameArry[0].ToString()].ToString().Trim());
  129. for (int j = 1; j < splitShowRowsNameArry.Length; j++)
  130. {
  131. item.SubItems.Add(Bindtable.Rows[i][splitShowRowsNameArry[j].ToString()].ToString().Trim());
  132. }
  133. lstviw.Items.Add(item);
  134. }
  135. LVSetAllColWidths(lstviw);
  136. }
  137. }
  138. [DllImport("user32.dll")]
  139. private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  140. public void LVSetAllColWidths(ListView lv, int lvWidth = 50)
  141. {
  142. if (lv.Columns.Count >= 0)
  143. {
  144. for (int i = 1; i < lv.Columns.Count; i++)
  145. {
  146. SendMessage(lv.Handle, 0x1000 + lvWidth, i - 1, -2);//
  147. }
  148. //lv.Columns[0].Width = lv.Columns[0].Width + 2;
  149. }
  150. }
  151. private void lstviw_Click(object sender, EventArgs e)
  152. {
  153. if (lstviw.Items.Count > 0)
  154. {
  155. int IntLength = lstviw.Columns.Count;
  156. Text = "";
  157. //Text += lstviw.Items[lstviw.SelectedItems[0].Index].ToString();
  158. Text += lstviw.Items[0].SubItems[0].Text.Trim();
  159. for (int i = 1; i < lstviw.Columns.Count; i++)
  160. {
  161. Text += "," + lstviw.Items[0].SubItems[i].Text.Trim();
  162. }
  163. }
  164. }
  165. }
  166. }