Block alphabetic ordering in DatagridView

0

My DataGridView receives product data from a database. They are: description, unit price and quantity. I programmatically added a subtotal column and added the unit price calculation * quantity. As I add items to the datagrid, a function does this calculation and adds the value to the correct row. The problem happens when I add items whose names begin with the same letter; Datagrid groups these products and messes the order of the subtotals.

My code:

        foreach (int i in numerolinha) //adiciona valores no subtotal do datagrid
        {
            dgvpProd.Rows[i].Cells["Subtotal"].Value = qtxpreco[i];
        }

        foreach (DataGridViewColumn column in dgvpProd.Columns)//Bloqueia as colunas do Datagrid
        {
            column.SortMode = DataGridViewColumnSortMode.NotSortable;
        }

The second foreach was a solution attempt blocking column sorting by the user, but it does not solve the alphabetical order problem. Any help?

Update: I found that the ordering is not alphabetic, but in the order in which the items were registered in the DB. Items that have smaller id's are inserted at the top of the DatagridView and vice versa. Still no solution.

    
asked by anonymous 31.01.2016 / 19:15

1 answer

0

By studying the SQL language, I discovered that I can create a "temporary" or "virtual" column and still display the already calculated values without having to do this through the DataGridView. I simply created a query that did all the work so I did not have to worry about miscalculations in column reordering cases. Here is the query:

SELECT Produto.nome AS Produto, Item.quantidade AS Quantidade,
Item.preco_item AS Preço, 
Item.quantidade * Item.preco_item AS Subtotal
FROM ((Item INNER JOIN
         Orcamento ON Item.id_orcam = Orcamento.id) INNER JOIN
         Produto ON Item.id_produto = Produto.id)
WHERE (Orcamento.id = ?)

The most important part was the 3rd line of this query that created the "Subtotal" column already calculated as I needed it. I hope it will be useful to other people.

    
06.02.2016 / 19:36