Click here to Skip to main content
15,845,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If user click on error button user should able to highlight the particular cell for a row/column

What I have tried:

C#
bool validationPassed = true;
            string cellValue = string.Empty;
            try
            {
                for (int iRow = 0; iRow < ValidationGridView.Rows.Count; iRow++)
                {
                    for (int iCol = 0; iCol < ValidationGridView.Columns.Count; iCol++)
                    {
                        if (iCol == 5)  //because it is color column.It is set with default color
                            continue;

                        else if ((iCol == 1) || (iCol == 2))
                        {
                            string value = ValidationGridView[4, iRow].Value.ToString();   //checking the validation rules only for the User created queries.
                            if (value.ToUpper() == "TRUE")
                            {
                                cellValue = ValidationGridView.Rows[iRow].Cells[iCol].Value.ToString();
                                string colName = ValidationGridView.Rows[iRow].Cells[iCol].OwningColumn.Name.ToString();

                                if (string.IsNullOrEmpty(cellValue))
                                {
                                    if (colName == "Rule_Name")
                                    {
                                        System.Windows.Forms.DialogResult dialogResultForSameDescription = System.Windows.Forms.MessageBox.Show("Rule name cannot be empty.", "AVEVA P&ID", System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                    else if (colName == "Description")
                                    {
                                        System.Windows.Forms.DialogResult dialogResultForSameDescription = System.Windows.Forms.MessageBox.Show("Description cannot be empty.", "AVEVA P&ID", System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                    // ValidationGridView.Rows[iRow].Cells[iCol].Style.BackColor = Color.LightGreen;
                                     //ValidationGridView.CurrentCell = ValidationGridView.Rows[iRow-1].Cells[iCol-1];
                                    foreach (DataGridViewRow row in ValidationGridView.Rows)
                                    {
                                        if (row.Cells[0].Value.ToString() == "")
                                        {
                                            row.DefaultCellStyle.BackColor = Color.Tomato;
                                        }
                                    }
                                    validationPassed = false;
                                    break;
                                }
                                else
                                {
                                    if (colName == "Rule_Name")
                                    {
                                        bool chcksameRuleOrDescriptionExists = checkIfSameRuleNameOrDescriptionExist(iRow, 1, cellValue);
                                        if (chcksameRuleOrDescriptionExists)
                                        {
                                            System.Windows.Forms.DialogResult dialogResultForSameDescription = System.Windows.Forms.MessageBox.Show("Duplicate rule name exists.", "AVEVA P&ID", System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                            // ValidationGridView.Rows[iRow].Cells[iCol].Style.BackColor = Color.LightGreen;
                                           // ValidationGridView.CurrentCell = ValidationGridView.Rows[iRow].Cells[iCol];
                                            validationPassed = false;
                                            break;
                                        }
                                        else
                                        {
                                            ValidationGridView.Rows[iRow].Cells[iCol].Style.BackColor = Color.White;
                                        }
                                    }
                                    else if (colName == "Description")
                                    {
                                        bool chcksameRuleExists = checkIfSameRuleNameOrDescriptionExist(iRow, 2, cellValue);
                                        if (chcksameRuleExists)
                                        {
                                            System.Windows.Forms.DialogResult dialogResultForSameDescription = System.Windows.Forms.MessageBox.Show("Duplicate description exists.", "AVEVA P&ID", System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                            // ValidationGridView.Rows[iRow].Cells[iCol].Style.BackColor = Color.LightGreen;
                                             // ValidationGridView.CurrentCell = ValidationGridView.Rows[iRow].Cells[iCol];
                                            validationPassed = false;
                                            break;
                                        }
                                        else
                                        {
                                            ValidationGridView.Rows[iRow].Cells[iCol].Style.BackColor = Color.White;
                                        }
                                    }
                                }
                            }
                        }
                    }


                    if (validationPassed == false)
                        break;
                }
            }
            catch (System.Exception ex)
            {
            }
            return validationPassed;
Posted
Updated 15-Feb-16 23:33pm
v2
Comments
dan!sh 16-Feb-16 4:35am    
Is the code click event handler for the error button?
Member 12330333 17-Feb-16 2:17am    
no not,am trying to highlight the cell in a cell leave event mean when user gave a duplicate name and user leaves the current cell and user tries to enter into another cell user should get error message and where the user is given the duplicate name that current cell should be highlighted and set focus on that

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