Click here to Skip to main content
14,487,639 members

Highlight the particular row in datagridview

Member 12330333 asked:

Open original thread
If user click on error button user should able to highlight the particular cell for a row/column

What I have tried:

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;
Tags: C#, DataGridView

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100