WinForm DataGridView header shows hide and place call usage

Time:2020-10-30

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 = " ";
            
            //Test data
            //var list = new List();
            //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;
        }
        //Query
        private void SearchBtn_Click(object sender, EventArgs e)
        {

        }

        //Display settings
        List childList = null;
        private void ShowSetBtn_Click(object sender, EventArgs e)
        {
            //Fill value
            if (childList == null)
            {
                childList = new List();
                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,
                        });
                    }
                }
            }

            //Pass in value
            headsSet = new WeighRecordSet(childList);
            //Return value
            headsSet.itemTextChanged += new EventHandler((sender1, e1) =>
            {
                childList = headsSet.list; //Return value
                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;
                        }
                    }
                }
            });

            //Pop up form
            headsSet.ShowDialog();
        }
    }
public partial class WeighRecordSet : Form
    {
        public List list { get; set; }
        public event EventHandler itemTextChanged;
        public WeighRecordSet()
        {
            InitializeComponent();
            list = new List();
            list.Add (New tableheader() {name = ordinal "});
            list.Add (New tableheader() {name = license plate number});
            list.Add (New tableheader() {name = driver "});
            list.Add (New tableheader() {name = {item "});
            list.Add (New tableheader() {name = gross weight});
            list.Add (New tableheader() {name = "tare"});
            list.Add (New tableheader() {name = net weight});
            list.Add (New tableheader() {name = weight deduction});
            list.Add (New tableheader() {name = real weight});
            list.Add (New tableheader() {name = gross weight time});
            list.Add (New tableheader() {name = tare time});
            list.Add (New tableheader() {name = Order No.});
            list.Add (New tableheader() {name = tare weigher "});
            list.Add (New tableheader() {name = gross weight weigher "});
            list.Add (New tableheader() {name = record time});

            // list.Add (New tableheader() {name = unit price});
            // list.Add (New tableheader() {name = total price});
            // list.Add (New tableheader() {name = concentration "});
            // list.Add (New tableheader() {name = volume});

            list.Add (New tableheader() {name = unit price (yuan)};
            list.Add (New tableheader() {name = total price (RMB) "});
            list.Add (New tableheader() {name = concentration (kg / L) "});
            list.Add (New tableheader() {name = volume (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 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;
            }
        }

        //Move up
        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();
            }

        }
        //Move down
        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();
            }

        }
        //Confirm
        private void SaveBtn_Click(object sender, EventArgs e)
        {
            list = new List();
            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 ("Please tick at least one column! "";
                return;
            }
            //Events
            if (itemTextChanged != null)
            {
                itemTextChanged(this, e);
            }
            this.Close();
        }
    }

 

Recommended Today

C / C + + project development: do library management system with C + +, Xiaobai can also learn!

Today for you to introduce a commonly used management system – library management system, I hope you can personally practice it. Book information includes: login number, book name, author name, classification number, publishing unit, publishing time, price, etc. A book information management system is designed to provide the following functions: (1) Book information input function […]