Winform dataGridView 表头展示隐藏 和 位置调用 用法

  public partial class WeighRecord : Form
    {
        WeighRecordSet headsSet = null;
        public WeighRecord()
        {
            InitializeComponent();

            this.RecordTimepx.Format = DateTimePickerFormat.Custom;
            this.RecordTimepx.CustomFormat = " ";
            this.GrossTimepx.Format = DateTimePickerFormat.Custom;
            this.GrossTimepx.CustomFormat = " ";
            this.TareTimepx.Format = DateTimePickerFormat.Custom;
            this.TareTimepx.CustomFormat = " ";
            
            //测试数据
            //var list = new List<WeighRecordDto>();
            //list.Add(new WeighRecordDto() { Number = 1 });
            //list.Add(new WeighRecordDto() { Number = 2 });
            //list.Add(new WeighRecordDto() { Number = 3 });
            //list.Add(new WeighRecordDto() { Number = 4 });
            //list.Add(new WeighRecordDto() { Number = 5 });

            //this.dataGV.DataSource = list;
        }

        private void RecordTimepx_ValueChanged(object sender, EventArgs e)
        {
            this.RecordTimepx.Format = DateTimePickerFormat.Long;
            this.RecordTimepx.CustomFormat = null;
        }

        private void GrossTimepx_ValueChanged(object sender, EventArgs e)
        {
            this.GrossTimepx.Format = DateTimePickerFormat.Long;
            this.GrossTimepx.CustomFormat = null;
        }

        private void TareTimepx_ValueChanged(object sender, EventArgs e)
        {
            this.TareTimepx.Format = DateTimePickerFormat.Long;
            this.TareTimepx.CustomFormat = null;
        }
        //查询
        private void SearchBtn_Click(object sender, EventArgs e)
        {

        }

        //显示设置
        List<TableHeader> childList = null;
        private void ShowSetBtn_Click(object sender, EventArgs e)
        {
            //填充值
            if (childList == null)
            {
                childList = new List<TableHeader>();
                for (var i = 0; i < this.WeighTable.Columns.Count; i++)
                {
                    if (this.WeighTable.Columns[i].Visible)
                    {
                        childList.Add(new TableHeader()
                        {
                            Name = this.WeighTable.Columns[i].HeaderText,
                            Choice = this.WeighTable.Columns[i].Visible,
                        });
                    }
                }
            }

            //传入值
            headsSet = new WeighRecordSet(childList);
            //回传值
            headsSet.itemTextChanged += new EventHandler((sender1, e1) =>
            {
                childList = headsSet.list; //回传值
                for (var j = 0; j < headsSet.list.Count; j++)
                {
                    var item = headsSet.list[j];
                    for (var i = 0; i < this.WeighTable.Columns.Count; i++)
                    {
                        if (this.WeighTable.Columns[i].HeaderText == item.Name)
                        {
                            this.WeighTable.Columns[i].DisplayIndex = j;
                            this.WeighTable.Columns[i].Visible = item.Choice;
                            break;
                        }
                    }
                }
            });

            //弹出窗体
            headsSet.ShowDialog();
        }
    }
  public partial class WeighRecordSet : Form
    {
        public List<TableHeader> list { get; set; }
        public event EventHandler itemTextChanged;
        public WeighRecordSet()
        {
            InitializeComponent();
            list = new List<TableHeader>();
            list.Add(new TableHeader() { Name = "序号" });
            list.Add(new TableHeader() { Name = "车牌号" });
            list.Add(new TableHeader() { Name = "司机" });
            list.Add(new TableHeader() { Name = "货品" });
            list.Add(new TableHeader() { Name = "毛重" });
            list.Add(new TableHeader() { Name = "皮重" });
            list.Add(new TableHeader() { Name = "净重" });
            list.Add(new TableHeader() { Name = "扣重" });
            list.Add(new TableHeader() { Name = "实重" });
            list.Add(new TableHeader() { Name = "毛重时间" });
            list.Add(new TableHeader() { Name = "皮重时间" });
            list.Add(new TableHeader() { Name = "单号" });
            list.Add(new TableHeader() { Name = "皮重司磅员" });
            list.Add(new TableHeader() { Name = "毛重司磅员" });
            list.Add(new TableHeader() { Name = "记录时间" });

            //list.Add(new TableHeader() { Name = "单价" });
            //list.Add(new TableHeader() { Name = "总价" });
            //list.Add(new TableHeader() { Name = "浓度" });
            //list.Add(new TableHeader() { Name = "体积" });

            list.Add(new TableHeader() { Name = "单价(元)" });
            list.Add(new TableHeader() { Name = "总价(元)" });
            list.Add(new TableHeader() { Name = "浓度(kg/L)" });
            list.Add(new TableHeader() { Name = "体积(L)" });

            for (var i = 0; i < list.Count; i++)
            {
                this.HeaderLv.Items.Add(list[i].Name);
                this.HeaderLv.Items[i].Checked = list[i].Choice;
            }
            
        }

        public WeighRecordSet(List<TableHeader> list)
        {
            InitializeComponent();
            for (var i = 0; i < list.Count; i++)
            {
                this.HeaderLv.Items.Add(list[i].Name);
                this.HeaderLv.Items[i].Checked = list[i].Choice;
            }
        }

        //上移
        private void UpperBtn_Click(object sender, EventArgs e)
        {
            if (this.HeaderLv.SelectedItems.Count == 0)
            {
                return;
            }

            HeaderLv.BeginUpdate();

            if (this.HeaderLv.SelectedItems[0].Index > 0)
            {
                foreach (ListViewItem var in HeaderLv.SelectedItems)
                {
                    ListViewItem item = var;
                    int indexSelectedItem = var.Index;
                    HeaderLv.Items.RemoveAt(indexSelectedItem);
                    HeaderLv.Items.Insert(indexSelectedItem - 1, item);
                }
            }
            HeaderLv.EndUpdate();

            if (HeaderLv.Items.Count > 0 && HeaderLv.SelectedItems.Count > 0)
            {
                HeaderLv.Focus();
                HeaderLv.SelectedItems[0].Focused = true;
                HeaderLv.SelectedItems[0].EnsureVisible();
            }

        }
        //下移
        private void UnderBtn_Click(object sender, EventArgs e)
        {
            if (this.HeaderLv.SelectedItems.Count == 0)
            {
                return;
            }
            this.HeaderLv.BeginUpdate();
            int indexMaxSelectedItem = HeaderLv.SelectedItems[HeaderLv.SelectedItems.Count - 1].Index;

            if (indexMaxSelectedItem < HeaderLv.Items.Count - 1)
            {
                for (int i = this.HeaderLv.SelectedItems.Count - 1; i >= 0; i--)
                {
                    ListViewItem item = this.HeaderLv.SelectedItems[i];
                    int indexSelectedItem = item.Index;
                    HeaderLv.Items.RemoveAt(indexSelectedItem);
                    HeaderLv.Items.Insert(indexSelectedItem + 1, item);
                }
            }
            HeaderLv.EndUpdate();

            if (HeaderLv.Items.Count > 0 && HeaderLv.SelectedItems.Count > 0)
            {
                HeaderLv.Focus();
                HeaderLv.SelectedItems[HeaderLv.SelectedItems.Count - 1].Focused = true;
                HeaderLv.SelectedItems[HeaderLv.SelectedItems.Count - 1].EnsureVisible();
            }

        }
        //确定
        private void SaveBtn_Click(object sender, EventArgs e)
        {
            list = new List<TableHeader>();
            for (var i = 0; i < this.HeaderLv.Items.Count; i++)
            {
                list.Add(new TableHeader()
                {
                    Name = this.HeaderLv.Items[i].Text,
                    Choice = this.HeaderLv.Items[i].Checked,
                });
            }
            if (!list.Exists(s => s.Choice))
            {
                MessageBox.Show("请至少勾上一列!");
                return;
            }
            //事件
            if (itemTextChanged != null)
            {
                itemTextChanged(this, e);
            }
            this.Close();
        }
    }

 

posted @ 2020-09-30 15:17  cclon  阅读(1753)  评论(0编辑  收藏  举报