当用户在datagridview中添加新行时,我想防止空单元格 [英] I want to prevent empty cells when the user adds a new row in the datagridview

查看:59
本文介绍了当用户在datagridview中添加新行时,我想防止空单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在datagridview中添加新行时,我想防止空单元格吗?

因为gridview是绑定到Access数据库的,所以某些字段不允许保留为空.

有人可以帮我吗?

I want to prevent empty cells when the user adds a new row in the datagridview?

Because the gridview is bouned to an access database, some fields are not allowed to stay empty.

Can any one help me???

Thanks in advance!

推荐答案

您可以为Row添加验证,这些验证将显示在RowHeader中,以便您无法提交任何内容.

使用链接 [
You can add validation for the Row and these validations will appear in RowHeader so that you can''t commit anything.

use that link[^]


private void dgv1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
       {


           string headerText = dgv1.Columns[e.ColumnIndex].HeaderText;
           if (!headerText.Equals("ID"))
           if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
           {
               dgv1.Rows[e.RowIndex].ErrorText = "Information not complete";
               e.Cancel = true;
           }


           }





private void dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
           dgv1.Rows[e.RowIndex].ErrorText = string.Empty;
        }


这篇关于当用户在datagridview中添加新行时,我想防止空单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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