Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datagridview validation based on adjacent cells
 

Datagridview validation based on adjacent cells

Hi all

I have a datagridview that has 2 columns:

Column 1 Column 2

1 Employee name(this is mandatory)
0 Employee nickname (this is optional)
1 Employee gender (this is mandatory)


1st column : Contains an indicator that indicates that a value in the adjacent cell (2nd column)is mandatory ( 1 indicates that a value is mandatory, 0 indicates that the value is optional)
2nd column : Column where the user supplies the value

How can i check that all mandatory values have been supplied, using the error text functionality of the grid.

Thanks
Christopher
cgs1973  Saturday, September 19, 2009 5:18 PM

Hi cgs1973,

We can add a Validating event handler to the DataGridView. In the handler we can check the values and set the ErrorText of the row if necessary. This is a code snippet:

    void dataGridView1_Validating(object sender, CancelEventArgs e)
    {
        bool hasError = false;
        foreach (DataGridViewRow row in this.dataGridView1.Rows)
        {
            object value = row.Cells[0].Value;
            if (value == null)
            {
                row.ErrorText = "The value of the first column cannot be null";
                hasError = true;
            }
            else
            {
                int isMandatory = 0;
                bool parseSucess = int.TryParse(value.ToString(), out isMandatory);
                if (parseSucess == false)
                {
                    row.ErrorText = "The value of the first column is not an integer.";
                    hasError = true;
                }
                else
                {
                    if (isMandatory != 0 && isMandatory != 1)
                    {
                        row.ErrorText = "The value of the first column must be 0 or 1.";
                        hasError = true;
                    }
                    else if (isMandatory == 1)
                    {
                        //Column 2 is mandatory, so it cannot be null.
                        object value2 = row.Cells[1].Value;
                        if (value2 == null || value2.ToString().Trim() == "")
                        {
                            row.ErrorText = "The value of the first column is 1, so the value of the second column cannot be null.";
                            hasError = true;
                        }
                    }
                }
            }
        }
        if (hasError)
            e.Cancel = true;
    }



Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, September 22, 2009 8:41 AM

Hi cgs1973,

We can add a Validating event handler to the DataGridView. In the handler we can check the values and set the ErrorText of the row if necessary. This is a code snippet:

    void dataGridView1_Validating(object sender, CancelEventArgs e)
    {
        bool hasError = false;
        foreach (DataGridViewRow row in this.dataGridView1.Rows)
        {
            object value = row.Cells[0].Value;
            if (value == null)
            {
                row.ErrorText = "The value of the first column cannot be null";
                hasError = true;
            }
            else
            {
                int isMandatory = 0;
                bool parseSucess = int.TryParse(value.ToString(), out isMandatory);
                if (parseSucess == false)
                {
                    row.ErrorText = "The value of the first column is not an integer.";
                    hasError = true;
                }
                else
                {
                    if (isMandatory != 0 && isMandatory != 1)
                    {
                        row.ErrorText = "The value of the first column must be 0 or 1.";
                        hasError = true;
                    }
                    else if (isMandatory == 1)
                    {
                        //Column 2 is mandatory, so it cannot be null.
                        object value2 = row.Cells[1].Value;
                        if (value2 == null || value2.ToString().Trim() == "")
                        {
                            row.ErrorText = "The value of the first column is 1, so the value of the second column cannot be null.";
                            hasError = true;
                        }
                    }
                }
            }
        }
        if (hasError)
            e.Cancel = true;
    }



Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, September 22, 2009 8:41 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView event handlers: just what is allowed?
• DataGridView Update to Source Database
• integer and decimal value in cell of the datagridview????
• i want to get all buttons in the form
• How can I add rows programmatically to a bound datagridcombobox column?
• Can I tell if the header row in a bound grid is clicked
• tableAdapter's fill and get method giving different results
• unknown problem in binding an ArrayList to a Datagridview!
• Need help with checkboxes on a datagrid
• Bug? Datagrid not matching tablestyle at design time