WinForm DataGridView usage

Time:2020-10-31

//Method one: header exchange, background call

this.dataGridView1.Columns[“ProductName”].DisplayIndex = 0;

 

//Method 2: header exchange, property setting, drag and drop on the page

this.dataGridView1.AllowUserToOrderColumns = true;

 

//3. The time control is empty by default

public WeighRecord()
{
InitializeComponent();

this.RecordTimepx.Format = DateTimePickerFormat.Custom;
this.RecordTimepx.CustomFormat = " ";

}

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

 

 

//4. Pop up window

       private void ShowSetBtn_Click(object sender, EventArgs e)
       {

//Pass in value
            var childList = new List();
            var headsSet = new WeighRecordSet(childList);
            //Event + return value
            headsSet.itemTextChanged += new EventHandler((sender1, e1) =>
            {
                childList =  headsSet.list ; // return the value // / refresh the page with other logic
            });

            //Pop up form
            headsSet.ShowDialog();                 }
public partial class WeighRecordSet : Form
    {
        public List list { get; set; }
        public event EventHandler itemTextChanged;
        public WeighRecordSet()
        {
            InitializeComponent();
        }

        public WeighRecordSet(List list)
        {
            InitializeComponent();
        }

        //Confirm
        private void SaveBtn_Click(object sender, EventArgs e)
        {
            //Events
            if (itemTextChanged != null)
            {
                itemTextChanged(this, e);
            }
            this.Close();
        }
    }

 

//5. Before and after modifying DataGridView cells

//Before modification
        string strBefore = "";
        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {// select row
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;

            strBefore = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            string strField = this.dataGridView1.Columns[e.ColumnIndex].DataPropertyName;
            MessageBox.Show ("before modification" + strbefore) ";
        }
        //After modification
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            string strAfter = this.dataGridView1.CurrentRow.Cells[e.ColumnIndex].Value.ToString();
            if (strAfter == strBefore)
            {
                return;
            }
            MessageBox.Show (after modification = + strafter);
        }

//6. The delete dialog box will pop up

MessageBoxButtons btn = MessageBoxButtons.YesNo;
            if ( MessageBox.Show ("are you sure you want to delete it? Delete data, BTN== DialogResult.Yes )
            {
            }

 

Recommended Today

Docker minimalist Guide

What is docker? Beginners can use the concept of “virtual machine” to understand docker. When we want to build a virtual machine, we need to download two things: virtual machine software (such as VMware) and. ISO file (such as Ubuntu), and then we can use the Ubuntu system in VMware. Similarly, when we want to […]