判断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以为下次比较做准备.
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
网站栏目:判断DatagridView行修改保存-创新互联
本文网址:http://ybzwz.com/article/dhedhj.html