//方法一 表頭調換, 后台調用
this.dataGridView1.Columns["ProductName"].DisplayIndex = 0;
//方法二 表頭調換,屬性設置, 頁面上拖拽
this.dataGridView1.AllowUserToOrderColumns = true;


//3.時間控件默認為空

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.彈出窗口
private void ShowSetBtn_Click(object sender, EventArgs e)
{
//傳入值 var childList = new List<TableHeader>(); var headsSet = new WeighRecordSet(childList); //事件+回傳值 headsSet.itemTextChanged += new EventHandler((sender1, e1) => { childList = headsSet.list; //回傳值 ///其他邏輯 刷新頁面啥的 }); //彈出窗體 headsSet.ShowDialog();
}
public partial class WeighRecordSet : Form { public List<TableHeader> list { get; set; } public event EventHandler itemTextChanged; public WeighRecordSet() { InitializeComponent(); } public WeighRecordSet(List<TableHeader> list) { InitializeComponent(); } //確定 private void SaveBtn_Click(object sender, EventArgs e) { //事件 if (itemTextChanged != null) { itemTextChanged(this, e); } this.Close(); } }
// 5.dataGridView 單元格修改前后
//修改前 string strBefore = ""; private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) {
//選擇行 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("修改前=" + strBefore); } //修改后 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { string strAfter = this.dataGridView1.CurrentRow.Cells[e.ColumnIndex].Value.ToString(); if (strAfter == strBefore) { return; } MessageBox.Show("修改后=" + strAfter); }
//6.彈出是否刪除對話框
MessageBoxButtons btn = MessageBoxButtons.YesNo; if (MessageBox.Show("確定要刪除么?", "刪除數據", btn) == DialogResult.Yes) { }
