Check items in the checkedlistbox

1

I'm programming in C # in Visual Studio 2015, and I have a form with a checkedListBox with names of several courses. (The registration is for a student).

By marking and saving, it saves the student's enrollment in the students table, and presents me to my datagridview, where it server for viewing

I need now that when I edit the student's registry, the checkedListBox are checked, in the options that were marked at the time of insertion, but I'm not able to do this, I'm using the datagridview itself to perform the update, that is when I click on the cell of the datagridview it opens another form for me to update the register, I just can not get the information from my checkedlistbox.

In short:

I need to pull the bank and check the CheckedListBox options that have been marked at the student's insertion.

This is my code that throws information from my datagridview to my update form.

private void DG_edit_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        Tela_EditarAluno fmr = new Tela_EditarAluno();

        fmr.TB_cod.Text = DG_edit.CurrentRow.Cells["Cod"].Value.ToString();
        fmr.TB_nome.Text = DG_edit.CurrentRow.Cells["Nome"].Value.ToString();
        fmr.TB_idade.Text = DG_edit.CurrentRow.Cells["Idade"].Value.ToString();
        fmr.TB_endereco.Text = DG_edit.CurrentRow.Cells["Endereço"].Value.ToString();
        fmr.TB_quadra_lote.Text = DG_edit.CurrentRow.Cells["Quadra"].Value.ToString();
        fmr.MD_telefoneFixo.Text = DG_edit.CurrentRow.Cells["Residencial"].Value.ToString();
        fmr.MD_telefoneCel.Text = DG_edit.CurrentRow.Cells["Celular"].Value.ToString();
        fmr.TB_cidade.Text = DG_edit.CurrentRow.Cells["Cidade"].Value.ToString();
        fmr.TB_uf.Text = DG_edit.CurrentRow.Cells["Uf"].Value.ToString();
        fmr.TB_email.Text = DG_edit.CurrentRow.Cells["Email"].Value.ToString();
        fmr.TB_nomepai.Text = DG_edit.CurrentRow.Cells["Pai"].Value.ToString();
        fmr.TB_nomemae.Text = DG_edit.CurrentRow.Cells["Mãe"].Value.ToString();
        fmr.CB_ativo.Text = DG_edit.CurrentRow.Cells["Ativo"].Value.ToString();



        fmr.ShowDialog();
    }

I forgot to mention my checklistbox, which is why I took two prints from the initial sign-up screen and edit screen. insert the description of the image here

This image is from the edit screen.

Thisismyhomescreenforregisteringwithmycheckedlistbox"ACTIVITIES", "OBS: this column is from my students table" I have an Activities column where it plays my STRINGS that have been inserted by the checkedlistbox. and when I click on the cell of the datagridview no as in the first print of edit it has to check and mark the items that were registered in the screen of registration.

    
asked by anonymous 30.08.2017 / 21:19

1 answer

0

First you need to get the index of the CheckedListBox item you need to edit. To make it easier, I'm assuming you want to get the CheckedListBox item through its label. Use this code:

private int GetItemIndex(string item)
{
    int index = 0;

    foreach (object o in myCheckedListBox.Items)
    {
        if (item == o.ToString())
        {
            return index;
        }

        index++;
    }

    return -1;
}

Get the item index:

var index = GetItemIndex("Nome do Item do checkedBoxList");

Then you get the value of the field of your DataGridView that will define whether the item will be checked or not.

bool checked = DG_edit.CurrentRow.Cells["Ativo"].Value.ToString() == "Sim"; // Exemplo

Then you define whether or not that item of CheckedListBox you got the index will be checked:

myCheckedListBox.SetItemChecked(index, checked);

Do this for each item of your CheckedListBox .

References: Get CheckedListBox Item by Name

Edit:

You have your object checkedListBox in your form, it contains all the checkboxes.

You have recorded in the database, or in this case, in your dataGridView whether those check boxes are selected (ticked) or not.

To check each item of your checkedListBox , you must use the method SetItemChecked of CheckedListBox , passing the index of the item you want to check

Example:

// Obter checkBox de Yoga

int indexCbYoga = GetItemIndex("Yoga"); // Utilizando o método GetItemIndex que você vai precisar copiar no seu código, no code behind do seu form

// Pegar o valor da dataGridView que diz se a Yoga está marcada

bool yogaChecked = DG_edit.CurrentRow.Cells["Atividades"].Value.ToString().Contains("Yoga"); // Ou contém "Yoga", não sei como você guarda isto na sua 'DataGridView'

// Checar o valor do Yoga no seu 'checkedListBox'

myCheckedListBox.SetItemChecked(indexCbYoga, yogaChecked); // Se yogaChecked for true, checa o checkBox Yoga, senão, deixa desmarcado.

Edit 2:

private void DG_edit_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    Tela_EditarAluno fmr = new Tela_EditarAluno();

    fmr.TB_cod.Text = DG_edit.CurrentRow.Cells["Cod"].Value.ToString();
    fmr.TB_nome.Text = DG_edit.CurrentRow.Cells["Nome"].Value.ToString();
    fmr.TB_idade.Text = DG_edit.CurrentRow.Cells["Idade"].Value.ToString();
    fmr.TB_endereco.Text = DG_edit.CurrentRow.Cells["Endereço"].Value.ToString();
    fmr.TB_quadra_lote.Text = DG_edit.CurrentRow.Cells["Quadra"].Value.ToString();
    fmr.MD_telefoneFixo.Text = DG_edit.CurrentRow.Cells["Residencial"].Value.ToString();
    fmr.MD_telefoneCel.Text = DG_edit.CurrentRow.Cells["Celular"].Value.ToString();
    fmr.TB_cidade.Text = DG_edit.CurrentRow.Cells["Cidade"].Value.ToString();
    fmr.TB_uf.Text = DG_edit.CurrentRow.Cells["Uf"].Value.ToString();
    fmr.TB_email.Text = DG_edit.CurrentRow.Cells["Email"].Value.ToString();
    fmr.TB_nomepai.Text = DG_edit.CurrentRow.Cells["Pai"].Value.ToString();
    fmr.TB_nomemae.Text = DG_edit.CurrentRow.Cells["Mãe"].Value.ToString();
    fmr.CB_ativo.Text = DG_edit.CurrentRow.Cells["Ativo"].Value.ToString();

    // Obter checkBox de Yoga

    int indexCbYoga = GetItemIndex(fmr.chekedListBox, "Yoga"); // fmr.chekedListBox é o nome do seu CheckedListBox dentro do seu form. 

    // Pegar o valor da dataGridView que diz se a Yoga está marcada

    bool yogaChecked = DG_edit.CurrentRow.Cells["Atividade"].Value.ToString().Contains("Yoga"); // Ou contém "Yoga", não sei como você guarda isto na sua 'DataGridView'

    // Checar o valor do Yoga no seu 'checkedListBox'

    fmr.checkedListBox.SetItemChecked(indexCbYoga, yogaChecked); // Se yogaChecked for true, checa o checkBox Yoga, senão, deixa desmarcado.

    // Fazer o mesmo para as outras opções além de Yoga...

    fmr.ShowDialog();

}

private int GetItemIndex(CheckedListBox checkedListBox, string item)
{
    int index = 0;

    foreach (object o in checkedListBox.Items)
    {
        if (item == o.ToString())
        {
            return index;
        }

        index++;
    }

    return -1;
}
    
30.08.2017 / 22:04