Question

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?

Was it helpful?

Solution

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");
}

OTHER TIPS

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.

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");
}

I think you dont need to any event i solved this problem by this code:

//In Fill DataGridViewEvent :
        DataGridViewCheckBoxColumn ChCol = new DataGridViewCheckBoxColumn();
        ChCol.Name = "CheckedRow";
        ChCol.HeaderText = "انتخاب";
        ChCol.Width = 50;
        ChCol.TrueValue = "1";
        ChCol.FalseValue = "0";
        dg.Columns.Insert(0, ChCol);

// In Button Event put these codes:

            try
            {
                MessageBox.Show(row.Cells[2].Value.ToString() + " --> " +
                row.Cells["CheckedRow"].Value.ToString());
            }
            catch
            {
                // Nothing Act
            }


enter code here

Your Output by this parameter : ItemID --> 0 // For Unchecked Values ItemID --> 1 // For Checked Values

Now You Can filter the rows that return 1 as value for your action i tested this code and it work for me

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top