Link a checkboxlist to a datagridview

1

I would like to request your support to select some items from my datagridview when selecting an option in the checkedlistbox. Example if I select option 2 only the last 3 options must be marked on the grid. I only achieve that everything is selected but I want to establish by row index which is what is selected in the grid. This is my form:

I am using language c # in visual studio 2015 and sql server 2016 .. This is my code of the form:

    namespace CapaPresentacion
   {
    public partial class FrmCombos : Form
   {
    public FrmCombos()
    {
        InitializeComponent();
    }

    double Precio = 0;

    private void dataServicio_CellContentClick(object sender, 
    DataGridViewCellEventArgs e)
    {
        if (dataServicio.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "1")
        {
            dataServicio.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "0";
        }
        else
        {
            dataServicio.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "1";
        }
    }

    //metodo ocultar columnas
    private void OcultarColumnas()
    {
        this.dataServicio.Columns[4].Visible = false;
    }

    private void dataServicio_CellValueChanged(object sender, 
    DataGridViewCellEventArgs e)
    {
        if (dataServicio.Columns[e.ColumnIndex].Name == "Seleccionarse")
        {
            DataGridViewRow row = dataServicio.Rows[e.RowIndex];

            DataGridViewCheckBoxCell cellSeleccion = row.Cells["Seleccionarse"] as DataGridViewCheckBoxCell;
        }
    }

    private void dataServicio_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dataServicio.IsCurrentCellDirty)
        {
            dataServicio.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }

    private void FrmCombos_Load(object sender, EventArgs e)
    {
        // TODO: esta línea de código carga datos en la tabla 'dsPrincipal.spcombo_venta' Puede moverla o quitarla según sea necesario.
        this.spcombo_ventaTableAdapter.Fill(this.dsPrincipal.spcombo_venta);
        this.OcultarColumnas();
    }

    private void chkKits_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataServicio.Rows)
        {
          row.Cells[Seleccionarse.Name].Value = true;
          Precio += Convert.ToDouble(row.Cells[3].Value);
        }

        txtTotal.Text = Convert.ToString(Precio);
        txtTotal.Text = (double.Parse(txtTotal.Text)).ToString("#,#.00");
    }
  }
}

Thanks in advance !!!

    
asked by Jorge R. 19.05.2017 в 22:14
source

1 answer

0

Only the first item in the listbox should "check" all the items in the grid? if so, you could try something like this in the event click of CheckListBox :

... 
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    row.Cells[CheckBoxColumn1.Name].Value = true;
}
...

that you would add it to the first item of your checkListBox validating the index of the item:

foreach(int indexChecked in checkedListBox1.CheckedIndices) {
    if (indexChecked = 0) {
       foreach (DataGridViewRow row in dataGridView1.Rows)
       {
          row.Cells[CheckBoxColumn1.Name].Value = true;
       }
    }
}
    
answered by 20.05.2017 в 00:02