Loading Grid data with Checkboxcolumn

Asked

Viewed 127 times

2

I am creating a form to perform a registration for a product license.

This form is used to record which solution a customer has.

In the form to add a product just select product and the client.

I am having difficulty to elaborate the logic of loading the form that leaves marked "checked" the products that this customer already owns.

My initial idea and make two queries that return one DataTable, the first consultation the existing products, the second brings the products that the customer has .

First Consultation

SELECT id, nome
FROM   produto

Second Consultation

SELECT produto_id
FROM licenca
JOIN produto on produto_id = produto.id
join pessoa on pessoa.id   = cliente_id
where cliente_id           = @id

So on loading it during loop will make the comparison leaving marked the ones he already owns .

foreach (DataRow row in ProdutosCLiente.Rows)
                {
                    int id = (int)row["produto_id"];
                    GridProduto.Rows[id].Cells[0].Value = CheckState.Checked;
                }

Can anyone tell me what would be the correct way to reference the values that will be filled in? I’m having trouble referencing during the loop.

  • 1

    Can you post a base of your Product and Customer model? In Datagrid only go product data, right?

  • I edited the question showing the query SQL , on the grid are displayed only the solution names. First the user selects the customer sets the initial and final license date and then selects the product.

  • 1

    Take a look at my answer, I edited it showing a way you can do what you need with just one query, without using loops and the like.

2 answers

1

The estate Rows do Gridview is an array containing the rows of it, access to that array is by the row index, not by the product id as you are doing. To do what you want it will take a loop inside another loop, for example:

foreach (DataRow row in ProdutosCLiente.Rows)
{
     int id = (int)row["produto_id"];
     foreach (DataRow rowGrid in GridProduto.Rows)
     {
         if (id == int.Parse(rowGrid.Cells[(Aqui vai o índice da coluna contendo o id do produto)].Value))
         {
             rowGrid.Cells[0].Value = CheckState.Checked;
             break;
         }
     }
}

@Edit:

An easier way to do this is by using only a query to popular the grid, bringing a column that represents whether the user has the product or not. First, in the Checkbox field of your grid, you need to put the following:

<asp:CheckBox ID="id" runat="server" Checked='<%# Convert.ToBoolean(Eval("PossuiProduto")) %>' />

And then you modify the select to return that column using a CASE

SELECT produto.id, produto.nome, CASE WHEN licenca.cliente_id IS NULL THEN 1
 ELSE 0
 END AS PossuiProduto
FROM produto
LEFT JOIN licenca on licenca.produto_id = produto.id
where (licenca.cliente_id = @id OR licenca.cliente_id IS NULL)
  • 1

    Marciano managed to solve , I had to contact my superior because I had to use some properties of Telerik , soon I will post the reply , thank you very much for the help !

0


I managed to solve using some properties of Telerik :

 private void radMultiColumnComboBoxPessoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //Finaliza algum editor em aberto - Grid
                GridProduto.GridViewElement.EditorManager.EndEdit();

                //Inicia atualização - Grid
                GridProduto.TableElement.ViewTemplate.BeginUpdate();

                //Verifica se a pessoa foi selecionada
                if (radMultiColumnComboBoxPessoa.SelectedIndex >= 0)
                {
                    //Obtem a id do cliente selecionado 
                    int Cliente_id = (int)radMultiColumnComboBoxPessoa.EditorControl.Rows[radMultiColumnComboBoxPessoa.SelectedIndex].Cells["id"].Value;

                    //Armazena os produtos que o cliente possui
                    DataTable dataTableLicenca = Negocio.Licenca.Listar(Cliente_id);

                    var produto_ids = (from row in dataTableLicenca.AsEnumerable()
                                       select (int)row.GetValue("produto_id"));

                    foreach (var row in GridProduto.Rows)
                    {
                        if (row is GridViewDataRowInfo)
                        {
                            //Produto
                            int produto_id = (int)((System.Data.DataRowView)(row.DataBoundItem)).Row.GetValue("id");

                            //Valor a ser definido            
                            bool valueState = produto_ids.Contains(produto_id) ? true : false;

                            //Atualiza valor
                            row.Cells[0].Value = valueState;
                        }
                    }
                }
                else
                {
                    foreach (var row in GridProduto.Rows)
                    {
                        if (row is GridViewDataRowInfo)
                        {
                            //Atualiza valor
                            row.Cells[0].Value = false;
                        }
                    }
                }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.