Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!
Posted

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

use that link[^]
 
Share this answer
 
v3
Comments
Dalek Dave 17-Jun-11 3:31am    
nice link, and I edited for spelling :)
nit_singh 17-Jun-11 3:35am    
Thanks Dave
Ragi Gopi 17-Jun-11 6:32am    
thanku
nit_singh
C#
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;
           }


           }


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900