判断DatagridView行修改保存

2023-05-21,,

这些天一直在想怎么样才能去判断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以为下次比较做准备.