Click here to Skip to main content
15,908,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,

I have a datagrid,and i dont want the user to change go to any other rows if any of the datagrid cells is empty.Can anyone help?

Thnx in advance!
Posted

First detect and then disable.

Detect:
if myGrid.CurrentCell.Value == null


Disable:
myGrid.Rows[0].ReadOnly = True


Refer similar post here [^]
 
Share this answer
 
v2
Comments
IviKAZAZI 27-Jun-12 8:52am    
I dont want to disable it,i just want the user to be able to change the row only when all cells are filled.
Rajeev Jayaram 27-Jun-12 8:57am    
Ok, then use DataGridView.CellValidating Event. Refer to the updated solution for a similar post.
IviKAZAZI 27-Jun-12 9:08am    
i saw what you posted,but its not what i need. What i want is,when user tries to change the row,a check should occur.if there is any empty cell on the row he currently is,he cannot do that.I just dont know hot to prevent the rowchange. Anyway thnx for the help
C#
 private void datagridview1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (datagridview1.Rows[e.RowIndex].Cells[4].FormattedValue.ToString() == string.Empty)
            {
                e.Cancel = true;
                datagridview1.Rows[e.RowIndex].Cells[4].ErrorText = "Mandatory";
            }
            else
            {
                datagridview1.Rows[e.RowIndex].Cells[4].ErrorText = string.Empty;
            }
}

 private void datagridview1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 4)
            {
                if (e.FormattedValue.ToString() == string.Empty)
                {
                    datagridview1[e.ColumnIndex, e.RowIndex].ErrorText = "Mandatory";
                    e.Cancel = true;
                }
                else
                {
                    datagridview1[e.ColumnIndex, 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