判斷DatagridView行修改保存

這些天一直在想怎麼樣才能去判斷datagridview裏的一行是不是已經被修改,如果是,就保存.最後還是發現用datagridview的rowenter事件來解決了.

方法如下:

1,先定義一個全局變量selectedRow=99999(這個值取一個行number絕對不會出現的即可)和一個對象變量(這裏是computer)如下:

private int selectedRow=99999;

private Computer oldComputer;


2.創建RowEnter事件如下:

private void dgv_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv.CurrentCell != null && selectedRow == 9999)
            {
                int id = cc.Items[dgv.CurrentCell.RowIndex].ID;
                string sn = cc.Items[dgv.CurrentCell.RowIndex].SN;
                string model = cc.Items[dgv.CurrentCell.RowIndex].ModelID;
                DateTime pdate = cc.Items[dgv.CurrentCell.RowIndex].PurchaseDate;
                DateTime wdate = cc.Items[dgv.CurrentCell.RowIndex].WarrantyDate;
                string mac = cc.Items[dgv.CurrentCell.RowIndex].MAC;
                string vendor = cc.Items[dgv.CurrentCell.RowIndex].Vendor;
                string site = cc.Items[dgv.CurrentCell.RowIndex].SiteCode;
                string remarks = cc.Items[dgv.CurrentCell.RowIndex].Remarks;
                oldComputer = new Computer(id, sn, model, pdate, wdate, mac, vendor, site, remarks);
                selectedRow = dgv.CurrentCell.RowIndex;
            }
            else if(dgv.CurrentCell!=null)
            {
                int id = cc.Items[selectedRow].ID;
                string sn = cc.Items[selectedRow].SN;
                string model = cc.Items[selectedRow].ModelID;
                DateTime pdate = cc.Items[selectedRow].PurchaseDate;
                DateTime wdate = cc.Items[selectedRow].WarrantyDate;
                string mac = cc.Items[selectedRow].MAC;
                string vendor = cc.Items[selectedRow].Vendor;
                string site = cc.Items[selectedRow].SiteCode;
                string remarks = cc.Items[selectedRow].Remarks;
                Computer newComputer = new Computer(id, sn, model, pdate, wdate, mac, vendor, site, remarks);
                if (Computer.IsModified(oldComputer, newComputer))
                {
                    try
                    {
                        if (MessageBox.Show("Leave this row will save all changes you've made, are you save to update this record?", "Update Record",
                            MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.Cancel)
                        {
                            newComputer.Update();
                        }
                        else
                        {
                            dgv.Rows[selectedRow].Cells[0].Value = oldComputer.ID;
                            dgv.Rows[selectedRow].Cells[1].Value = oldComputer.SN;
                            dgv.Rows[selectedRow].Cells[2].Value = oldComputer.ModelID;
                            dgv.Rows[selectedRow].Cells[3].Value = oldComputer.PurchaseDate;
                            dgv.Rows[selectedRow].Cells[4].Value = oldComputer.WarrantyDate;
                            dgv.Rows[selectedRow].Cells[5].Value = oldComputer.MAC;
                            dgv.Rows[selectedRow].Cells[6].Value = oldComputer.Vendor;
                            dgv.Rows[selectedRow].Cells[7].Value = oldComputer.SiteCode;
                            dgv.Rows[selectedRow].Cells[8].Value = oldComputer.Remarks;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            if (selectedRow!=9999 && e.RowIndex != selectedRow)
            {
                int id1 = cc.Items[e.RowIndex].ID;
                string sn1 = cc.Items[e.RowIndex].SN;
                string model1 = cc.Items[e.RowIndex].ModelID;
                DateTime pdate1 = cc.Items[e.RowIndex].PurchaseDate;
                DateTime wdate1 = cc.Items[e.RowIndex].WarrantyDate;
                string mac1 = cc.Items[e.RowIndex].MAC;
                string vendor1 = cc.Items[e.RowIndex].Vendor;
                string site1 = cc.Items[e.RowIndex].SiteCode;
                string remarks1 = cc.Items[e.RowIndex].Remarks;
                oldComputer = new Computer(id1, sn1, model1, pdate1, wdate1, mac1, vendor1, site1, remarks1);
                selectedRow = e.RowIndex;
            }


通過觸發兩次RowEnter事件來比較看是否有修改.第一次得到當前行的數據以及行號,第二次再次檢查該行號數據,兩次做對比來決定是否需要更新.


最後把第二次的行號所有數據賦值給oldComputer以爲下次比較做準備.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章