判断DatagridView行修改保存
这些天一直在想怎么样才能去判断datagridview里的一行是不是已经被修改,如果是,就保存.最后还是发现用datagridview的rowenter事件来解决了.
发展壮大离不开广大客户长期以来的信赖与支持,我们将始终秉承“诚信为本、服务至上”的服务理念,坚持“二合一”的优良服务模式,真诚服务每家企业,认真做好每个细节,不断完善自我,成就企业,实现共赢。行业涉及成都广告推广等,在成都网站建设、营销型网站建设、WAP手机网站、VI设计、软件开发等项目上具有丰富的设计经验。
方法如下:
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以为下次比较做准备.
网页题目:判断DatagridView行修改保存
分享路径:http://ybzwz.com/article/gdgpgh.html