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_CellValidati ng(object sender, DataGridViewCellValidating EventArgs e)
{
//Check that all mandatory values are supplied
if (e.ColumnIndex == 5) //if (dataGridView1.Rows[rowInd ex].Cells[ "Column2"] .Value.ToS tring()!= "")
{
if (dataGridView1.Columns[e.C olumnIndex ].Name == "c_attIsMandatory")
{
if (String.IsNullOrEmpty(e.Fo rmattedVal ue.ToStrin g()))
{
dataGridView1.Rows[e.RowIn dex].Error Text = "Mandatory information must be supplied";
e.Cancel = true;
}
}
}
//if ((dataGridView1.Columns[e. ColumnInde x].Name == "c_attIsMandatory") && (dataGridView1.Columns[e.C olumnIndex ].Name == "c_AttVal"))
}
dgv-small.jpg
a. Where the cell is empty
b. The attribute is mandatory.
My code only handles one condition
private void dataGridView1_CellValidati
{
//Check that all mandatory values are supplied
if (e.ColumnIndex == 5) //if (dataGridView1.Rows[rowInd
{
if (dataGridView1.Columns[e.C
{
if (String.IsNullOrEmpty(e.Fo
{
dataGridView1.Rows[e.RowIn
e.Cancel = true;
}
}
}
//if ((dataGridView1.Columns[e.
}
dgv-small.jpg
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.
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.C olumnIndex ].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
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.C
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.