Link to home
Start Free TrialLog in
Avatar of northfields
northfields

asked on

Datagridview cell validation

1.As can be seen in the attached screenshot, the company name attribute (first row) has not been specified, the user should not be able to leave a mandatory row(AttIsMandatory column) before this is done, please provide an example of how to use the error text based on 2 conditions:

a.      Where the cell is empty
b.      The attribute is mandatory.                   
My code only handles one condition
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
               
            //Check that all mandatory values are supplied
            if (e.ColumnIndex == 5) //if (dataGridView1.Rows[rowIndex].Cells["Column2"].Value.ToString()!= "")
            {
                if (dataGridView1.Columns[e.ColumnIndex].Name == "c_attIsMandatory")
                {
                    if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
                    {
                        dataGridView1.Rows[e.RowIndex].ErrorText = "Mandatory information must be supplied";
                        e.Cancel = true;
                    }
                }
            }

            //if ((dataGridView1.Columns[e.ColumnIndex].Name == "c_attIsMandatory") && (dataGridView1.Columns[e.ColumnIndex].Name == "c_AttVal"))
        }


dgv-small.jpg
Avatar of dericstone
dericstone
Flag of Germany image

I don't understand your question. It looks like your code handles both conditions. The only limitation is that your code only works for column 5.
Avatar of northfields
northfields

ASKER

Hi dericstone

When i run the code, the validation doesn't happen. Looks like the first line sets the column that i want validated, iv'e set it to 6 now.
 if (e.ColumnIndex == 6)
         //The second line below, then requires that the adjacent cell in the 3rd column is evaluated
                if (dataGridView1.Columns[e.ColumnIndex].Name == "c_attIsMandatory")
               

See the attached screenshots, the first shot shows that the column value is still set to column 6, and then the second screenshot shows that the validation code is skipped
1.jpg
2.jpg
ASKER CERTIFIED SOLUTION
Avatar of dericstone
dericstone
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial