C# Datagridview Checkbox Checked Event - multiple rows?

35,355

Solution 1

On button Click event do:

static int SelectColumnIndex = 0;
PerformAction_Click(object sender, System.EventArgs e)
{
    string data = string.Empty;
    foreach(DataGridViewRow row in MyDataGridView.Rows)
    {
      if(row.Cells[SelectColumnIndex].Value!=null &&
             Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)
      {
        foreach(DataGridViewCell cell in row.Cells)
        {
          if(cell.OwningColumn.Index!= SelectColumnIndex)
          {
            data+= (cell.Value + " ") ; // do some thing
          }
        }
        data+="\n";
      }
   }
   MessageBox.Show(data, "Data");
}

Solution 2

static int SelectColumnIndex = 0;

PerformAction_Click(object sender, System.EventArgs e)
{
    string data = string.Empty;    

    foreach(DataGridViewRow row in MyDataGridView.Rows)     
    {
        if(row.Cells[SelectColumnIndex].Value != null 
            &&
         Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)       
        {
            foreach(DataGridViewCell cell in row.Cells)        
            {
                if (cell.OwningColumn.Index != SelectColumnIndex)                               
                {
                    data+= (cell.Value + " ") ; // do some thing           
                 }
            }

            data+="\n";
        }
    }

    MessageBox.Show(data, "Data");
}

Solution 3

If you want the user to click on a button to perform the action, then what you need to handle is the Click event of the button, not the CheckBox Changed event... When the button is clicked, just go through all rows of your DataGridView and perform an action on rows with a checked checkbox.

Share:
35,355
cheunology
Author by

cheunology

Updated on January 19, 2020

Comments

  • cheunology
    cheunology over 4 years

    I have a datagridview with multiple columns and rows. The first column contains a checkbox. I want the user to be able to select multiple checkboxes and then perform an action. For example if they select checkboxes in rows 1 and 2, the data from other columns in rows 1 and 2 can be selected and passed into a messagebox.

    I know i need to use the checkbox_changed event to do this. However I am having trouble working out how to do this for multiple rows?