datagridview检查记录 [英] datagridview checking records

查看:73
本文介绍了datagridview检查记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

datagridview dgv是从数据表dt中填充的.
在dgv中,我可以添加记录,因为网格允许添加新记录.
在单击保存"按钮之前,如何检查dgv中是否已存在添加的记录?

谢谢

datagridview dgv is populated from a datatable dt.
Within dgv I can add records as the grid allows adding a new record.
Before clicking on the save button, how can I check if the added records is already present in the dgv?

Thanks

推荐答案

能否在将数据绑定/填充到网格的地方发布代码?
Can you please post the code where you bind/populate the data to the grid please ?


防止重复在编辑网格时记录,处理RowValidating.在处理程序中,检查记录是否唯一,除非该记录不允许,否则不允许用户退出该行.这样,您的网格只能包含唯一的记录.
如果您在单击保存"时创建重复项,则在Validating事件(网格验证)中执行相同的检查(尽管将触发RowValidating事件,在这种情况下,它不会阻止用户退出该行).

如果要在单击保存"之前允许重复记录,只需在Validating事件处理程序中进行验证.这样,在删除重复项之前,用户无法离开网格.

我假设您具有检查重复项的知识.您需要解析DataTable进行检查.
To prevent duplicate records while the grid is being edited, handle RowValidating. In the handler, check that the record is unique and do not allow the user to exit the row unless it is. That way, your grid can only ever contain unique records.
Perform the same check in the Validating event (grid validation) in case you are creating a duplicate when you click save (although the RowValidating event will fire, in this case it won''t stop the user exiting the row).

If you want to allow duplicate records until Save is clicked, just do verification in the Validating event handler. Then the user cannot leave the grid until the duplicate is deleted.

I''ve assumed that you have the knowledge to check for duplicates. You need to parse the DataTable to check that.


  var rowCollection = dataGridView1.Rows.OfType<datagridviewrow>().Where(r => !r.IsNewRow).ToList();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.IsNewRow)
                {
                    rowCollection.ForEach(f =>
                        {
                            //code to check on duplication
                        });
                }
            }

</datagridviewrow>


这篇关于datagridview检查记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆