Block alphabetical sorting in Datagridview

Asked

Viewed 435 times

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 * quantity unit price calculation. As I add items to the datagrid, a function does this calculation and adds the value to the correct line. The problem occurs when I add items whose names start with the same letter; Datagrid groups these products and messes up the order of 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 the ordering of columns by the user, but did not solve the alphabetic order problem. Some help?

valores desordenados Update: I found that the ordering is not alphabetical, but in the order in which the items were registered in the BD. Items that have smaller id’s are inserted at the top of Datagridview and vice versa. Still no solution.

  • How is the structure of query who populate the datagridview?

  • What is the field that is the ordering criterion?

  • I didn’t understand very well the question of structure but if it is the sequence in which the data is saved, it would be this: id, description, unit price, total and subtotal; where id is hidden and subtotal is an additional column that is not in the BD. In the second question: the desired criterion is the insertion order, but it seems that it is ordering by items of the same name.

  • Dener, your question gave me a clue: I think it’s being ordered by ID since this field is hidden. I will try to create a restriction to not allow 2 products with the same ID, because it is illogical two identical items since I can assign quantity.

  • Are you populating datagridview for a query? If you show me how your query is, and show the routine that populates the whole datagridview, with the fields: descricao, preco unitario, quantidade, subtotal.

  • You can have two identical items, however they can differ in some aspect, but I don’t know if it’s a requirement of your system.

  • itemTableAdapter.Insert(id_orcam, id_produto, quantidade, preco_item);
itemTableAdapter.Fill(vorc2DataSet.Item);
dgvpProd.DataSource = produtoTableAdapter.GetDataByIDOrc(id_orcam); //Este é um resumo da rotina

  • This last snippet updates the Datagrid with the following query: SELECT MAX(id) AS Expr1 FROM Budget that is already stored in the datasource.

Show 3 more comments

1 answer

0


Studying the SQL language, I discovered that I can create a "temporary" or "virtual" column and also show the values already calculated without having to do this by Datagridview. I simply created a query that did all the work so that I didn’t have to worry about miscalculations in column reordering cases. Follow 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 row of this query that created the column "Subtotal" already calculated as I needed. I hope it will be useful for other people.

Browser other questions tagged

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