Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to use button event instead of dataGridView CellValidating event with the following codes. But They are giving some errors.

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
       {
           DataGridViewComboBoxColumn comboBoxColumn = dataGridView1.Columns["kbaskani"] as DataGridViewComboBoxColumn;

           if (comboBoxColumn != null && e.ColumnIndex == comboBoxColumn.Index)
           {
               string newValue = e.FormattedValue.ToString();
               string oldValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

               // Validate the selected value
               if (newValue == "Evet")
               {
                   // Check if another "Yes" option already exists in the column
                   int yesCount = 0;
                   foreach (DataGridViewRow row in dataGridView1.Rows)
                   {
                       if (row.Index != e.RowIndex && row.Cells[e.ColumnIndex].Value.ToString() == "Evet")
                       {
                           yesCount++;
                           break;
                       }
                   }

                   if (yesCount > 0)
                   {
                       MessageBox.Show("You have chosen more than one Yes option. You can choose only one Yes option.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                       e.Cancel = true; // Cancel cell validation
                       return;
                   }
               }
               else if (newValue == "Hayır")
               {
                   // Check if no "Yes" option is chosen
                   int yesCount = 0;
                   foreach (DataGridViewRow row in dataGridView1.Rows)
                   {
                       if (row.Index != e.RowIndex && row.Cells[e.ColumnIndex].Value.ToString() == "Evet")
                       {
                           yesCount++;
                           break;
                       }
                   }

                   if (yesCount == 0)
                   {
                       MessageBox.Show("You haven’t chosen any Yes option. You have to choose one of the Yes options.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                       e.Cancel = true; // Cancel cell validation
                       return;
                   }
               }
           }
       }


What I have tried:

How can we assign the following codes into the bottom event?
Posted
Updated 28-Jul-23 4:53am

You can't, not directly: that event takes a DataGridViewCellValidatingEventArgs parameter, while a Button Click event handler on gets a standard System.EventArgs which doesn't contain the info you are processing.
 
Share this answer
 
Comments
Member 12505620 27-Jul-23 16:17pm    
Is there another way of to validate the combobox selection when I click the button?
The thing I want to do:

1-) If one of “Yes” options is not chosen, I want it to give this warning “You haven’t chosen one any Yes option. You have to choose one of the Yes options.”
2-) If someone chooses more than one Yes option, I want it to give this message “You have chosen more than one Yes option. You can choose only one Yes option.”
You can add the code to a button event and then loop through each row to do your validation for 'Yes'. I have adapted the code a little where if multiple 'Yes' options were selected, it deselects the other options and keeps only one 'Yes' option selected. -
C#
private void btnValidate_Click(object sender, EventArgs e)
{
    DataGridViewComboBoxColumn comboBoxColumn = dataGridView1.Columns["kbaskani"] as DataGridViewComboBoxColumn;

    if (comboBoxColumn != null)
    {
        int yesCount = 0;
        int rowIndexWithMultipleYes = -1;

        //Count the number of 'Yes' options and store the index if multiple 'Yes' options are chosen...
        for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)
        {
            DataGridViewRow row = dataGridView1.Rows[rowIndex];
            string cellValue = row.Cells[comboBoxColumn.Index].Value?.ToString();

            if (cellValue == "Evet")
            {
                yesCount++;

                if (yesCount > 1)
                {
                    rowIndexWithMultipleYes = rowIndex;
                    break;
                }
            }
        }

        //Display your message based on the count of 'Yes' options...
        if (yesCount == 0)
        {
            MessageBox.Show("You haven’t chosen any Yes option. You have to choose one of the Yes options.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else if (yesCount > 1)
        {
            MessageBox.Show("You have chosen more than one Yes option. You can choose only one Yes option.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else if (rowIndexWithMultipleYes != -1)
        {
            //If you find more than one 'Yes' option, but exactly one was chosen, deselect the other one/s...
            for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)
            {
                if (rowIndex != rowIndexWithMultipleYes)
                {
                    dataGridView1.Rows[rowIndex].Cells[comboBoxColumn.Index].Value = "Hayır";
                }
            }
        }
    }
}
 
Share this answer
 
Comments
Member 12505620 28-Jul-23 12:51pm    
Thanks a lot for your interest Andre Oosthuizen. It works perfectly.
Andre Oosthuizen 28-Jul-23 14:39pm    
You're welcome.

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