Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I loop the performclick() then after a condition, it'll stop selecting a next row?

What I have tried:

This is my code for next row after i click a button.

private void MaterialRaisedButton6_Click(object sender, EventArgs e)
       {
           if (nRow < dataGridView1.RowCount)
           {
               dataGridView1.Rows[selectedIndex].Selected = false;
               dataGridView1.Rows[++selectedIndex].Selected = true;
               label1.Text = "Sum";
               label2.Text = "Selected Cell:";
               label3.Text = "Average:";

               materialRaisedButton4.PerformClick();
           }
       }


And then this code get a row depending on its start column and end column and a column to display the computed average that was inputted in a textfield.

dataGridView1.ClearSelection();

           string startHeader = this.materialSingleLineTextField2.Text;
           string endHeader = this.materialSingleLineTextField3.Text;
           string aveHeader = this.materialSingleLineTextField4.Text;

           int startIndex = -1;
           int endIndex = -1;

           for (int col= 0;col < this.dataGridView1.Columns.Count;col++)
           {
             if(this.dataGridView1.Columns[col].HeaderText == startHeader)
               {
                   startIndex = col;
               }
               else if (this.dataGridView1.Columns[col].HeaderText == endHeader)
               {
                   endIndex = col;
                   break;
               }
           }

           for(int i = startIndex; i < endIndex; i++)
           {

               dataGridView1.Rows[selectedIndex].Cells[i].Selected = true;
           }

           float next = 0;
           float sum = 0;

           //
           for (int x = 0; x < dataGridView1.SelectedCells.Count; x++)
           {

               try
               {
                   if (!dataGridView1.SelectedCells.Contains(dataGridView1.Rows[x].Cells[x]))
                   {
                       if (float.TryParse(dataGridView1.SelectedCells[x].FormattedValue.ToString(), out next))
                       {
                           sum += next;
                           float ave = dataGridView1.SelectedCells.Count;
                           float get = sum / ave;
                           label1.Text = "Sum: " + sum;
                           label2.Text = "Selected Cell: " + dataGridView1.SelectedCells.Count.ToString();
                           label3.Text = "Average: " + get;

                           if (label3.Text != null)
                           {
                               dataGridView1.Rows[selectedIndex].Cells[materialSingleLineTextField4.Text].Value = get;


                           }



                       }


my code works, its just that i want the button6 to automatically performclick then stops if the selected cells on a row dont have value or there's no value to compute(since my gridview goes way down even the cells there doesnt have values).
Thanks
Posted
Updated 18-Jul-19 20:06pm

1 solution

In terms of UI behavior: I think any automated/repeated selecting/deselecting of elements of a Control is not a good practice, and any multiple clicks from code on a Button which may also have visual effect are a bad practice.

Using a 'while loop, and violating the advice above :):
private void btn_Click(object sender, EventArgs e)
{
    keepGoing = true;

    DataGridViewRow currentRow;
    int currentNdx;

    while (keepGoing)
    {
        currentNdx = dataGridView1.SelectedRows[0].Index;

        if (currentNdx < dataGridView1.RowCount - 1)
        {
            dataGridView1.Rows[currentNdx].Selected = false;

            currentRow = dataGridView1.Rows[++currentNdx];

            currentRow.Selected = true;

            keepGoing = calculateAverage(currentRow, currentNdx);
        }
        else
        {
            keepGoing = false;
        }
    }
}

private bool calculateAverage(DataGridViewRow currentRow, int currentNdx)
{
    // do the business the Labels, and calculation 
    return true; // or false ... depending on ?
}
 
Share this answer
 

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