123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace LYFZ.ComponentLibrary
- {
- public partial class TestListViewEx : UserControl
- {
- public TestListViewEx()
- {
- InitializeComponent();
- }
- #region 属性
- DataTable _Bindtable;
- /// <summary>
- /// 要绑定数据
- /// </summary>
- public DataTable Bindtable
- {
- get { return _Bindtable; }
- set
- {
- _Bindtable = value;
- if (_Bindtable != null && _Bindtable.Rows.Count>0)
- {
- DataForLsitViewAdd();
- }
- }
- }
- string _ShowColumnsName;
- /// <summary>
- /// 要显示出来列名标题名,多个列时,中间用“,”号连接
- /// 可以是中文,也可以是英文
- /// </summary>
- public string ShowColumnsName
- {
- get { return _ShowColumnsName; }
- set { _ShowColumnsName = value; }
- }
- string _ShowRowsName;
- /// <summary>
- /// 要显示出来的DataTable的字段名,多个列时,中间用“,”号连接
- /// 必须与DataTable中的列名一致
- /// </summary>
- public string ShowRowsName
- {
- get { return _ShowRowsName; }
- set { _ShowRowsName = value; }
- }
- string _ShowColumnWhith;
- /// <summary>
- /// 要显示出来列名Name,多个列时,中间用“,”号连接
- /// 可以是中文,也可以是英文
- /// </summary>
- public string ShowColumnWhith
- {
- get { return _ShowColumnWhith; }
- set { _ShowColumnWhith = value; }
- }
- string _Text;
- /// <summary>
- /// 选择后返回的值
- /// </summary>
- public new string Text
- {
- get { return _Text; }
- set { _Text = value; }
- }
- #endregion
- private void TestListViewEx_Load(object sender, EventArgs e)
- {
- //lstviw.View;
- }
- private void DataForLsitViewAdd()
- {
- lstviw.Columns.Clear();
- if (ShowColumnsName != null && ShowColumnsName != "")
- {
- if (ShowColumnsName.ToString().LastIndexOf(',') == ShowColumnsName.Length - 1)
- {
- ShowColumnsName.Remove(ShowColumnsName.Length - 1, 1);
- }
- if (ShowColumnWhith.ToString().LastIndexOf(',') == ShowColumnWhith.Length - 1)
- {
- ShowColumnWhith.Remove(ShowColumnWhith.Length - 1, 1);
- }
- string[] splitColumnArry = ShowColumnsName.Split(',');
- string[] splitColumnWhithArry = ShowColumnWhith.Split(',');
- for (int i = 0; i < splitColumnArry.Length; i++)
- {
- if (ShowColumnWhith != null && ShowColumnWhith != "" && splitColumnArry.Length == splitColumnWhithArry.Length)
- {
- try
- {
- int lvWidth = Convert.ToInt32(splitColumnWhithArry[i].ToString());
- lstviw.Columns.Add(splitColumnArry[i].ToString(), lvWidth, HorizontalAlignment.Left);
- LVSetAllColWidths(lstviw, lvWidth);
- }
- catch
- {
- int lvWidth = this.Width / splitColumnArry.Length;
- lstviw.Columns.Add(splitColumnArry[i].ToString(), lvWidth, HorizontalAlignment.Left);
- LVSetAllColWidths(lstviw, lvWidth);
- }
- }
- else
- {
- int lvWidth = this.Width / splitColumnArry.Length;
- lstviw.Columns.Add(splitColumnArry[i].ToString(), lvWidth, HorizontalAlignment.Left);
- LVSetAllColWidths(lstviw, lvWidth);
- }
- }
- }
- if (Bindtable.Rows.Count > 0)
- {
- if (ShowRowsName.ToString().LastIndexOf(',') == ShowRowsName.Length - 1)
- {
- ShowRowsName.Remove(ShowRowsName.Length - 1, 1);
- }
- string[] splitShowRowsNameArry = ShowRowsName.Split(',');
- for (int i = 0; i < Bindtable.Rows.Count; i++)
- {
- ListViewItem item = new ListViewItem(Bindtable.Rows[i][splitShowRowsNameArry[0].ToString()].ToString().Trim());
- for (int j = 1; j < splitShowRowsNameArry.Length; j++)
- {
- item.SubItems.Add(Bindtable.Rows[i][splitShowRowsNameArry[j].ToString()].ToString().Trim());
- }
- lstviw.Items.Add(item);
- }
- LVSetAllColWidths(lstviw);
- }
- }
- [DllImport("user32.dll")]
- private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
- public void LVSetAllColWidths(ListView lv, int lvWidth = 50)
- {
- if (lv.Columns.Count >= 0)
- {
- for (int i = 1; i < lv.Columns.Count; i++)
- {
- SendMessage(lv.Handle, 0x1000 + lvWidth, i - 1, -2);//
- }
- //lv.Columns[0].Width = lv.Columns[0].Width + 2;
- }
- }
- private void lstviw_Click(object sender, EventArgs e)
- {
- if (lstviw.Items.Count > 0)
- {
- int IntLength = lstviw.Columns.Count;
- Text = "";
- //Text += lstviw.Items[lstviw.SelectedItems[0].Index].ToString();
- Text += lstviw.Items[0].SubItems[0].Text.Trim();
- for (int i = 1; i < lstviw.Columns.Count; i++)
- {
- Text += "," + lstviw.Items[0].SubItems[i].Text.Trim();
- }
- }
- }
- }
-
- }
|